Skip to content

Commit

Permalink
htable: avoid branch in calculating perfect bit.
Browse files Browse the repository at this point in the history
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: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Apr 2, 2019
1 parent d615e53 commit 7623d08
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions ccan/htable/htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
/* We use 0x1 as deleted marker. */
#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)
{
return calloc(len, 1);
Expand Down Expand Up @@ -58,13 +61,9 @@ 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;
return (uintptr_t)2 << ht->perfect_bitnum;
}

static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
Expand Down Expand Up @@ -232,10 +231,10 @@ static COLD bool double_table(struct htable *ht)
ht->bits++;

/* 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++) {
if (ht->common_mask & ((size_t)1 << i)) {
ht->perfect_bitnum = i + 1;
if (ht->common_mask & ((size_t)2 << i)) {
ht->perfect_bitnum = i;
break;
}
}
Expand All @@ -259,7 +258,7 @@ static COLD bool double_table(struct htable *ht)
static COLD void rehash_table(struct htable *ht)
{
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. */
for (start = 0; ht->table[start]; start++);
Expand All @@ -271,7 +270,7 @@ static COLD void rehash_table(struct htable *ht)
continue;
if (e == HTABLE_DELETED)
ht->table[h] = 0;
else if (!(e & ht_perfect_mask(ht))) {
else if (!(e & perfect)) {
void *p = get_raw_ptr(ht, e);
ht->table[h] = 0;
ht_add(ht, p, ht->rehash(p, ht->priv));
Expand Down Expand Up @@ -299,7 +298,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_bitnum = 1;
ht->perfect_bitnum = 0;
(void)htable_debug(ht, HTABLE_LOC);
return;
}
Expand All @@ -323,7 +322,7 @@ static COLD void update_common(struct htable *ht, const void *p)
ht->common_mask &= ~maskdiff;
ht->common_bits &= ~maskdiff;
if (ht_perfect_mask(ht) & maskdiff)
ht->perfect_bitnum = 0;
ht->perfect_bitnum = NO_PERFECT_BIT;
(void)htable_debug(ht, HTABLE_LOC);
}

Expand Down

0 comments on commit 7623d08

Please sign in to comment.