[glib: 1/2] garray: Optimise over-allocations with g_array_insert_vals()



commit e1e800299896dd28b924e42807bca6052b4732e1
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Jun 4 11:42:24 2018 +0100

    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>

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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]