From 178d58852e5af5614a6537dd16c8b0c44905ed74 Mon Sep 17 00:00:00 2001 From: Tyge Date: Wed, 29 Apr 2020 13:42:18 +0200 Subject: Making code clearer, and update benchmark.c --- benchmark.c | 17 +++++++++-------- stc/cmap.h | 11 +++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/benchmark.c b/benchmark.c index e759884c..9bf3722d 100644 --- a/benchmark.c +++ b/benchmark.c @@ -10,15 +10,15 @@ #include "others/robin_hood.hpp" #endif -// Visual Studio: compile with -TP to force C++ +// Visual Studio: compile with -TP to force C++: cl -TP -EHsc -O2 benchmark.c declare_CMap(ii, int64_t, int64_t, c_noDestroy, c_lowbias32Hash); declare_CVector_string(s); -declare_CMap(ix, short, short); // test: bucket size = 6 bytes only! +declare_CMap(ix, short, short); // sizeof(CMapBucket_ix) = 6 bytes only! const size_t seed = 123; // time(NULL); const double maxLoadFactor = 0.77; -#define RAND() rand() * rand() +#define RAND(N) ((rand() << ((N) - 15)) ^ rand()) // N=16-30 #define CMAP_SETUP(tag, Key, Value) CMap_##tag map = cmap_init; \ cmap_##tag##_setMaxLoadFactor(&map, maxLoadFactor) @@ -61,8 +61,9 @@ const double maxLoadFactor = 0.77; #define RMAP_BUCKETS(tag) map.bucket_count() #define RMAP_CLEAR(tag) map.clear() -const size_t N1 = 7000000; \ -const size_t N2 = 10000000; \ +const size_t N1 = 7000000; +const size_t N2 = 10000000; +#define RR 24 #define MAP_TEST1(M, tag) \ { \ @@ -71,8 +72,8 @@ const size_t N2 = 10000000; \ srand(seed); \ clock_t difference, before = clock(); \ for (size_t i = 0; i < N1; ++i) { \ - checksum += ++M##_PUT(tag, RAND(), i); \ - erased += M##_DEL(tag, RAND()); \ + checksum += ++M##_PUT(tag, RAND(RR), i); \ + erased += M##_DEL(tag, RAND(RR)); \ } \ difference = clock() - before; \ printf(#M "(" #tag "): sz: %llu, bucks: %llu, time: %.02f, sum: %llu, erase: %llu\n", M##_SIZE(tag), M##_BUCKETS(tag), (float) difference / CLOCKS_PER_SEC, checksum, erased); \ @@ -97,7 +98,7 @@ const size_t N2 = 10000000; \ int main() { - printf("\nmap: Insert + remove %llu random keys in range 0 - 2^30:\n", N1); \ + printf("\nmap: Insert + remove %llu random keys in range 0 - 2^%d:\n", N1, RR); \ MAP_TEST1(CMAP, ii) #ifdef __cplusplus MAP_TEST1(UMAP, ii) diff --git a/stc/cmap.h b/stc/cmap.h index 7ef28bec..95b1ee13 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -175,12 +175,15 @@ cmap_##tag##_setShrinkLimitFactor(CMap_##tag* self, double limit) { \ \ static inline size_t \ cmap_##tag##_bucket(CMap_##tag* self, const cmap_##tag##_rawkey_t* rawKeyPtr, uint32_t* hxPtr) { \ - uint32_t hash = keyHashRaw(rawKeyPtr, sizeof(cmap_##tag##_rawkey_t)), hx = (hash & cmapentry_HASH) | cmapentry_USED; \ + uint32_t hash = keyHashRaw(rawKeyPtr, sizeof(cmap_##tag##_rawkey_t)), sx, hx = (hash & cmapentry_HASH) | cmapentry_USED; \ size_t cap = cvector_capacity(self->_table); \ size_t idx = cmap_reduce(hash, cap); \ CMapEntry_##tag* slot = self->_table.data; \ - cmap_##tag##_rawkey_t r; \ - while (slot[idx].hashx && (slot[idx].hashx != hx || !keyEqualsRaw((r = keyGetRaw(&slot[idx].key), &r), rawKeyPtr))) { \ + while ((sx = slot[idx].hashx)) { \ + if (sx == hx) { \ + cmap_##tag##_rawkey_t r = keyGetRaw(&slot[idx].key); \ + if (keyEqualsRaw(&r, rawKeyPtr)) break; \ + } \ if (++idx == cap) idx = 0; \ } \ *hxPtr = hx; \ @@ -275,7 +278,7 @@ cmap_##tag##_erase(CMap_##tag* self, cmap_##tag##_rawkey_t rawKey) { \ return false; \ cmap_##tag##_rawkey_t r; \ do { /* deletion from hash table without tombstone */ \ - if (++j == cap) j = 0; /* j %= cap; is slow */ \ + if (++j == cap) j = 0; /* ++j %= cap; is slow */ \ if (! slot[j].hashx) \ break; \ k = cmap_reduce(keyHashRaw((r = keyGetRaw(&slot[j].key), &r), \ -- cgit v1.2.3