From 49c151bc361c0b868a90796dbb29089a862d9a76 Mon Sep 17 00:00:00 2001 From: Dmitri Tikhonov Date: Mon, 29 May 2023 23:23:54 -0400 Subject: [PATCH 1/2] htable: use precalculated list of primes --- main/htable.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/main/htable.c b/main/htable.c index 8c08ebe93f..483dab19cb 100644 --- a/main/htable.c +++ b/main/htable.c @@ -258,24 +258,25 @@ static void hashTablePutItem00 (hashTable *htable, void *key, void *val htable->count++; } -/* TODO: A pre-calculated array can be used instead of - * finding a new one at runtume. */ +/* "doubling" primes smaller than 2^32 */ +static const unsigned int primes[] = { + 3, 7, 17, 37, 79, 163, 331, 673, 1361, 2729, 5471, 10949, 21911, + 43853, 87719, 175447, 350899, 701819, 1403641, 2807303, 5614657, + 11229331, 22458671, 44917381, 89834777, 179669557, 359339171, + 718678369, 1437356741, 2874713497, +}; + static unsigned int prime_double(unsigned int i) { + unsigned int j; + Assert (i > 2); Assert (i % 2); - for (unsigned int c = 2 * i + 1; ; c += 2) - { - for (unsigned int i0 = 3; i0 < i; i0 += 2) - { - if ((c % i0) == 0) - goto next; - } - return c; - next:; - } + for (j = 0; j < sizeof(primes) / sizeof(primes[0]); ++j) + if (primes[j] > i) + return primes[j]; return i; } From f0bfcb271367446ffab77e5c84208679bb5b1eed Mon Sep 17 00:00:00 2001 From: Dmitri Tikhonov Date: Tue, 30 May 2023 16:13:04 -0400 Subject: [PATCH 2/2] Test htable growth --- extra-cmds/utiltest.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/extra-cmds/utiltest.c b/extra-cmds/utiltest.c index 5feab6cf13..dfba0442b1 100644 --- a/extra-cmds/utiltest.c +++ b/extra-cmds/utiltest.c @@ -215,6 +215,24 @@ static void test_htable_update(void) hashTableDelete(htable); } +static void test_htable_grow(void) +{ + hashTable *htable; + int i; + char keyBuf[20]; + + htable = hashTableNew (3, hashCstrhash, hashCstreq, eFree, NULL); + + for (i = 0; i < 1000; ++i) + { + snprintf(keyBuf, sizeof(keyBuf), "str_%d", i); + hashTablePutItem (htable, strdup(keyBuf), strdup(keyBuf)); + } + + TEST_CHECK (strcmp (hashTableGetItem (htable, "str_123"), "str_123") == 0); + hashTableDelete(htable); +} + static void test_routines_strrstr(void) { TEST_CHECK(strcmp(strrstr("abcdcdb", "cd"), "cdb") == 0); @@ -225,6 +243,7 @@ TEST_LIST = { { "fname/absolute+cache", test_fname_absolute_with_cache }, { "fname/relative", test_fname_relative }, { "htable/update", test_htable_update }, + { "htable/grow", test_htable_grow }, { "routines/strrstr", test_routines_strrstr }, { NULL, NULL } /* zeroed record marking the end of the list */ };