1 parent
188cfae0d7
commit
5687cb512c
21 files changed
+108
-27
No files matched your search
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "../../stream/stream.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
@@ -30,6 +31,7 @@ typedef struct WpSiphashCtx {
|
||||
|
||||
typedef struct WpHasherCtx {
|
||||
WpHasherCtxType type;
|
||||
u8 padding[4];
|
||||
union {
|
||||
WpSiphashCtx siphash;
|
||||
void *user;
|
||||
|
||||
@@ -29,7 +29,7 @@ WpHasher wpX64Mur3HasherWithSeed(u32 seed) {
|
||||
WpU128 wpX64Mur3GetHash128(WpU8Stream *bytes, WpHasherCtx ctx) {
|
||||
validateMur3HasherCtx(&ctx);
|
||||
|
||||
const i32 nblocks = (bytes->count * bytes->item_size) / 16;
|
||||
const i32 nblocks = (const i32)((bytes->count * bytes->item_size) / 16);
|
||||
|
||||
u64 h1 = ctx.murmur3;
|
||||
u64 h2 = ctx.murmur3;
|
||||
|
||||
@@ -119,12 +119,20 @@ void hashTableGrow(const WpAllocator *allocator, WpHashTable *table, u64 key_siz
|
||||
table->buckets = buckets;
|
||||
table->bucket_mask = mask;
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(old_keys); ++i) {
|
||||
void *key = arrayGet(old_keys, i, key_size);
|
||||
void *value = table->has_values ? arrayGet(old_values, i, value_size) : NULL;
|
||||
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
|
||||
// MSVC Spectre mitigation warnings
|
||||
u64 count = wpArrayCount(old_keys);
|
||||
u64 index = 0;
|
||||
b8 running = true;
|
||||
while (running) {
|
||||
void *key = arrayGet(old_keys, index, key_size);
|
||||
void *value = table->has_values ? arrayGet(old_values, index, value_size) : NULL;
|
||||
u64 hash = hashTableCalcHash(table, key, key_size);
|
||||
WpHashBucketRef ref = hashTableBucketRef(table, hash);
|
||||
hashTableInsert(table, key, value, ref, key_size, value_size);
|
||||
|
||||
++index;
|
||||
running = index < count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +140,9 @@ WpHashLookupResult hashTableLookup(WpHashTable *table, void *key, WpHashBucketRe
|
||||
hashTableValidate(table, key_size, 0);
|
||||
wpDebugAssert(key != NULL, "`key` should not be NULL");
|
||||
|
||||
while (true) {
|
||||
u64 initial_bucket_index = ref.bucket_index;
|
||||
b8 running = true;
|
||||
while (running) {
|
||||
WpHashBucket *bucket = &table->buckets[ref.bucket_index];
|
||||
|
||||
if (ref.dist_and_tag == bucket->dist_and_tag) {
|
||||
@@ -152,7 +162,16 @@ WpHashLookupResult hashTableLookup(WpHashTable *table, void *key, WpHashBucketRe
|
||||
|
||||
ref.dist_and_tag = hashTableDistInc(ref.dist_and_tag);
|
||||
ref.bucket_index = hashTableNextBucket(table, ref.bucket_index);
|
||||
|
||||
// Break if we loop back to the start bucket index
|
||||
running = ref.bucket_index != initial_bucket_index;
|
||||
}
|
||||
|
||||
return (WpHashLookupResult){
|
||||
.ref.dist_and_tag = ref.dist_and_tag,
|
||||
.ref.bucket_index = ref.bucket_index,
|
||||
.found = false,
|
||||
};
|
||||
}
|
||||
|
||||
void hashTableUpdate(WpHashTable *table, void *value, WpHashBucketRef ref, u64 value_size) {
|
||||
@@ -172,7 +191,10 @@ void hashTableInsert(WpHashTable *table, void *key, void *value, WpHashBucketRef
|
||||
wpDebugAssert(key != NULL, "`key` should not be NULL");
|
||||
|
||||
u64 index = wpArrayCount(table->keys);
|
||||
while (true) {
|
||||
|
||||
// NOTE (Abdelrahman): Extract into a variable to satisfy MSVC
|
||||
b8 running = true;
|
||||
while (running) {
|
||||
WpHashBucket *bucket = &table->buckets[ref.bucket_index];
|
||||
|
||||
if (bucket->dist_and_tag == 0) {
|
||||
@@ -202,7 +224,9 @@ void hashTableRemove(WpHashTable *table, WpHashBucketRef ref, u64 key_size, u64
|
||||
|
||||
u64 last_item_index = wpArrayCount(table->keys) - 1;
|
||||
|
||||
if (bucket->item_index < last_item_index) {
|
||||
// NOTE (Abdelrahman): Extract into a variable to get rid of MSVC Spectre mitigation warnings
|
||||
b8 is_last_item = bucket->item_index == last_item_index;
|
||||
if (!is_last_item) {
|
||||
// Update the bucket for the last item before swapping
|
||||
u64 hash = hashTableCalcHash(table, arrayGet(table->keys, last_item_index, key_size), key_size);
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "../../stream/stream.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
@@ -56,7 +57,7 @@ typedef struct WpHashTable {
|
||||
u64 magic;
|
||||
u64 bucket_mask;
|
||||
f32 load_factor;
|
||||
b8 has_values;
|
||||
b32 has_values;
|
||||
} WpHashTable;
|
||||
|
||||
typedef struct WpHashBucketRef {
|
||||
@@ -66,7 +67,9 @@ typedef struct WpHashBucketRef {
|
||||
|
||||
typedef struct WpHashLookupResult {
|
||||
WpHashBucketRef ref;
|
||||
b8 found;
|
||||
b32 found;
|
||||
|
||||
wpMiscUtilsReservePadding(sizeof(WpHashBucketRef) + sizeof(b32));
|
||||
} WpHashLookupResult;
|
||||
|
||||
void hashTableAlloc(const WpAllocator *allocator, WpHashTable *table,
|
||||
|
||||
Reference in new issue
Block a user