From e1e800299896dd28b924e42807bca6052b4732e1 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 4 Jun 2018 11:42:24 +0100 Subject: [PATCH] garray: Optimise over-allocations with g_array_insert_vals() When over-allocating by inserting values off the end of an array, maybe expand the array once, rather than twice (once for setting the size and once for appending the values). Suggestion by Peter Bloomfield (@peterb) as a follow-up to #1374. Signed-off-by: Philip Withnall --- glib/garray.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/glib/garray.c b/glib/garray.c index 5fa8c4663..39c87ca88 100644 --- a/glib/garray.c +++ b/glib/garray.c @@ -546,7 +546,10 @@ g_array_insert_vals (GArray *farray, /* Is the index off the end of the array, and hence do we need to over-allocate * and clear some elements? */ if (index_ >= array->len) + { + g_array_maybe_expand (array, index_ - array->len + len); return g_array_append_vals (g_array_set_size (farray, index_), data, len); + } g_array_maybe_expand (array, len); -- 2.11.4.GIT