diff options
| author | gingerBill <bill@gingerbill.org> | 2021-11-05 12:42:19 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-11-05 12:42:19 +0000 |
| commit | 439fc86740a3a170b1ea5d2ce4816dcba03b21ad (patch) | |
| tree | f9da7d3ca190ee29204a2d1c65ee78ccdefdc602 /src | |
| parent | 0010e882a771fc834ea7902786f4b26e8860915c (diff) | |
Improve performance of the compiler hash table types and unify behaviour
Diffstat (limited to 'src')
| -rw-r--r-- | src/map.cpp | 100 | ||||
| -rw-r--r-- | src/ptr_set.cpp | 73 | ||||
| -rw-r--r-- | src/string_map.cpp | 94 |
3 files changed, 151 insertions, 116 deletions
diff --git a/src/map.cpp b/src/map.cpp index 3a34764bf..86def4f1b 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -7,12 +7,17 @@ #ifndef MAP_UTIL_STUFF #define MAP_UTIL_STUFF // NOTE(bill): This util stuff is the same for every `Map` + +typedef isize MapIndex; + struct MapFindResult { - isize hash_index; - isize entry_prev; - isize entry_index; + MapIndex hash_index; + MapIndex entry_prev; + MapIndex entry_index; }; +enum : MapIndex { MAP_SENTINEL = ~(MapIndex)0 }; + struct HashKey { u64 key; @@ -73,6 +78,7 @@ template <typename T> void map_remove (Map<T> *h, HashKey const &key); template <typename T> void map_clear (Map<T> *h); template <typename T> void map_grow (Map<T> *h); template <typename T> void map_rehash (Map<T> *h, isize new_count); +template <typename T> void map_reserve (Map<T> *h, isize cap); #if MAP_ENABLE_MULTI_MAP // Mutlivalued map procedure @@ -92,7 +98,7 @@ gb_inline void map_init(Map<T> *h, gbAllocator a, isize capacity) { slice_init(&h->hashes, a, capacity); array_init(&h->entries, a, 0, capacity); for (isize i = 0; i < capacity; i++) { - h->hashes.data[i] = -1; + h->hashes.data[i] = MAP_SENTINEL; } } @@ -106,20 +112,20 @@ template <typename T> gb_internal isize map__add_entry(Map<T> *h, HashKey const &key) { MapEntry<T> e = {}; e.key = key; - e.next = -1; + e.next = MAP_SENTINEL; array_add(&h->entries, e); return h->entries.count-1; } template <typename T> gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) { - MapFindResult fr = {-1, -1, -1}; + MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; if (h->hashes.count == 0) { return fr; } fr.hash_index = key.key & (h->hashes.count-1); fr.entry_index = h->hashes.data[fr.hash_index]; - while (fr.entry_index >= 0) { + while (fr.entry_index != MAP_SENTINEL) { if (hash_key_equal(h->entries.data[fr.entry_index].key, key)) { return fr; } @@ -131,13 +137,13 @@ gb_internal MapFindResult map__find(Map<T> *h, HashKey const &key) { template <typename T> gb_internal MapFindResult map__find_from_entry(Map<T> *h, MapEntry<T> *e) { - MapFindResult fr = {-1, -1, -1}; + MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; if (h->hashes.count == 0) { return fr; } fr.hash_index = e->key.key & (h->hashes.count-1); fr.entry_index = h->hashes.data[fr.hash_index]; - while (fr.entry_index >= 0) { + while (fr.entry_index != MAP_SENTINEL) { if (&h->entries.data[fr.entry_index] == e) { return fr; } @@ -159,44 +165,44 @@ gb_inline void map_grow(Map<T> *h) { } template <typename T> -void map_rehash(Map<T> *h, isize new_count) { - isize i, j; - Map<T> nh = {}; - new_count = next_pow2_isize(new_count); - nh.hashes = h->hashes; - nh.entries.allocator = h->entries.allocator; - slice_resize(&nh.hashes, h->entries.allocator, new_count); - for (i = 0; i < new_count; i++) { - nh.hashes.data[i] = -1; +void map_reset_entries(Map<T> *h) { + isize i; + for (i = 0; i < h->hashes.count; i++) { + h->hashes.data[i] = MAP_SENTINEL; } - array_reserve(&nh.entries, ARRAY_GROW_FORMULA(h->entries.count)); for (i = 0; i < h->entries.count; i++) { - MapEntry<T> *e = &h->entries.data[i]; MapFindResult fr; - if (nh.hashes.count == 0) { - map_grow(&nh); - } - fr = map__find(&nh, e->key); - j = map__add_entry(&nh, e->key); - if (fr.entry_prev < 0) { - nh.hashes.data[fr.hash_index] = j; + MapEntry<T> *e = &h->entries.data[i]; + e->next = MAP_SENTINEL; + fr = map__find_from_entry(h, e); + if (fr.entry_prev == MAP_SENTINEL) { + h->hashes[fr.hash_index] = i; } else { - nh.entries.data[fr.entry_prev].next = j; - } - nh.entries.data[j].next = fr.entry_index; - nh.entries.data[j].value = e->value; - if (map__full(&nh)) { - map_grow(&nh); + h->entries[fr.entry_prev].next = i; } } - array_free(&h->entries); - *h = nh; +} + +template <typename T> +void map_reserve(Map<T> *h, isize cap) { + array_reserve(&h->entries, cap); + if (h->entries.count*2 < h->hashes.count) { + return; + } + slice_resize(&h->hashes, h->entries.allocator, cap*2); + map_reset_entries(h); +} + + +template <typename T> +void map_rehash(Map<T> *h, isize new_count) { + map_reserve(h, new_count); } template <typename T> T *map_get(Map<T> *h, HashKey const &key) { isize index = map__find(h, key).entry_index; - if (index >= 0) { + if (index != MAP_SENTINEL) { return &h->entries.data[index].value; } return nullptr; @@ -205,7 +211,7 @@ T *map_get(Map<T> *h, HashKey const &key) { template <typename T> T &map_must_get(Map<T> *h, HashKey const &key) { isize index = map__find(h, key).entry_index; - GB_ASSERT(index >= 0); + GB_ASSERT(index != MAP_SENTINEL); return h->entries.data[index].value; } @@ -217,11 +223,11 @@ void map_set(Map<T> *h, HashKey const &key, T const &value) { map_grow(h); } fr = map__find(h, key); - if (fr.entry_index >= 0) { + if (fr.entry_index != MAP_SENTINEL) { index = fr.entry_index; } else { index = map__add_entry(h, key); - if (fr.entry_prev >= 0) { + if (fr.entry_prev != MAP_SENTINEL) { h->entries.data[fr.entry_prev].next = index; } else { h->hashes.data[fr.hash_index] = index; @@ -238,7 +244,7 @@ void map_set(Map<T> *h, HashKey const &key, T const &value) { template <typename T> void map__erase(Map<T> *h, MapFindResult const &fr) { MapFindResult last; - if (fr.entry_prev < 0) { + if (fr.entry_prev == MAP_SENTINEL) { h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; } else { h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; @@ -251,7 +257,7 @@ void map__erase(Map<T> *h, MapFindResult const &fr) { array_pop(&h->entries); last = map__find(h, h->entries.data[fr.entry_index].key); - if (last.entry_prev >= 0) { + if (last.entry_prev != MAP_SENTINEL) { h->entries.data[last.entry_prev].next = fr.entry_index; } else { h->hashes.data[last.hash_index] = fr.entry_index; @@ -261,7 +267,7 @@ void map__erase(Map<T> *h, MapFindResult const &fr) { template <typename T> void map_remove(Map<T> *h, HashKey const &key) { MapFindResult fr = map__find(h, key); - if (fr.entry_index >= 0) { + if (fr.entry_index != MAP_SENTINEL) { map__erase(h, fr); } } @@ -270,7 +276,7 @@ template <typename T> gb_inline void map_clear(Map<T> *h) { array_clear(&h->entries); for (isize i = 0; i < h->hashes.count; i++) { - h->hashes.data[i] = -1; + h->hashes.data[i] = MAP_SENTINEL; } } @@ -279,7 +285,7 @@ gb_inline void map_clear(Map<T> *h) { template <typename T> MapEntry<T> *multi_map_find_first(Map<T> *h, HashKey const &key) { isize i = map__find(h, key).entry_index; - if (i < 0) { + if (i == MAP_SENTINEL) { return nullptr; } return &h->entries.data[i]; @@ -288,7 +294,7 @@ MapEntry<T> *multi_map_find_first(Map<T> *h, HashKey const &key) { template <typename T> MapEntry<T> *multi_map_find_next(Map<T> *h, MapEntry<T> *e) { isize i = e->next; - while (i >= 0) { + while (i != MAP_SENTINEL) { if (hash_key_equal(h->entries.data[i].key, e->key)) { return &h->entries.data[i]; } @@ -328,7 +334,7 @@ void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) { // Make fr = map__find(h, key); i = map__add_entry(h, key); - if (fr.entry_prev < 0) { + if (fr.entry_prev == MAP_SENTINEL) { h->hashes.data[fr.hash_index] = i; } else { h->entries.data[fr.entry_prev].next = i; @@ -344,7 +350,7 @@ void multi_map_insert(Map<T> *h, HashKey const &key, T const &value) { template <typename T> void multi_map_remove(Map<T> *h, HashKey const &key, MapEntry<T> *e) { MapFindResult fr = map__find_from_entry(h, e); - if (fr.entry_index >= 0) { + if (fr.entry_index != MAP_SENTINEL) { map__erase(h, fr); } } diff --git a/src/ptr_set.cpp b/src/ptr_set.cpp index 9a9f6d252..a17ab7e1c 100644 --- a/src/ptr_set.cpp +++ b/src/ptr_set.cpp @@ -30,6 +30,7 @@ template <typename T> void ptr_set_remove (PtrSet<T> *s, T ptr); template <typename T> void ptr_set_clear (PtrSet<T> *s); template <typename T> void ptr_set_grow (PtrSet<T> *s); template <typename T> void ptr_set_rehash (PtrSet<T> *s, isize new_count); +template <typename T> void ptr_set_reserve(PtrSet<T> *h, isize cap); template <typename T> @@ -79,6 +80,25 @@ gb_internal PtrSetFindResult ptr_set__find(PtrSet<T> *s, T ptr) { } template <typename T> +gb_internal PtrSetFindResult ptr_set__find_from_entry(PtrSet<T> *s, PtrSetEntry<T> *e) { + PtrSetFindResult fr = {PTR_SET_SENTINEL, PTR_SET_SENTINEL, PTR_SET_SENTINEL}; + if (s->hashes.count != 0) { + u64 hash = 0xcbf29ce484222325ull ^ cast(u64)cast(uintptr)e->ptr; + u64 n = cast(u64)s->hashes.count; + fr.hash_index = cast(PtrSetIndex)(hash & (n-1)); + fr.entry_index = s->hashes.data[fr.hash_index]; + while (fr.entry_index != PTR_SET_SENTINEL) { + if (&s->entries.data[fr.entry_index] == e) { + return fr; + } + fr.entry_prev = fr.entry_index; + fr.entry_index = s->entries.data[fr.entry_index].next; + } + } + return fr; +} + +template <typename T> gb_internal bool ptr_set__full(PtrSet<T> *s) { return 0.75f * s->hashes.count <= s->entries.count; } @@ -90,37 +110,38 @@ gb_inline void ptr_set_grow(PtrSet<T> *s) { } template <typename T> -void ptr_set_rehash(PtrSet<T> *s, isize new_count) { - isize i, j; - PtrSet<T> ns = {}; - new_count = next_pow2_isize(new_count); - ns.hashes = s->hashes; - ns.entries.allocator = s->entries.allocator; - slice_resize(&ns.hashes, s->entries.allocator, new_count); - for (i = 0; i < new_count; i++) { - ns.hashes.data[i] = PTR_SET_SENTINEL; - } - array_reserve(&ns.entries, ARRAY_GROW_FORMULA(s->entries.count)); - for (i = 0; i < s->entries.count; i++) { - PtrSetEntry<T> *e = &s->entries.data[i]; +void ptr_set_reset_entries(PtrSet<T> *s) { + PtrSetIndex i; + for (i = 0; i < cast(PtrSetIndex)s->hashes.count; i++) { + s->hashes.data[i] = PTR_SET_SENTINEL; + } + for (i = 0; i < cast(PtrSetIndex)s->entries.count; i++) { PtrSetFindResult fr; - if (ns.hashes.count == 0) { - ptr_set_grow(&ns); - } - fr = ptr_set__find(&ns, e->ptr); - j = ptr_set__add_entry(&ns, e->ptr); + PtrSetEntry<T> *e = &s->entries.data[i]; + e->next = PTR_SET_SENTINEL; + fr = ptr_set__find_from_entry(s, e); if (fr.entry_prev == PTR_SET_SENTINEL) { - ns.hashes.data[fr.hash_index] = cast(PtrSetIndex)j; + s->hashes[fr.hash_index] = i; } else { - ns.entries.data[fr.entry_prev].next = cast(PtrSetIndex)j; - } - ns.entries.data[j].next = fr.entry_index; - if (ptr_set__full(&ns)) { - ptr_set_grow(&ns); + s->entries[fr.entry_prev].next = i; } } - array_free(&s->entries); - *s = ns; +} + +template <typename T> +void ptr_set_reserve(PtrSet<T> *s, isize cap) { + array_reserve(&s->entries, cap); + if (s->entries.count*2 < s->hashes.count) { + return; + } + slice_resize(&s->hashes, s->entries.allocator, cap*2); + ptr_set_reset_entries(s); +} + + +template <typename T> +void ptr_set_rehash(PtrSet<T> *s, isize new_count) { + ptr_set_reserve(s, new_count); } template <typename T> diff --git a/src/string_map.cpp b/src/string_map.cpp index c8715b60b..2d0da8c66 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -1,10 +1,16 @@ // NOTE(bill): This util stuff is the same for every `Map` + +typedef isize StringMapIndex; + struct StringMapFindResult { - isize hash_index; - isize entry_prev; - isize entry_index; + StringMapIndex hash_index; + StringMapIndex entry_prev; + StringMapIndex entry_index; }; +enum : StringMapIndex { STRING_MAP_SENTINEL = ~(StringMapIndex)0 }; + + struct StringHashKey { u64 hash; String string; @@ -65,6 +71,7 @@ template <typename T> void string_map_remove (StringMap<T> *h, StringH template <typename T> void string_map_clear (StringMap<T> *h); template <typename T> void string_map_grow (StringMap<T> *h); template <typename T> void string_map_rehash (StringMap<T> *h, isize new_count); +template <typename T> void string_map_reserve (StringMap<T> *h, isize cap); template <typename T> gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) { @@ -72,7 +79,7 @@ gb_inline void string_map_init(StringMap<T> *h, gbAllocator a, isize capacity) { slice_init(&h->hashes, a, capacity); array_init(&h->entries, a, 0, capacity); for (isize i = 0; i < capacity; i++) { - h->hashes.data[i] = -1; + h->hashes.data[i] = STRING_MAP_SENTINEL; } } @@ -86,18 +93,18 @@ template <typename T> gb_internal isize string_map__add_entry(StringMap<T> *h, StringHashKey const &key) { StringMapEntry<T> e = {}; e.key = key; - e.next = -1; + e.next = STRING_MAP_SENTINEL; array_add(&h->entries, e); return h->entries.count-1; } template <typename T> gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey const &key) { - StringMapFindResult fr = {-1, -1, -1}; + StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL}; if (h->hashes.count != 0) { fr.hash_index = key.hash & (h->hashes.count-1); fr.entry_index = h->hashes.data[fr.hash_index]; - while (fr.entry_index >= 0) { + while (fr.entry_index != STRING_MAP_SENTINEL) { if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) { return fr; } @@ -110,11 +117,11 @@ gb_internal StringMapFindResult string_map__find(StringMap<T> *h, StringHashKey template <typename T> gb_internal StringMapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) { - StringMapFindResult fr = {-1, -1, -1}; + StringMapFindResult fr = {STRING_MAP_SENTINEL, STRING_MAP_SENTINEL, STRING_MAP_SENTINEL}; if (h->hashes.count != 0) { fr.hash_index = e->key.hash & (h->hashes.count-1); fr.entry_index = h->hashes.data[fr.hash_index]; - while (fr.entry_index >= 0) { + while (fr.entry_index != STRING_MAP_SENTINEL) { if (&h->entries.data[fr.entry_index] == e) { return fr; } @@ -136,45 +143,46 @@ gb_inline void string_map_grow(StringMap<T> *h) { string_map_rehash(h, new_count); } + template <typename T> -void string_map_rehash(StringMap<T> *h, isize new_count) { - isize i, j; - StringMap<T> nh = {}; - new_count = next_pow2_isize(new_count); - nh.hashes = h->hashes; - nh.entries.allocator = h->entries.allocator; - slice_resize(&nh.hashes, h->entries.allocator, new_count); - for (i = 0; i < new_count; i++) { - nh.hashes.data[i] = -1; +void string_map_reset_entries(StringMap<T> *h) { + isize i; + for (i = 0; i < h->hashes.count; i++) { + h->hashes.data[i] = STRING_MAP_SENTINEL; } - array_reserve(&nh.entries, ARRAY_GROW_FORMULA(h->entries.count)); for (i = 0; i < h->entries.count; i++) { - StringMapEntry<T> *e = &h->entries.data[i]; StringMapFindResult fr; - if (nh.hashes.count == 0) { - string_map_grow(&nh); - } - fr = string_map__find(&nh, e->key); - j = string_map__add_entry(&nh, e->key); - if (fr.entry_prev < 0) { - nh.hashes.data[fr.hash_index] = j; + StringMapEntry<T> *e = &h->entries.data[i]; + e->next = STRING_MAP_SENTINEL; + fr = string_map__find_from_entry(h, e); + if (fr.entry_prev == STRING_MAP_SENTINEL) { + h->hashes[fr.hash_index] = i; } else { - nh.entries.data[fr.entry_prev].next = j; - } - nh.entries.data[j].next = fr.entry_index; - nh.entries.data[j].value = e->value; - if (string_map__full(&nh)) { - string_map_grow(&nh); + h->entries[fr.entry_prev].next = i; } } - array_free(&h->entries); - *h = nh; +} + +template <typename T> +void string_map_reserve(StringMap<T> *h, isize cap) { + array_reserve(&h->entries, cap); + if (h->entries.count*2 < h->hashes.count) { + return; + } + slice_resize(&h->hashes, h->entries.allocator, cap*2); + string_map_reset_entries(h); +} + + +template <typename T> +void string_map_rehash(StringMap<T> *h, isize new_count) { + string_map_reserve(h, new_count); } template <typename T> T *string_map_get(StringMap<T> *h, StringHashKey const &key) { isize index = string_map__find(h, key).entry_index; - if (index >= 0) { + if (index != STRING_MAP_SENTINEL) { return &h->entries.data[index].value; } return nullptr; @@ -193,7 +201,7 @@ gb_inline T *string_map_get(StringMap<T> *h, char const *key) { template <typename T> T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) { isize index = string_map__find(h, key).entry_index; - GB_ASSERT(index >= 0); + GB_ASSERT(index != STRING_MAP_SENTINEL); return h->entries.data[index].value; } @@ -215,11 +223,11 @@ void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) { string_map_grow(h); } fr = string_map__find(h, key); - if (fr.entry_index >= 0) { + if (fr.entry_index != STRING_MAP_SENTINEL) { index = fr.entry_index; } else { index = string_map__add_entry(h, key); - if (fr.entry_prev >= 0) { + if (fr.entry_prev != STRING_MAP_SENTINEL) { h->entries.data[fr.entry_prev].next = index; } else { h->hashes.data[fr.hash_index] = index; @@ -246,7 +254,7 @@ gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value) template <typename T> void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) { StringMapFindResult last; - if (fr.entry_prev < 0) { + if (fr.entry_prev == STRING_MAP_SENTINEL) { h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; } else { h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; @@ -257,7 +265,7 @@ void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) { } h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1]; last = string_map__find(h, h->entries.data[fr.entry_index].key); - if (last.entry_prev >= 0) { + if (last.entry_prev != STRING_MAP_SENTINEL) { h->entries.data[last.entry_prev].next = fr.entry_index; } else { h->hashes.data[last.hash_index] = fr.entry_index; @@ -267,7 +275,7 @@ void string_map__erase(StringMap<T> *h, StringMapFindResult const &fr) { template <typename T> void string_map_remove(StringMap<T> *h, StringHashKey const &key) { StringMapFindResult fr = string_map__find(h, key); - if (fr.entry_index >= 0) { + if (fr.entry_index != STRING_MAP_SENTINEL) { string_map__erase(h, fr); } } @@ -276,7 +284,7 @@ template <typename T> gb_inline void string_map_clear(StringMap<T> *h) { array_clear(&h->entries); for (isize i = 0; i < h->hashes.count; i++) { - h->hashes.data[i] = -1; + h->hashes.data[i] = STRING_MAP_SENTINEL; } } |