From 171f698ead70bd0450b530069b9f146094148050 Mon Sep 17 00:00:00 2001 From: Hans Petter Jansson Date: Tue, 10 Jul 2018 12:48:26 +0200 Subject: [PATCH] ghash: Simplify g_hash_table_set_shift() Even if we're using a prime modulo for the initial probe, our table is power-of-two-sized, meaning we can set the mask simply by subtracting one from the size. --- glib/ghash.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/glib/ghash.c b/glib/ghash.c index 888e67e00..11d71627e 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -297,19 +297,15 @@ static const gint prime_mod [] = static void g_hash_table_set_shift (GHashTable *hash_table, gint shift) { - gint i; - guint mask = 0; - hash_table->size = 1 << shift; hash_table->mod = prime_mod [shift]; - for (i = 0; i < shift; i++) - { - mask <<= 1; - mask |= 1; - } + /* hash_table->size is always a power of two, so we can calculate the mask + * by simply subtracting 1 from it. The leading assertion ensures that + * we're really dealing with a power of two. */ - hash_table->mask = mask; + g_assert ((hash_table->size & (hash_table->size - 1)) == 0); + hash_table->mask = hash_table->size - 1; } static gint