Skip to content

Commit

Permalink
Updated shootout_hashmaps.cpp and small optimization in hmap.h
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed Jun 5, 2024
1 parent 1ead0cb commit 92c8227
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/stc/hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ STC_DEF _m_result
_c_MEMB(_bucket_)(const i_type* self, const _m_keyraw* rkeyptr) {
const uint64_t _hash = i_hash(rkeyptr);
size_t _mask = (size_t)self->bucket_count - 1, _idx = _hash & _mask;
_m_result _res = {.inserted=true, .hashx=(uint8_t)(_hash | 0x80)};
_m_result _res = {.inserted=true, .hashx=(uint8_t)((_hash >> 24) | 0x1)};
const struct hmap_slot* s = self->slot;
while (s[_idx].hashx) {
if (s[_idx].hashx == _res.hashx) {
Expand Down
45 changes: 33 additions & 12 deletions misc/benchmarks/shootout_hashmaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ KHASH_MAP_INIT_INT64(ii, IValue)
//#include "stc/hmap-robin.h"
#include "stc/hmap.h"

#define i_TYPE ivec,IKey
#include "stc/stack.h"

// Verstable map
#define NAME vmap_ii
#define KEY_TY IKey
Expand Down Expand Up @@ -225,33 +228,51 @@ KHASH_MAP_INIT_INT64(ii, IValue)
M##_DTOR(X); \
}

void fisher_yates_shuffle(IKey a[], size_t n) {
--n;
for (size_t i = 0; i < n; i++) {
size_t j = i + (crand() % (n - i)); // swap element with random later element
IKey t = a[i];
a[i] = a[j];
a[j] = t;
}
}

#define MAP_TEST3(M, X, n) \
{ /* Erase elements */ \
M##_SETUP(X, IKey, IValue); \
size_t erased = 0, _n = (n)*2; \
size_t erased = 0, _n = n; \
clock_t difference, before; \
SEED(seed); \
for (size_t i = 0; i < _n; ++i) \
M##_GET_OR_INSERT(X, RAND(keybits), i); \
ivec vec = {0}; \
for (size_t i = 0; i < _n; ++i) { \
IKey k = RAND(keybits); \
ivec_push(&vec, k); \
M##_GET_OR_INSERT(X, k, i); \
} \
fisher_yates_shuffle(vec.data, ivec_size(&vec)); \
SEED(seed); \
before = clock(); \
for (size_t i = 0; i < _n; ++i) \
erased += M##_ERASE(X, RAND(keybits)); \
c_foreach (i, ivec, vec) \
erased += M##_ERASE(X, *i.ref); \
/*for (size_t i = 0; i < _n; ++i) \
erased += M##_ERASE(X, RAND(keybits));*/ \
difference = clock() - before; \
printf(#M ": %5.03f s, size: %" c_ZU ", buckets: %8" c_ZU ", erased %" c_ZU "\n", \
(float) difference / CLOCKS_PER_SEC, (size_t) M##_SIZE(X), (size_t) M##_BUCKETS(X), erased); \
ivec_drop(&vec); \
M##_DTOR(X); \
}

#define MAP_TEST4(M, X, n) \
{ /* Iterate */ \
M##_SETUP(X, IKey, IValue); \
size_t sum = 0, m = 1ull << (keybits + 1), _n = n; \
size_t sum = 0, m = 1ull << keybits, _n = n; \
if (_n < m) m = _n; \
SEED(seed); \
for (size_t i = 0; i < m; ++i) \
for (size_t i = 0; i < n; ++i) \
M##_GET_OR_INSERT(X, RAND(keybits), i); \
size_t rep = 60000000ull/M##_SIZE(X); \
size_t rep = 80000000ull/M##_SIZE(X); \
clock_t difference, before = clock(); \
for (size_t k=0; k < rep; k++) M##_FOR (X, it) \
sum += M##_ITEM(X, it); \
Expand Down Expand Up @@ -347,12 +368,12 @@ int main(int argc, char* argv[])
//printf("\nT2: Insert %g mill. SEQUENTIAL keys, erase them in same order:\n", N2/1000000.0);
//RUN_TEST(2)

printf("\nT3: Erase all elements by lookup (%u mill. random inserts), key range [0, 2^%u)\n", n_mill*2, keybits);
printf("\nT3: Erase all elements by lookup (%u mill. random inserts), key range [0, 2^%u)\n", n_mill, keybits);
RUN_TEST(3)

//printf("\nT4: Iterate map with Min(%u mill, 2^%u) inserts repeated times:\n", n_mill, keybits+1);
//RUN_TEST(4)
printf("\nT4: Iterate map with Min(%u mill, 2^%u) inserts repeated times:\n", n_mill, keybits);
RUN_TEST(4)

printf("\nT5: Lookup mix of random/existing keys in range [0, 2^%u). Num lookups depends on size.\n", keybits);
printf("\nT5: Lookup mix of random/existing keys in range [0, 2^%u). Num lookups adjusted for size.\n", keybits);
RUN_TEST(5)
}

0 comments on commit 92c8227

Please sign in to comment.