Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/artifact_cache/artifact_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ac_artifact_from_key_(Access *access, String8 key, AC_ArtifactParams *params, U6
AC_Cache *cache = 0;
{
U64 cache_hash = u64_hash_from_str8(str8_struct(&params->create));
U64 cache_slot_idx = cache_hash%ac_shared->cache_slots_count;
U64 cache_slot_idx = hash_index64(cache_hash, ac_shared->cache_slots_count);
Stripe *cache_stripe = stripe_from_slot_idx(&ac_shared->cache_stripes, cache_slot_idx);
for(B32 write_mode = 0; write_mode <= 1; write_mode += 1)
{
Expand Down Expand Up @@ -70,7 +70,7 @@ ac_artifact_from_key_(Access *access, String8 key, AC_ArtifactParams *params, U6

//- rjf: unpack key
U64 hash = u64_hash_from_str8(key);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
AC_Slot *slot = &cache->slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);

Expand Down Expand Up @@ -415,7 +415,7 @@ ac_async_tick(void)
if(status != AC_Status_NeedRetry && lane_idx() == 0)
{
U64 cache_hash = u64_hash_from_str8(str8_struct(&r->create));
U64 cache_slot_idx = cache_hash%ac_shared->cache_slots_count;
U64 cache_slot_idx = hash_index64(cache_hash, ac_shared->cache_slots_count);
Stripe *cache_stripe = stripe_from_slot_idx(&ac_shared->cache_stripes, cache_slot_idx);
RWMutexScope(cache_stripe->rw_mutex, 0)
{
Expand All @@ -434,7 +434,7 @@ ac_async_tick(void)
if(status != AC_Status_NeedRetry && lane_idx() == 0)
{
U64 hash = u64_hash_from_str8(r->key);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
AC_Slot *slot = &cache->slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 1)
Expand Down Expand Up @@ -542,7 +542,7 @@ ac_async_tick(void)
if(status != AC_Status_NeedRetry)
{
U64 cache_hash = u64_hash_from_str8(str8_struct(&r->create));
U64 cache_slot_idx = cache_hash%ac_shared->cache_slots_count;
U64 cache_slot_idx = hash_index64(cache_hash, ac_shared->cache_slots_count);
Stripe *cache_stripe = stripe_from_slot_idx(&ac_shared->cache_stripes, cache_slot_idx);
RWMutexScope(cache_stripe->rw_mutex, 0)
{
Expand All @@ -561,7 +561,7 @@ ac_async_tick(void)
if(status != AC_Status_NeedRetry)
{
U64 hash = u64_hash_from_str8(r->key);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
AC_Slot *slot = &cache->slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 1)
Expand Down
2 changes: 1 addition & 1 deletion src/base/base_command_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cmd_line_slot_from_string(CmdLine *cmd_line, String8 string)
if(cmd_line->option_table_size != 0)
{
U64 hash = u64_hash_from_str8(string);
U64 bucket = hash % cmd_line->option_table_size;
U64 bucket = hash_index64(hash, cmd_line->option_table_size);
slot = &cmd_line->option_table[bucket];
}
return slot;
Expand Down
28 changes: 28 additions & 0 deletions src/base/base_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,34 @@ count_digits_u64(U64 v, U64 radix)
return count;
}

////////////////////////////////

internal U32
hash_index32(U32 hash, U32 count)
{
U32 result = 0;
#if COMPILER_MSVC && ARCH_X64
result = __emulu(hash, count) >> 32;
#else
result = ((U64)hash * count) >> 32;
#endif
return result;
}

internal U64
hash_index64(U64 hash, U64 count)
{
U64 result = 0;
#if COMPILER_MSVC && ARCH_X64
(void)_umul128(hash, count, &result);
#elif COMPILER_MSVC && ARCH_ARM64
result = __umulh(hash, count);
#else
result = ((unsigned __int128)hash * count) >> 64;
#endif
return result;
}

////////////////////////////////
//~ rjf: Third Party Includes

Expand Down
8 changes: 8 additions & 0 deletions src/base/base_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1132,4 +1132,12 @@ internal U64 index_of_zero_u32(U32 *ptr, U64 count);
internal U64 index_of_zero_u64(U64 *ptr, U64 count);
internal U64 count_digits_u64(U64 v, U64 radix);

////////////////////////////////
// maps uniform random 32-bit or 64-bit hash value to [0, count) interval
// this is similar to "hash % count" operation, except it is much faster
// be aware it will calculate different result than % operation!

internal U32 hash_index32(U32 hash, U32 count);
internal U64 hash_index64(U64 hash, U64 count);

#endif // BASE_CORE_H
8 changes: 4 additions & 4 deletions src/config/config_bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ cfg_key_map_from_cfg(Arena *arena)
{
U64 name_hash = d_hash_from_string(name);
U64 binding_hash = d_hash_from_string(str8_struct(&binding));
U64 name_slot_idx = name_hash%key_map->name_slots_count;
U64 binding_slot_idx = binding_hash%key_map->binding_slots_count;
U64 name_slot_idx = hash_index64(name_hash, key_map->name_slots_count);
U64 binding_slot_idx = hash_index64(binding_hash, key_map->binding_slots_count);
CFG_KeyMapNode *n = push_array(arena, CFG_KeyMapNode, 1);
n->cfg_id = keybinding->id;
n->name = push_str8_copy(arena, name);
Expand All @@ -74,7 +74,7 @@ cfg_key_map_node_ptr_list_from_name(Arena *arena, CFG_KeyMap *key_map, String8 s
CFG_KeyMapNodePtrList list = {0};
{
U64 hash = d_hash_from_string(string);
U64 slot_idx = hash%key_map->name_slots_count;
U64 slot_idx = hash_index64(hash, key_map->name_slots_count);
for(CFG_KeyMapNode *n = key_map->name_slots[slot_idx].first; n != 0; n = n->name_hash_next)
{
if(str8_match(n->name, string, 0))
Expand All @@ -95,7 +95,7 @@ cfg_key_map_node_ptr_list_from_binding(Arena *arena, CFG_KeyMap *key_map, CFG_Bi
CFG_KeyMapNodePtrList list = {0};
{
U64 hash = d_hash_from_string(str8_struct(&binding));
U64 slot_idx = hash%key_map->binding_slots_count;
U64 slot_idx = hash_index64(hash, key_map->binding_slots_count);
for(CFG_KeyMapNode *n = key_map->binding_slots[slot_idx].first; n != 0; n = n->binding_hash_next)
{
if(MemoryMatchStruct(&binding, &n->binding))
Expand Down
10 changes: 5 additions & 5 deletions src/config/config_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal void
cfg_schema_table_insert(Arena *arena, CFG_SchemaTable *table, String8 name, MD_Node *schema)
{
U64 hash = u64_hash_from_str8(name);
U64 slot_idx = hash%table->slots_count;
U64 slot_idx = hash_index64(hash, table->slots_count);
CFG_SchemaNode *node = 0;
for(CFG_SchemaNode *n = table->slots[slot_idx]; n != 0; n = n->next)
{
Expand All @@ -94,7 +94,7 @@ cfg_schema_from_name(CFG_SchemaTable *table, String8 name)
MD_Node *result = &md_nil_node;
{
U64 hash = u64_hash_from_str8(name);
U64 slot_idx = hash%table->slots_count;
U64 slot_idx = hash_index64(hash, table->slots_count);
CFG_SchemaNode *node = 0;
for(CFG_SchemaNode *n = table->slots[slot_idx]; n != 0; n = n->next)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ cfg_node_from_id(CFG_ID id)
else
{
U64 hash = u64_hash_from_str8(str8_struct(&id));
U64 slot_idx = hash%cfg_ctx->id_slots_count;
U64 slot_idx = hash_index64(hash, cfg_ctx->id_slots_count);
for(CFG_NodePtrNode *n = cfg_ctx->id_slots[slot_idx].first; n != 0; n = n->next)
{
if(n->v->id == id)
Expand Down Expand Up @@ -620,7 +620,7 @@ cfg_node_alloc(CFG_State *state)
cfg_id_node = push_array(state->arena, CFG_NodePtrNode, 1);
}
U64 hash = u64_hash_from_str8(str8_struct(&result->id));
U64 slot_idx = hash%state->ctx.id_slots_count;
U64 slot_idx = hash_index64(hash, state->ctx.id_slots_count);
DLLPushBack(state->ctx.id_slots[slot_idx].first, state->ctx.id_slots[slot_idx].last, cfg_id_node);
cfg_id_node->v = result;
}
Expand Down Expand Up @@ -650,7 +650,7 @@ cfg_node_release(CFG_State *state, CFG_Node *node)
{
CFG_Node *c = n->v;
U64 hash = u64_hash_from_str8(str8_struct(&c->id));
U64 slot_idx = hash%state->ctx.id_slots_count;
U64 slot_idx = hash_index64(hash, state->ctx.id_slots_count);
cfg_string_release(state, c->string);
SLLStackPush(state->free, c);
c->first = c->last = c->prev = c->parent = 0;
Expand Down
20 changes: 10 additions & 10 deletions src/content/content.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
{
//- rjf: unpack key
U64 key_hash = u64_hash_from_str8(str8_struct(&key));
U64 key_slot_idx = key_hash%c_shared->key_slots_count;
U64 key_slot_idx = hash_index64(key_hash, c_shared->key_slots_count);
U64 key_stripe_idx = key_slot_idx%c_shared->key_stripes_count;
C_KeySlot *key_slot = &c_shared->key_slots[key_slot_idx];
C_Stripe *key_stripe = &c_shared->key_stripes[key_stripe_idx];

//- rjf: hash data, unpack hash
U128 hash = u128_hash_from_str8(data);
U64 slot_idx = hash.u64[1]%c_shared->blob_slots_count;
U64 slot_idx = hash_index64(hash.u64[1], c_shared->blob_slots_count);
U64 stripe_idx = slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *slot = &c_shared->blob_slots[slot_idx];
C_Stripe *stripe = &c_shared->blob_stripes[stripe_idx];
Expand Down Expand Up @@ -273,7 +273,7 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
if(key_is_new)
{
U64 root_hash = u64_hash_from_str8(str8_struct(&key.root));
U64 root_slot_idx = root_hash%c_shared->root_slots_count;
U64 root_slot_idx = hash_index64(root_hash, c_shared->root_slots_count);
U64 root_stripe_idx = root_slot_idx%c_shared->root_stripes_count;
C_RootSlot *root_slot = &c_shared->root_slots[root_slot_idx];
C_Stripe *root_stripe = &c_shared->root_stripes[root_stripe_idx];
Expand Down Expand Up @@ -305,7 +305,7 @@ c_submit_data(C_Key key, Arena **data_arena, String8 data)
//- rjf: decrement key ref count of expired hash
if(!u128_match(key_expired_hash, u128_zero())) ProfScope("decrement key ref count of expired hash")
{
U64 old_hash_slot_idx = key_expired_hash.u64[1]%c_shared->blob_slots_count;
U64 old_hash_slot_idx = hash_index64(key_expired_hash.u64[1], c_shared->blob_slots_count);
U64 old_hash_stripe_idx = old_hash_slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *old_hash_slot = &c_shared->blob_slots[old_hash_slot_idx];
C_Stripe *old_hash_stripe = &c_shared->blob_stripes[old_hash_stripe_idx];
Expand All @@ -332,7 +332,7 @@ internal void
c_close_key(C_Key key)
{
U64 key_hash = u64_hash_from_str8(str8_struct(&key));
U64 key_slot_idx = key_hash%c_shared->key_slots_count;
U64 key_slot_idx = hash_index64(key_hash, c_shared->key_slots_count);
U64 key_stripe_idx = key_slot_idx%c_shared->key_stripes_count;
C_KeySlot *key_slot = &c_shared->key_slots[key_slot_idx];
C_Stripe *key_stripe = &c_shared->key_stripes[key_stripe_idx];
Expand All @@ -347,7 +347,7 @@ c_close_key(C_Key key)
history_idx += 1)
{
U128 hash = n->hash_history[(n->hash_history_gen-1-history_idx) % ArrayCount(n->hash_history)];
U64 hash_slot_idx = hash.u64[1]%c_shared->blob_slots_count;
U64 hash_slot_idx = hash_index64(hash.u64[1], c_shared->blob_slots_count);
U64 hash_stripe_idx = hash_slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *hash_slot = &c_shared->blob_slots[hash_slot_idx];
C_Stripe *hash_stripe = &c_shared->blob_stripes[hash_stripe_idx];
Expand Down Expand Up @@ -377,7 +377,7 @@ c_close_key(C_Key key)
internal void
c_hash_downstream_inc(U128 hash)
{
U64 slot_idx = hash.u64[1]%c_shared->blob_slots_count;
U64 slot_idx = hash_index64(hash.u64[1], c_shared->blob_slots_count);
U64 stripe_idx = slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *slot = &c_shared->blob_slots[slot_idx];
C_Stripe *stripe = &c_shared->blob_stripes[stripe_idx];
Expand All @@ -397,7 +397,7 @@ c_hash_downstream_inc(U128 hash)
internal void
c_hash_downstream_dec(U128 hash)
{
U64 slot_idx = hash.u64[1]%c_shared->blob_slots_count;
U64 slot_idx = hash_index64(hash.u64[1], c_shared->blob_slots_count);
U64 stripe_idx = slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *slot = &c_shared->blob_slots[slot_idx];
C_Stripe *stripe = &c_shared->blob_stripes[stripe_idx];
Expand All @@ -422,7 +422,7 @@ c_hash_from_key(C_Key key, U64 rewind_count)
{
U128 result = {0};
U64 key_hash = u64_hash_from_str8(str8_struct(&key));
U64 key_slot_idx = key_hash%c_shared->key_slots_count;
U64 key_slot_idx = hash_index64(key_hash, c_shared->key_slots_count);
U64 key_stripe_idx = key_slot_idx%c_shared->key_stripes_count;
C_KeySlot *key_slot = &c_shared->key_slots[key_slot_idx];
C_Stripe *key_stripe = &c_shared->key_stripes[key_stripe_idx];
Expand All @@ -445,7 +445,7 @@ c_data_from_hash(Access *access, U128 hash)
{
ProfBeginFunction();
String8 result = {0};
U64 slot_idx = hash.u64[1]%c_shared->blob_slots_count;
U64 slot_idx = hash_index64(hash.u64[1], c_shared->blob_slots_count);
U64 stripe_idx = slot_idx%c_shared->blob_stripes_count;
C_BlobSlot *slot = &c_shared->blob_slots[slot_idx];
C_Stripe *stripe = &c_shared->blob_stripes[stripe_idx];
Expand Down
26 changes: 13 additions & 13 deletions src/dbg_engine/dbg_engine_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ d_entity_from_handle(D_Handle handle)
if(!d_handle_match(handle, d_handle_zero()))
{
U64 hash = d_hash_from_handle(handle);
U64 slot_idx = hash%d_entity_ctx->hash_slots_count;
U64 slot_idx = hash_index64(hash, d_entity_ctx->hash_slots_count);
D_EntityHashSlot *slot = &d_entity_ctx->hash_slots[slot_idx];
D_EntityHashNode *node = 0;
for(D_EntityHashNode *n = slot->first; n != 0; n = n->next)
Expand Down Expand Up @@ -1059,7 +1059,7 @@ d_entity_alloc(D_EntityCtxRWStore *store, D_Entity *parent, D_EntityKind kind, A
// rjf: insert into hash map
{
U64 hash = d_hash_from_handle(handle);
U64 slot_idx = hash%store->ctx.hash_slots_count;
U64 slot_idx = hash_index64(hash, store->ctx.hash_slots_count);
D_EntityHashSlot *slot = &store->ctx.hash_slots[slot_idx];
D_EntityHashNode *node = 0;
for(D_EntityHashNode *n = slot->first; n != 0; n = n->next)
Expand Down Expand Up @@ -1131,7 +1131,7 @@ d_entity_release(D_EntityCtxRWStore *store, D_Entity *entity)
// rjf: remove from hash map
{
U64 hash = d_hash_from_handle(t->e->handle);
U64 slot_idx = hash%store->ctx.hash_slots_count;
U64 slot_idx = hash_index64(hash, store->ctx.hash_slots_count);
D_EntityHashSlot *slot = &store->ctx.hash_slots[slot_idx];
D_EntityHashNode *node = 0;
for(D_EntityHashNode *n = slot->first; n != 0; n = n->next)
Expand Down Expand Up @@ -1516,7 +1516,7 @@ d_thread_read_reg_block(D_Handle handle, void *reg_block)
D_Entity *process = d_process_from_entity(thread);
D_DumpCache *cache = &d_ctrl_state->dump_cache;
U64 hash = d_hash_from_handle(process->handle);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 0)
{
Expand Down Expand Up @@ -1647,7 +1647,7 @@ d_cached_reg_block_from_thread(Arena *arena, D_Handle handle)
ARCH_Info *arch_info = arch_info_from_arch(arch);
U64 reg_block_size = arch_info->reg_block_size;
U64 hash = d_hash_from_handle(handle);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
U64 stripe_idx = slot_idx%cache->stripes_count;
D_ThreadRegCacheSlot *slot = &cache->slots[slot_idx];
D_ThreadRegCacheStripe *stripe = &cache->stripes[stripe_idx];
Expand Down Expand Up @@ -1736,7 +1736,7 @@ d_info_from_module(Access *access, D_Handle module)
{
D_ModuleInfoCache *cache = &d_ctrl_state->module_info_cache;
U64 hash = d_hash_from_handle(module);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
D_ModuleInfoCacheSlot *slot = &cache->slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
MutexScopeR(stripe->rw_mutex) for(D_ModuleInfoCacheNode *n = slot->first; n != 0; n = n->next)
Expand Down Expand Up @@ -2755,7 +2755,7 @@ d_ctrl_thread__module_open(D_Handle process, D_Handle module, U64 base_vaddr, DM
{
D_ModuleInfoCache *cache = &d_ctrl_state->module_info_cache;
U64 hash = d_hash_from_handle(module);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
D_ModuleInfoCacheSlot *slot = &cache->slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
MutexScopeW(stripe->rw_mutex)
Expand Down Expand Up @@ -2795,7 +2795,7 @@ d_ctrl_thread__module_close(D_Handle process, D_Handle module, U64 base_vaddr)
{
D_ModuleInfoCache *cache = &d_ctrl_state->module_info_cache;
U64 hash = d_hash_from_handle(module);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
D_ModuleInfoCacheSlot *slot = &cache->slots[slot_idx];
MutexScopeW(stripe->rw_mutex) for(;;)
Expand Down Expand Up @@ -2863,7 +2863,7 @@ d_ctrl_thread__close_dump_process(D_MsgID msg_id, D_Handle process)
{
D_DumpCache *cache = &d_ctrl_state->dump_cache;
U64 hash = d_hash_from_handle(process);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);

// rjf: remove node
Expand Down Expand Up @@ -3486,7 +3486,7 @@ d_ctrl_thread__eval_scope_begin(Arena *arena, D_BreakpointList *user_bps, D_Enti
{
// rjf: find cached result
U64 hash = d_hash_from_handle(mod->handle);
U64 slot_idx = hash%d_ctrl_state->module_req_cache_slots_count;
U64 slot_idx = hash_index64(hash, d_ctrl_state->module_req_cache_slots_count);
D_ModuleReqCacheNode *slot = d_ctrl_state->module_req_cache_slots[slot_idx];
D_ModuleReqCacheNode *node = 0;
for(D_ModuleReqCacheNode *n = slot; n != 0; n = n->next)
Expand Down Expand Up @@ -4034,7 +4034,7 @@ d_ctrl_thread__open_crash_dump(DMN_CtrlCtx *ctrl_ctx, D_Msg *msg)
{
U64 hash = d_hash_from_handle(process);
D_DumpCache *cache = &d_ctrl_state->dump_cache;
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 1)
{
Expand Down Expand Up @@ -5552,7 +5552,7 @@ d_process_read(D_Handle process, Rng1U64 range, void *dst)
{
D_DumpCache *cache = &d_ctrl_state->dump_cache;
U64 hash = d_hash_from_handle(process);
U64 slot_idx = hash%cache->slots_count;
U64 slot_idx = hash_index64(hash, cache->slots_count);
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 0)
{
Expand Down Expand Up @@ -6123,7 +6123,7 @@ d_call_stack_artifact_create(String8 key, B32 *cancel_signal, AC_Status *status_
if(dst_e != &d_entity_nil)
{
U64 hash = d_hash_from_handle(dst_e->handle);
U64 slot_idx = hash%dst_ctx->hash_slots_count;
U64 slot_idx = hash_index64(hash, dst_ctx->hash_slots_count);
D_EntityHashSlot *slot = &dst_ctx->hash_slots[slot_idx];
D_EntityHashNode *node = 0;
for(D_EntityHashNode *n = slot->first; n != 0; n = n->next)
Expand Down
Loading
Loading