Commit 7623d082 authored by Rusty Russell's avatar Rusty Russell

htable: avoid branch in calculating perfect bit.

Final results of tools/speed/10000000 (10 runs) shows a slight
slowdown in some tests, but it makes an empty htable smaller.

-Initial delete all: 96-98(96.4+/-0.66) ns
+Initial delete all: 97-99(98.2+/-0.75) ns
-Initial re-inserting: 117-124(121.4+/-1.9) ns
+Initial re-inserting: 124-131(126.4+/-2.4) ns
-Adding (a different) half: 49-50(49.3+/-0.46) ns
+Adding (a different) half: 50-52(51.2+/-0.75) ns
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent d615e534
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
/* We use 0x1 as deleted marker. */ /* We use 0x1 as deleted marker. */
#define HTABLE_DELETED (0x1) #define HTABLE_DELETED (0x1)
/* perfect_bitnum 63 means there's no perfect bitnum */
#define NO_PERFECT_BIT (sizeof(uintptr_t) * CHAR_BIT - 1)
static void *htable_default_alloc(struct htable *ht, size_t len) static void *htable_default_alloc(struct htable *ht, size_t len)
{ {
return calloc(len, 1); return calloc(len, 1);
...@@ -58,13 +61,9 @@ static inline bool entry_is_valid(uintptr_t e) ...@@ -58,13 +61,9 @@ 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) static inline uintptr_t ht_perfect_mask(const struct htable *ht)
{ {
if (ht->perfect_bitnum > 0) return (uintptr_t)2 << ht->perfect_bitnum;
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,
...@@ -232,10 +231,10 @@ static COLD bool double_table(struct htable *ht) ...@@ -232,10 +231,10 @@ 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_bitnum == 0 && ht->common_mask) { if (ht->perfect_bitnum == NO_PERFECT_BIT && 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)2 << i)) {
ht->perfect_bitnum = i + 1; ht->perfect_bitnum = i;
break; break;
} }
} }
...@@ -259,7 +258,7 @@ static COLD bool double_table(struct htable *ht) ...@@ -259,7 +258,7 @@ static COLD bool double_table(struct htable *ht)
static COLD void rehash_table(struct htable *ht) static COLD void rehash_table(struct htable *ht)
{ {
size_t start, i; size_t start, i;
uintptr_t e; uintptr_t e, perfect = ht_perfect_mask(ht);
/* Beware wrap cases: we need to start from first empty bucket. */ /* Beware wrap cases: we need to start from first empty bucket. */
for (start = 0; ht->table[start]; start++); for (start = 0; ht->table[start]; start++);
...@@ -271,7 +270,7 @@ static COLD void rehash_table(struct htable *ht) ...@@ -271,7 +270,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_mask(ht))) { else if (!(e & perfect)) {
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));
...@@ -299,7 +298,7 @@ static COLD void update_common(struct htable *ht, const void *p) ...@@ -299,7 +298,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_bitnum = 1; ht->perfect_bitnum = 0;
(void)htable_debug(ht, HTABLE_LOC); (void)htable_debug(ht, HTABLE_LOC);
return; return;
} }
...@@ -323,7 +322,7 @@ static COLD void update_common(struct htable *ht, const void *p) ...@@ -323,7 +322,7 @@ static COLD void update_common(struct htable *ht, const void *p)
ht->common_mask &= ~maskdiff; ht->common_mask &= ~maskdiff;
ht->common_bits &= ~maskdiff; ht->common_bits &= ~maskdiff;
if (ht_perfect_mask(ht) & maskdiff) if (ht_perfect_mask(ht) & maskdiff)
ht->perfect_bitnum = 0; ht->perfect_bitnum = NO_PERFECT_BIT;
(void)htable_debug(ht, HTABLE_LOC); (void)htable_debug(ht, HTABLE_LOC);
} }
......
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