From 8b4e2a099f9847dfbb59ecee6758aab0d0b4830e Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Sun, 18 May 2025 16:00:18 -0700 Subject: [PATCH] hash store root allocation/deallocation --- src/hash_store/hash_store.c | 64 +++++++++++++++++++++++++++++++++++++ src/hash_store/hash_store.h | 9 +++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/hash_store/hash_store.c b/src/hash_store/hash_store.c index baa6f3b6..20c26aee 100644 --- a/src/hash_store/hash_store.c +++ b/src/hash_store/hash_store.c @@ -55,9 +55,73 @@ hs_init(void) stripe->rw_mutex = os_rw_mutex_alloc(); stripe->cv = os_condition_variable_alloc(); } + hs_shared->root_slots_count = 4096; + hs_shared->root_stripes_count = Min(hs_shared->root_slots_count, os_get_system_info()->logical_processor_count); + hs_shared->root_slots = push_array(arena, HS_RootSlot, hs_shared->root_slots_count); + hs_shared->root_stripes = push_array(arena, HS_Stripe, hs_shared->root_stripes_count); + hs_shared->root_stripes_free_nodes = push_array(arena, HS_RootNode *, hs_shared->root_stripes_count); + for(U64 idx = 0; idx < hs_shared->root_stripes_count; idx += 1) + { + HS_Stripe *stripe = &hs_shared->root_stripes[idx]; + stripe->arena = arena_alloc(); + stripe->rw_mutex = os_rw_mutex_alloc(); + stripe->cv = os_condition_variable_alloc(); + } hs_shared->evictor_thread = os_thread_launch(hs_evictor_thread__entry_point, 0, 0); } +//////////////////////////////// +//~ rjf: Root Allocation/Deallocation + +internal U128 +hs_root_alloc(void) +{ + U128 root = {0}; + root.u64[1] = ins_atomic_u64_inc_eval(&hs_shared->root_id_gen); + U64 slot_idx = root.u64[1]%hs_shared->root_slots_count; + U64 stripe_idx = slot_idx%hs_shared->root_stripes_count; + HS_RootSlot *slot = &hs_shared->root_slots[slot_idx]; + HS_Stripe *stripe = &hs_shared->root_stripes[stripe_idx]; + OS_MutexScopeW(stripe->rw_mutex) + { + HS_RootNode *node = hs_shared->root_stripes_free_nodes[stripe_idx]; + if(node != 0) + { + SLLStackPop(hs_shared->root_stripes_free_nodes[stripe_idx]); + } + else + { + node = push_array(stripe->arena, HS_RootNode, 1); + } + DLLPushBack(slot->first, slot->last, node); + node->root = root; + node->arena = arena_alloc(); + } + return root; +} + +internal void +hs_root_release(U128 root) +{ + U64 slot_idx = root.u64[1]%hs_shared->root_slots_count; + U64 stripe_idx = slot_idx%hs_shared->root_stripes_count; + HS_RootSlot *slot = &hs_shared->root_slots[slot_idx]; + HS_Stripe *stripe = &hs_shared->root_stripes[stripe_idx]; + OS_MutexScopeW(stripe->rw_mutex) + { + for(HS_RootNode *n = slot->first; n != 0; n = n->next) + { + if(u128_match(n->root, root)) + { + DLLRemove(slot->first, slot->last, n); + arena_release(n->arena); + SLLStackPush(hs_shared->root_stripes_free_nodes[stripe_idx], n); + break; + } + } + } +} + //////////////////////////////// //~ rjf: Cache Submission diff --git a/src/hash_store/hash_store.h b/src/hash_store/hash_store.h index e9f56998..b626a0ba 100644 --- a/src/hash_store/hash_store.h +++ b/src/hash_store/hash_store.h @@ -180,8 +180,9 @@ struct HS_Shared U64 root_slots_count; U64 root_stripes_count; HS_RootSlot *root_slots; - HS_Stripe root_stripes; + HS_Stripe *root_stripes; HS_RootNode **root_stripes_free_nodes; + U64 root_id_gen; // rjf: evictor thread OS_Handle evictor_thread; @@ -203,6 +204,12 @@ internal U128 hs_hash_from_data(String8 data); internal void hs_init(void); +//////////////////////////////// +//~ rjf: Root Allocation/Deallocation + +internal U128 hs_root_alloc(void); +internal void hs_root_release(U128 root); + //////////////////////////////// //~ rjf: Cache Submission