htable: reduce size of htable by storing perfect bitnum, not mask.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 1 Apr 2019 23:49:19 +0000 (10:19 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 1 Apr 2019 23:49:19 +0000 (10:19 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/htable/htable.c
ccan/htable/htable.h
ccan/htable/test/run-debug.c
ccan/htable/test/run.c
ccan/htable/tools/speed.c

index 2028d336aec20bff59201c13074602dadd144ec7..b27d063f207000b65fbbdd14aed05f171c279d09 100644 (file)
@@ -58,6 +58,15 @@ static inline bool entry_is_valid(uintptr_t e)
        return e > HTABLE_DELETED;
 }
 
+/* We use 0 to mean we don't have a perfect bit, otherwise it's
+ * bit n - 1 */
+static inline uintptr_t ht_perfect_mask(const struct htable *ht)
+{
+       if (ht->perfect_bitnum > 0)
+               return (uintptr_t)1 << (ht->perfect_bitnum - 1);
+       return 0;
+}
+
 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
                                          size_t hash)
 {
@@ -65,7 +74,7 @@ static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
         * end is quite expensive.  But the lower bits are redundant, so
         * we fold the value first. */
        return (hash ^ (hash >> ht->bits))
-               & ht->common_mask & ~ht->perfect_bit;
+               & ht->common_mask & ~ht_perfect_mask(ht);
 }
 
 void htable_init(struct htable *ht,
@@ -75,7 +84,7 @@ void htable_init(struct htable *ht,
        *ht = empty;
        ht->rehash = rehash;
        ht->priv = priv;
-       ht->table = &ht->perfect_bit;
+       ht->table = &ht->common_bits;
 }
 
 static inline size_t ht_max(const struct htable *ht)
@@ -102,7 +111,7 @@ bool htable_init_sized(struct htable *ht,
 
        ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
        if (!ht->table) {
-               ht->table = &ht->perfect_bit;
+               ht->table = &ht->common_bits;
                return false;
        }
        (void)htable_debug(ht, HTABLE_LOC);
@@ -111,7 +120,7 @@ bool htable_init_sized(struct htable *ht,
        
 void htable_clear(struct htable *ht)
 {
-       if (ht->table != &ht->perfect_bit)
+       if (ht->table != &ht->common_bits)
                htable_free(ht, (void *)ht->table);
        htable_init(ht, ht->rehash, ht->priv);
 }
@@ -154,7 +163,7 @@ void *htable_firstval_(const struct htable *ht,
                       struct htable_iter *i, size_t hash)
 {
        i->off = hash_bucket(ht, hash);
-       return htable_val(ht, i, hash, ht->perfect_bit);
+       return htable_val(ht, i, hash, ht_perfect_mask(ht));
 }
 
 void *htable_nextval_(const struct htable *ht,
@@ -197,7 +206,7 @@ void *htable_prev_(const struct htable *ht, struct htable_iter *i)
 static void ht_add(struct htable *ht, const void *new, size_t h)
 {
        size_t i;
-       uintptr_t perfect = ht->perfect_bit;
+       uintptr_t perfect = ht_perfect_mask(ht);
 
        i = hash_bucket(ht, h);
 
@@ -223,16 +232,16 @@ static COLD bool double_table(struct htable *ht)
        ht->bits++;
 
        /* If we lost our "perfect bit", get it back now. */
-       if (!ht->perfect_bit && ht->common_mask) {
+       if (ht->perfect_bitnum == 0 && ht->common_mask) {
                for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
                        if (ht->common_mask & ((size_t)1 << i)) {
-                               ht->perfect_bit = (size_t)1 << i;
+                               ht->perfect_bitnum = i + 1;
                                break;
                        }
                }
        }
 
-       if (oldtable != &ht->perfect_bit) {
+       if (oldtable != &ht->common_bits) {
                for (i = 0; i < oldnum; i++) {
                        if (entry_is_valid(e = oldtable[i])) {
                                void *p = get_raw_ptr(ht, e);
@@ -262,7 +271,7 @@ static COLD void rehash_table(struct htable *ht)
                        continue;
                if (e == HTABLE_DELETED)
                        ht->table[h] = 0;
-               else if (!(e & ht->perfect_bit)) {
+               else if (!(e & ht_perfect_mask(ht))) {
                        void *p = get_raw_ptr(ht, e);
                        ht->table[h] = 0;
                        ht_add(ht, p, ht->rehash(p, ht->priv));
@@ -290,7 +299,7 @@ static COLD void update_common(struct htable *ht, const void *p)
 
                ht->common_mask = ~((uintptr_t)1 << i);
                ht->common_bits = ((uintptr_t)p & ht->common_mask);
-               ht->perfect_bit = 1;
+               ht->perfect_bitnum = 1;
                (void)htable_debug(ht, HTABLE_LOC);
                return;
        }
@@ -313,7 +322,8 @@ static COLD void update_common(struct htable *ht, const void *p)
        /* Take away those bits from our mask, bits and perfect bit. */
        ht->common_mask &= ~maskdiff;
        ht->common_bits &= ~maskdiff;
-       ht->perfect_bit &= ~maskdiff;
+       if (ht_perfect_mask(ht) & maskdiff)
+               ht->perfect_bitnum = 0;
        (void)htable_debug(ht, HTABLE_LOC);
 }
 
index bdce920b552eb0e23a5bbe53877a146decf87d81..938b43afc89ce5a6603c45761da17d26b748475e 100644 (file)
 struct htable {
        size_t (*rehash)(const void *elem, void *priv);
        void *priv;
-       unsigned int bits;
+       unsigned int bits, perfect_bitnum;
        size_t elems, deleted;
        /* These are the bits which are the same in all pointers. */
        uintptr_t common_mask, common_bits;
-       uintptr_t perfect_bit;
        uintptr_t *table;
 };
 
@@ -50,7 +49,7 @@ struct htable {
  *     static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
  */
 #define HTABLE_INITIALIZER(name, rehash, priv)                         \
-       { rehash, priv, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
+       { rehash, priv, 0, 0, 0, 0, -1, 0, &name.common_bits }
 
 /**
  * htable_init - initialize an empty hash table.
index ce4c4103646ee3b69dc214205a5b01bebb32c418..b9f48ee70aa96afc0dec95d98d8240b2659bba17 100644 (file)
@@ -194,17 +194,17 @@ int main(void)
        /* Corner cases: wipe out the perfect bit using bogus pointer. */
        htable_clear(&ht);
        htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]);
-       ok1(ht.perfect_bit);
-       perfect_bit = ht.perfect_bit;
+       ok1(ht_perfect_mask(&ht));
+       perfect_bit = ht_perfect_mask(&ht);
        bad_pointer = (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit);
        htable_add(&ht, 0, bad_pointer);
-       ok1(ht.perfect_bit == 0);
+       ok1(ht_perfect_mask(&ht) == 0);
        htable_del(&ht, 0, bad_pointer);
 
        /* Enlarging should restore it... */
        add_vals(&ht, val, 0, NUM_VALS-1);
 
-       ok1(ht.perfect_bit != 0);
+       ok1(ht_perfect_mask(&ht) != 0);
        htable_clear(&ht);
 
        ok1(htable_init_sized(&ht, hash, NULL, 1024));
index 067816a9c51c474aef38d78337f2fab05937a759..85502c4ab1f8d3dbeb6e69cde0ecaa2cb4560b7e 100644 (file)
@@ -183,17 +183,17 @@ int main(void)
        /* Corner cases: wipe out the perfect bit using bogus pointer. */
        htable_clear(&ht);
        htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]));
-       ok1(ht.perfect_bit);
-       perfect_bit = ht.perfect_bit;
+       ok1(ht_perfect_mask(&ht));
+       perfect_bit = ht_perfect_mask(&ht);
        htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]
                                   | perfect_bit));
-       ok1(ht.perfect_bit == 0);
+       ok1(ht_perfect_mask(&ht) == 0);
        htable_del(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit));
 
        /* Enlarging should restore it... */
        add_vals(&ht, val, 0, NUM_VALS-1);
 
-       ok1(ht.perfect_bit != 0);
+       ok1(ht_perfect_mask(&ht) != 0);
        htable_clear(&ht);
 
        ok1(htable_init_sized(&ht, hash, NULL, 1024));
index dce3fdf911430ce66e8e6b16c2604723d9634f89..e185b6f69eb74f3435789d82abe144eb32d8d1ca 100644 (file)
@@ -74,8 +74,8 @@ static size_t perfect(const struct htable *ht)
                        continue;
                if (hash_bucket(ht, ht->rehash(get_raw_ptr(ht, ht->table[i]),
                                               ht->priv)) == i) {
-                       assert((ht->table[i] & ht->perfect_bit)
-                              == ht->perfect_bit);
+                       assert((ht->table[i] & ht_perfect_mask(ht))
+                              == ht_perfect_mask(ht));
                        placed_perfect++;
                }
        }