From c1b6c98589747e66e8b2b8db6b1e746498e7f552 Mon Sep 17 00:00:00 2001 From: Gert Wollny Date: Sat, 24 Nov 2018 11:55:28 +0100 Subject: [PATCH] vrend: reduce number of re-allocations in constant copying Also use memcpy to copy the constats. These two things might shave of some cycles but the measureable performance benefit is marginal. v2: Avoid the memcpy that ralloc does when increasing the size, the data is overwritten anyway. Reviewed-by: Gurchetan Singh Signed-off-by: Gert Wollny Signed-off-by: Jakob Bornecrantz --- src/vrend_renderer.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/vrend_renderer.c b/src/vrend_renderer.c index 0719447..cc97ca4 100644 --- a/src/vrend_renderer.c +++ b/src/vrend_renderer.c @@ -420,6 +420,7 @@ struct vrend_vertex_element_array { struct vrend_constants { unsigned int *consts; uint32_t num_consts; + uint32_t num_allocated_consts; }; struct vrend_shader_view { @@ -2379,18 +2380,21 @@ void vrend_set_constants(struct vrend_context *ctx, float *data) { struct vrend_constants *consts; - uint i; consts = &ctx->sub->consts[shader]; ctx->sub->const_dirty[shader] = true; - consts->consts = realloc(consts->consts, num_constant * sizeof(float)); - if (!consts->consts) - return; + /* avoid reallocations by only growing the buffer */ + if (consts->num_allocated_consts < num_constant) { + free(consts->consts); + consts->consts = malloc(num_constant * sizeof(float)); + if (!consts->consts) + return; + consts->num_allocated_consts = num_constant; + } + memcpy(consts->consts, data, num_constant * sizeof(unsigned int)); consts->num_consts = num_constant; - for (i = 0; i < num_constant; i++) - consts->consts[i] = ((unsigned int *)data)[i]; } void vrend_set_uniform_buffer(struct vrend_context *ctx,