From 9e92552b1b2a1b631bde1c379b9f2950725b1245 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 2 Apr 2019 09:50:37 +1030 Subject: [PATCH] htable: reduce size of htable by calculating max every time. Signed-off-by: Rusty Russell --- ccan/htable/htable.c | 17 +++++++++-------- ccan/htable/htable.h | 4 ++-- ccan/htable/test/run-copy.c | 4 ++-- ccan/htable/test/run-debug.c | 12 ++++++------ ccan/htable/test/run-size.c | 4 ++-- ccan/htable/test/run-type-int.c | 4 ++-- ccan/htable/test/run-type.c | 4 ++-- ccan/htable/test/run.c | 12 ++++++------ 8 files changed, 31 insertions(+), 30 deletions(-) diff --git a/ccan/htable/htable.c b/ccan/htable/htable.c index e6dc4fd8..2028d336 100644 --- a/ccan/htable/htable.c +++ b/ccan/htable/htable.c @@ -78,11 +78,14 @@ void htable_init(struct htable *ht, ht->table = &ht->perfect_bit; } -/* We've changed ht->bits, update ht->max and ht->max_with_deleted */ -static void htable_adjust_capacity(struct htable *ht) +static inline size_t ht_max(const struct htable *ht) { - ht->max = ((size_t)3 << ht->bits) / 4; - ht->max_with_deleted = ((size_t)9 << ht->bits) / 10; + return ((size_t)3 << ht->bits) / 4; +} + +static inline size_t ht_max_with_deleted(const struct htable *ht) +{ + return ((size_t)9 << ht->bits) / 10; } bool htable_init_sized(struct htable *ht, @@ -102,7 +105,6 @@ bool htable_init_sized(struct htable *ht, ht->table = &ht->perfect_bit; return false; } - htable_adjust_capacity(ht); (void)htable_debug(ht, HTABLE_LOC); return true; } @@ -219,7 +221,6 @@ static COLD bool double_table(struct htable *ht) return false; } ht->bits++; - htable_adjust_capacity(ht); /* If we lost our "perfect bit", get it back now. */ if (!ht->perfect_bit && ht->common_mask) { @@ -318,9 +319,9 @@ static COLD void update_common(struct htable *ht, const void *p) bool htable_add_(struct htable *ht, size_t hash, const void *p) { - if (ht->elems+1 > ht->max && !double_table(ht)) + if (ht->elems+1 > ht_max(ht) && !double_table(ht)) return false; - if (ht->elems+1 + ht->deleted > ht->max_with_deleted) + if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht)) rehash_table(ht); assert(p); if (((uintptr_t)p & ht->common_mask) != ht->common_bits) diff --git a/ccan/htable/htable.h b/ccan/htable/htable.h index 28755d61..bdce920b 100644 --- a/ccan/htable/htable.h +++ b/ccan/htable/htable.h @@ -25,7 +25,7 @@ struct htable { size_t (*rehash)(const void *elem, void *priv); void *priv; unsigned int bits; - size_t elems, deleted, max, max_with_deleted; + 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; @@ -50,7 +50,7 @@ struct htable { * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL); */ #define HTABLE_INITIALIZER(name, rehash, priv) \ - { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit } + { rehash, priv, 0, 0, 0, -1, 0, 0, &name.perfect_bit } /** * htable_init - initialize an empty hash table. diff --git a/ccan/htable/test/run-copy.c b/ccan/htable/test/run-copy.c index d111495a..f8a35006 100644 --- a/ccan/htable/test/run-copy.c +++ b/ccan/htable/test/run-copy.c @@ -28,8 +28,8 @@ int main(void) htable_init(&ht, hash, NULL); for (i = 0; i < NUM_VALS; i++) { - ok1(ht.max >= i); - ok1(ht.max <= i * 2); + ok1(ht_max(&ht) >= i); + ok1(ht_max(&ht) <= i * 2); htable_add(&ht, hash(&val[i], NULL), &val[i]); } diff --git a/ccan/htable/test/run-debug.c b/ccan/htable/test/run-debug.c index d2d6b0ff..ce4c4103 100644 --- a/ccan/htable/test/run-debug.c +++ b/ccan/htable/test/run-debug.c @@ -121,7 +121,7 @@ int main(void) dne = i; htable_init(&ht, hash, NULL); - ok1(ht.max == 0); + ok1(ht_max(&ht) == 0); ok1(ht.bits == 0); /* We cannot find an entry which doesn't exist. */ @@ -130,7 +130,7 @@ int main(void) /* This should increase it once. */ add_vals(&ht, val, 0, 1); ok1(ht.bits == 1); - ok1(ht.max == 1); + ok1(ht_max(&ht) == 1); weight = 0; for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) { if (ht.common_mask & ((uintptr_t)1 << i)) { @@ -146,7 +146,7 @@ int main(void) /* This should increase it again. */ add_vals(&ht, val, 1, 1); ok1(ht.bits == 2); - ok1(ht.max == 3); + ok1(ht_max(&ht) == 3); /* Mask should be set. */ ok1(ht.common_mask != 0); @@ -208,15 +208,15 @@ int main(void) htable_clear(&ht); ok1(htable_init_sized(&ht, hash, NULL, 1024)); - ok1(ht.max >= 1024); + ok1(ht_max(&ht) >= 1024); htable_clear(&ht); ok1(htable_init_sized(&ht, hash, NULL, 1023)); - ok1(ht.max >= 1023); + ok1(ht_max(&ht) >= 1023); htable_clear(&ht); ok1(htable_init_sized(&ht, hash, NULL, 1025)); - ok1(ht.max >= 1025); + ok1(ht_max(&ht) >= 1025); htable_clear(&ht); return exit_status(); diff --git a/ccan/htable/test/run-size.c b/ccan/htable/test/run-size.c index 1a2f5cdd..090ff996 100644 --- a/ccan/htable/test/run-size.c +++ b/ccan/htable/test/run-size.c @@ -26,8 +26,8 @@ int main(void) htable_init(&ht, hash, NULL); for (i = 0; i < NUM_VALS; i++) { - ok1(ht.max >= i); - ok1(ht.max <= i * 2); + ok1(ht_max(&ht) >= i); + ok1(ht_max(&ht) <= i * 2); htable_add(&ht, hash(&val[i], NULL), &val[i]); } htable_clear(&ht); diff --git a/ccan/htable/test/run-type-int.c b/ccan/htable/test/run-type-int.c index 7b71815f..16c4f56e 100644 --- a/ccan/htable/test/run-type-int.c +++ b/ccan/htable/test/run-type-int.c @@ -127,7 +127,7 @@ int main(void) dne = i; htable_obj_init(&ht); - ok1(ht.raw.max == 0); + ok1(ht_max(&ht.raw) == 0); ok1(ht.raw.bits == 0); /* We cannot find an entry which doesn't exist. */ @@ -136,7 +136,7 @@ int main(void) /* Fill it, it should increase in size. */ add_vals(&ht, val, NUM_VALS); ok1(ht.raw.bits == NUM_BITS + 1); - ok1(ht.raw.max < (1 << ht.raw.bits)); + ok1(ht_max(&ht.raw) < (1 << ht.raw.bits)); /* Mask should be set. */ ok1(ht.raw.common_mask != 0); diff --git a/ccan/htable/test/run-type.c b/ccan/htable/test/run-type.c index a3616a5a..f097acb6 100644 --- a/ccan/htable/test/run-type.c +++ b/ccan/htable/test/run-type.c @@ -122,7 +122,7 @@ int main(void) dne = i; htable_obj_init(&ht); - ok1(ht.raw.max == 0); + ok1(ht_max(&ht.raw) == 0); ok1(ht.raw.bits == 0); /* We cannot find an entry which doesn't exist. */ @@ -131,7 +131,7 @@ int main(void) /* Fill it, it should increase in size. */ add_vals(&ht, val, NUM_VALS); ok1(ht.raw.bits == NUM_BITS + 1); - ok1(ht.raw.max < (1 << ht.raw.bits)); + ok1(ht_max(&ht.raw) < (1 << ht.raw.bits)); /* Mask should be set. */ ok1(ht.raw.common_mask != 0); diff --git a/ccan/htable/test/run.c b/ccan/htable/test/run.c index 46514c72..067816a9 100644 --- a/ccan/htable/test/run.c +++ b/ccan/htable/test/run.c @@ -111,7 +111,7 @@ int main(void) dne = i; htable_init(&ht, hash, NULL); - ok1(ht.max == 0); + ok1(ht_max(&ht) == 0); ok1(ht.bits == 0); /* We cannot find an entry which doesn't exist. */ @@ -120,7 +120,7 @@ int main(void) /* This should increase it once. */ add_vals(&ht, val, 0, 1); ok1(ht.bits == 1); - ok1(ht.max == 1); + ok1(ht_max(&ht) == 1); weight = 0; for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) { if (ht.common_mask & ((uintptr_t)1 << i)) { @@ -136,7 +136,7 @@ int main(void) /* This should increase it again. */ add_vals(&ht, val, 1, 1); ok1(ht.bits == 2); - ok1(ht.max == 3); + ok1(ht_max(&ht) == 3); /* Mask should be set. */ ok1(ht.common_mask != 0); @@ -197,15 +197,15 @@ int main(void) htable_clear(&ht); ok1(htable_init_sized(&ht, hash, NULL, 1024)); - ok1(ht.max >= 1024); + ok1(ht_max(&ht) >= 1024); htable_clear(&ht); ok1(htable_init_sized(&ht, hash, NULL, 1023)); - ok1(ht.max >= 1023); + ok1(ht_max(&ht) >= 1023); htable_clear(&ht); ok1(htable_init_sized(&ht, hash, NULL, 1025)); - ok1(ht.max >= 1025); + ok1(ht_max(&ht) >= 1025); htable_clear(&ht); return exit_status(); -- 2.39.2