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 <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2018-06-04 11:42:24 +01:00
parent e0f82b9494
commit e1e8002998

View File

@ -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);