Commit d615e534 authored by Rusty Russell's avatar Rusty Russell

htable: reduce size of htable by storing perfect bitnum, not mask.

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 9e92552b
...@@ -58,6 +58,15 @@ static inline bool entry_is_valid(uintptr_t e) ...@@ -58,6 +58,15 @@ static inline bool entry_is_valid(uintptr_t e)
return e > HTABLE_DELETED; 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, static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
size_t hash) size_t hash)
{ {
...@@ -65,7 +74,7 @@ static inline uintptr_t get_hash_ptr_bits(const struct htable *ht, ...@@ -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 * end is quite expensive. But the lower bits are redundant, so
* we fold the value first. */ * we fold the value first. */
return (hash ^ (hash >> ht->bits)) return (hash ^ (hash >> ht->bits))
& ht->common_mask & ~ht->perfect_bit; & ht->common_mask & ~ht_perfect_mask(ht);
} }
void htable_init(struct htable *ht, void htable_init(struct htable *ht,
...@@ -75,7 +84,7 @@ void htable_init(struct htable *ht, ...@@ -75,7 +84,7 @@ void htable_init(struct htable *ht,
*ht = empty; *ht = empty;
ht->rehash = rehash; ht->rehash = rehash;
ht->priv = priv; ht->priv = priv;
ht->table = &ht->perfect_bit; ht->table = &ht->common_bits;
} }
static inline size_t ht_max(const struct htable *ht) static inline size_t ht_max(const struct htable *ht)
...@@ -102,7 +111,7 @@ bool htable_init_sized(struct htable *ht, ...@@ -102,7 +111,7 @@ bool htable_init_sized(struct htable *ht,
ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits); ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
if (!ht->table) { if (!ht->table) {
ht->table = &ht->perfect_bit; ht->table = &ht->common_bits;
return false; return false;
} }
(void)htable_debug(ht, HTABLE_LOC); (void)htable_debug(ht, HTABLE_LOC);
...@@ -111,7 +120,7 @@ bool htable_init_sized(struct htable *ht, ...@@ -111,7 +120,7 @@ bool htable_init_sized(struct htable *ht,
void htable_clear(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_free(ht, (void *)ht->table);
htable_init(ht, ht->rehash, ht->priv); htable_init(ht, ht->rehash, ht->priv);
} }
...@@ -154,7 +163,7 @@ void *htable_firstval_(const struct htable *ht, ...@@ -154,7 +163,7 @@ void *htable_firstval_(const struct htable *ht,
struct htable_iter *i, size_t hash) struct htable_iter *i, size_t hash)
{ {
i->off = hash_bucket(ht, 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, void *htable_nextval_(const struct htable *ht,
...@@ -197,7 +206,7 @@ void *htable_prev_(const struct htable *ht, struct htable_iter *i) ...@@ -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) static void ht_add(struct htable *ht, const void *new, size_t h)
{ {
size_t i; size_t i;
uintptr_t perfect = ht->perfect_bit; uintptr_t perfect = ht_perfect_mask(ht);
i = hash_bucket(ht, h); i = hash_bucket(ht, h);
...@@ -223,16 +232,16 @@ static COLD bool double_table(struct htable *ht) ...@@ -223,16 +232,16 @@ static COLD bool double_table(struct htable *ht)
ht->bits++; ht->bits++;
/* If we lost our "perfect bit", get it back now. */ /* 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++) { for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
if (ht->common_mask & ((size_t)1 << i)) { if (ht->common_mask & ((size_t)1 << i)) {
ht->perfect_bit = (size_t)1 << i; ht->perfect_bitnum = i + 1;
break; break;
} }
} }
} }
if (oldtable != &ht->perfect_bit) { if (oldtable != &ht->common_bits) {
for (i = 0; i < oldnum; i++) { for (i = 0; i < oldnum; i++) {
if (entry_is_valid(e = oldtable[i])) { if (entry_is_valid(e = oldtable[i])) {
void *p = get_raw_ptr(ht, e); void *p = get_raw_ptr(ht, e);
...@@ -262,7 +271,7 @@ static COLD void rehash_table(struct htable *ht) ...@@ -262,7 +271,7 @@ static COLD void rehash_table(struct htable *ht)
continue; continue;
if (e == HTABLE_DELETED) if (e == HTABLE_DELETED)
ht->table[h] = 0; ht->table[h] = 0;
else if (!(e & ht->perfect_bit)) { else if (!(e & ht_perfect_mask(ht))) {
void *p = get_raw_ptr(ht, e); void *p = get_raw_ptr(ht, e);
ht->table[h] = 0; ht->table[h] = 0;
ht_add(ht, p, ht->rehash(p, ht->priv)); ht_add(ht, p, ht->rehash(p, ht->priv));
...@@ -290,7 +299,7 @@ static COLD void update_common(struct htable *ht, const void *p) ...@@ -290,7 +299,7 @@ static COLD void update_common(struct htable *ht, const void *p)
ht->common_mask = ~((uintptr_t)1 << i); ht->common_mask = ~((uintptr_t)1 << i);
ht->common_bits = ((uintptr_t)p & ht->common_mask); ht->common_bits = ((uintptr_t)p & ht->common_mask);
ht->perfect_bit = 1; ht->perfect_bitnum = 1;
(void)htable_debug(ht, HTABLE_LOC); (void)htable_debug(ht, HTABLE_LOC);
return; return;
} }
...@@ -313,7 +322,8 @@ static COLD void update_common(struct htable *ht, const void *p) ...@@ -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. */ /* Take away those bits from our mask, bits and perfect bit. */
ht->common_mask &= ~maskdiff; ht->common_mask &= ~maskdiff;
ht->common_bits &= ~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); (void)htable_debug(ht, HTABLE_LOC);
} }
......
...@@ -24,11 +24,10 @@ ...@@ -24,11 +24,10 @@
struct htable { struct htable {
size_t (*rehash)(const void *elem, void *priv); size_t (*rehash)(const void *elem, void *priv);
void *priv; void *priv;
unsigned int bits; unsigned int bits, perfect_bitnum;
size_t elems, deleted; size_t elems, deleted;
/* These are the bits which are the same in all pointers. */ /* These are the bits which are the same in all pointers. */
uintptr_t common_mask, common_bits; uintptr_t common_mask, common_bits;
uintptr_t perfect_bit;
uintptr_t *table; uintptr_t *table;
}; };
...@@ -50,7 +49,7 @@ struct htable { ...@@ -50,7 +49,7 @@ struct htable {
* static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL); * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
*/ */
#define HTABLE_INITIALIZER(name, rehash, priv) \ #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. * htable_init - initialize an empty hash table.
......
...@@ -194,17 +194,17 @@ int main(void) ...@@ -194,17 +194,17 @@ int main(void)
/* Corner cases: wipe out the perfect bit using bogus pointer. */ /* Corner cases: wipe out the perfect bit using bogus pointer. */
htable_clear(&ht); htable_clear(&ht);
htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]); htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]);
ok1(ht.perfect_bit); ok1(ht_perfect_mask(&ht));
perfect_bit = ht.perfect_bit; perfect_bit = ht_perfect_mask(&ht);
bad_pointer = (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit); bad_pointer = (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit);
htable_add(&ht, 0, bad_pointer); htable_add(&ht, 0, bad_pointer);
ok1(ht.perfect_bit == 0); ok1(ht_perfect_mask(&ht) == 0);
htable_del(&ht, 0, bad_pointer); htable_del(&ht, 0, bad_pointer);
/* Enlarging should restore it... */ /* Enlarging should restore it... */
add_vals(&ht, val, 0, NUM_VALS-1); add_vals(&ht, val, 0, NUM_VALS-1);
ok1(ht.perfect_bit != 0); ok1(ht_perfect_mask(&ht) != 0);
htable_clear(&ht); htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1024)); ok1(htable_init_sized(&ht, hash, NULL, 1024));
......
...@@ -183,17 +183,17 @@ int main(void) ...@@ -183,17 +183,17 @@ int main(void)
/* Corner cases: wipe out the perfect bit using bogus pointer. */ /* Corner cases: wipe out the perfect bit using bogus pointer. */
htable_clear(&ht); htable_clear(&ht);
htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1])); htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]));
ok1(ht.perfect_bit); ok1(ht_perfect_mask(&ht));
perfect_bit = ht.perfect_bit; perfect_bit = ht_perfect_mask(&ht);
htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1]
| perfect_bit)); | 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)); htable_del(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit));
/* Enlarging should restore it... */ /* Enlarging should restore it... */
add_vals(&ht, val, 0, NUM_VALS-1); add_vals(&ht, val, 0, NUM_VALS-1);
ok1(ht.perfect_bit != 0); ok1(ht_perfect_mask(&ht) != 0);
htable_clear(&ht); htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1024)); ok1(htable_init_sized(&ht, hash, NULL, 1024));
......
...@@ -74,8 +74,8 @@ static size_t perfect(const struct htable *ht) ...@@ -74,8 +74,8 @@ static size_t perfect(const struct htable *ht)
continue; continue;
if (hash_bucket(ht, ht->rehash(get_raw_ptr(ht, ht->table[i]), if (hash_bucket(ht, ht->rehash(get_raw_ptr(ht, ht->table[i]),
ht->priv)) == i) { ht->priv)) == i) {
assert((ht->table[i] & ht->perfect_bit) assert((ht->table[i] & ht_perfect_mask(ht))
== ht->perfect_bit); == ht_perfect_mask(ht));
placed_perfect++; placed_perfect++;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment