[glib: 5/9] ghash: Be less eager to opportunistically grow the table on cleanup



commit 194eef5f178cda516c3038faa3e5547a86af58c3
Author: Hans Petter Jansson <hpj cl no>
Date:   Mon Jul 30 13:37:42 2018 +0200

    ghash: Be less eager to opportunistically grow the table on cleanup
    
    When g_hash_table_resize() gets called, we clear out tombstones and grow
    the table at the same time if needed. However, the threshold was set too
    low, so we'd grow if the load was greater than .5 after subtracting
    tombstones. Increase this threshold to ~.75.

 glib/ghash.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
---
diff --git a/glib/ghash.c b/glib/ghash.c
index 70fd07926..83252b833 100644
--- a/glib/ghash.c
+++ b/glib/ghash.c
@@ -814,7 +814,18 @@ g_hash_table_resize (GHashTable *hash_table)
   old_size = hash_table->size;
   is_a_set = hash_table->keys == hash_table->values;
 
-  g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
+  /* The outer checks in g_hash_table_maybe_resize() will only consider
+   * cleanup/resize when the load factor goes below .25 (1/4, ignoring
+   * tombstones) or above .9375 (15/16, including tombstones).
+   *
+   * Once this happens, tombstones will always be cleaned out. If our
+   * load sans tombstones is greater than .75 (1/1.333, see below), we'll
+   * take this opportunity to grow the table too.
+   *
+   * Immediately after growing, the load factor will be in the range
+   * .375 .. .469. After shrinking, it will be exactly .5. */
+
+  g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 1.333);
 
   if (hash_table->size > old_size)
     {


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