From fa4f750beb8237686341247ff1c1b1a39d434a86 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 21 May 2025 14:14:05 -0500 Subject: [PATCH] rename 'fixed_dict' -> 'dict' --- src/renderer_d3d11.c | 8 ++--- src/resource.c | 6 ++-- src/sprite.c | 22 ++++++------ src/sprite.h | 4 +-- src/tar.c | 8 ++--- src/tar.h | 2 +- src/util.h | 80 +++++++++++++++++++++----------------------- 7 files changed, 64 insertions(+), 66 deletions(-) diff --git a/src/renderer_d3d11.c b/src/renderer_d3d11.c index e65e040b..18048967 100644 --- a/src/renderer_d3d11.c +++ b/src/renderer_d3d11.c @@ -174,7 +174,7 @@ GLOBAL struct { struct dx11_shader shaders[NUM_SHADERS]; struct dx11_shader_desc shader_info[NUM_SHADERS]; - struct fixed_dict shader_info_lookup; + struct dict shader_info_lookup; } G = ZI, DEBUG_ALIAS(G, G_renderer_d3d11); @@ -240,12 +240,12 @@ INTERNAL void init_shader_table(void) } }; - G.shader_info_lookup = fixed_dict_init(&G.arena, SHADER_INFO_LOOKUP_BINS); + G.shader_info_lookup = dict_init(&G.arena, SHADER_INFO_LOOKUP_BINS); for (u64 i = SHADER_NONE + 1; i < ARRAY_COUNT(G.shader_info); ++i) { struct dx11_shader_desc *desc = &G.shader_info[i]; struct string name = string_from_cstr_no_limit(desc->name_cstr); u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - fixed_dict_set(&G.arena, &G.shader_info_lookup, hash, desc); + dict_set(&G.arena, &G.shader_info_lookup, hash, desc); } } @@ -390,7 +390,7 @@ INTERNAL void reload_shader(struct dx11_shader *old_shader, struct dx11_shader_d INTERNAL RESOURCE_WATCH_CALLBACK_FUNC_DEF(shader_resource_watch_callback, name) { u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - struct dx11_shader_desc *desc = (struct dx11_shader_desc *)fixed_dict_get(&G.shader_info_lookup, hash); + struct dx11_shader_desc *desc = (struct dx11_shader_desc *)dict_get(&G.shader_info_lookup, hash); if (desc) { logf_info("Shader source file \"%F\" has changed", FMT_STR(name)); atomic_i32_eval_exchange(&desc->should_reload, 1); diff --git a/src/resource.c b/src/resource.c index 24d64e83..761a6adc 100644 --- a/src/resource.c +++ b/src/resource.c @@ -236,15 +236,15 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(resource_watch_dispatcher_thread_entry_ sys_mutex_unlock(&watch_dispatcher_lock); { __profscope(run_resource_watch_callbacks); - struct fixed_dict dedup_dict = fixed_dict_init(temp.arena, WATCH_DISPATCHER_DEDUP_DICT_BINS); + struct dict dedup_dict = dict_init(temp.arena, WATCH_DISPATCHER_DEDUP_DICT_BINS); for (struct sys_watch_info *info = watch_info_list.first; info; info = info->next) { /* Do not run callbacks for the same file more than once */ b32 skip = false; u64 hash = hash_fnv64(HASH_FNV64_BASIS, info->name); - if ((u64)fixed_dict_get(&dedup_dict, hash) == 1) { + if (dict_get(&dedup_dict, hash) == 1) { skip = true; } else { - fixed_dict_set(temp.arena, &dedup_dict, hash, (void *)1); + dict_set(temp.arena, &dedup_dict, hash, 1); } if (!skip) { struct sys_lock callbacks_lock = sys_mutex_lock_s(&G.watch_callbacks_mutex); diff --git a/src/sprite.c b/src/sprite.c index 32661530..2d9fda4e 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -447,7 +447,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str if (ase.num_spans > 0) { __profscope(init_spans); sheet.spans = arena_push_array(arena, struct sprite_sheet_span, sheet.spans_count); - sheet.spans_dict = fixed_dict_init(arena, (u64)(ase.num_spans * SHEET_SPAN_LOOKUP_TABLE_BIN_RATIO)); + sheet.spans_dict = dict_init(arena, (u64)(ase.num_spans * SHEET_SPAN_LOOKUP_TABLE_BIN_RATIO)); u64 index = 0; for (struct ase_span *ase_span = ase.span_head; ase_span; ase_span = ase_span->next) { struct string name = string_copy(arena, ase_span->name); @@ -456,7 +456,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str span->start = ase_span->start; span->end = ase_span->end; u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - fixed_dict_set(arena, &sheet.spans_dict, hash, span); + dict_set(arena, &sheet.spans_dict, hash, span); ++index; } } @@ -487,15 +487,15 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str u64 num_temp_slice_group_nodes = 0; struct temp_slice_group_node *temp_slice_group_head = NULL; { - struct fixed_dict temp_slice_dict = fixed_dict_init(scratch.arena, (u64)(ase.num_slice_keys * 2)); + struct dict temp_slice_dict = dict_init(scratch.arena, (u64)(ase.num_slice_keys * 2)); for (struct ase_slice_key *ase_slice_key = ase.slice_key_head; ase_slice_key; ase_slice_key = ase_slice_key->next) { struct string name = ase_slice_key->name; u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - struct temp_slice_group_node *temp_slice_group_node = fixed_dict_get(&temp_slice_dict, hash); + struct temp_slice_group_node *temp_slice_group_node = dict_get(&temp_slice_dict, hash); if (!temp_slice_group_node) { temp_slice_group_node = arena_push(scratch.arena, struct temp_slice_group_node); temp_slice_group_node->name = name; - fixed_dict_set(scratch.arena, &temp_slice_dict, hash, temp_slice_group_node); + dict_set(scratch.arena, &temp_slice_dict, hash, temp_slice_group_node); ++num_temp_slice_group_nodes; temp_slice_group_node->next = temp_slice_group_head; @@ -516,7 +516,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str /* Allocate slice groups & fill originals in 2d array */ sheet.slice_groups_count = num_temp_slice_group_nodes; sheet.slice_groups = arena_push_array(arena, struct sprite_sheet_slice_group, sheet.slice_groups_count); - sheet.slice_groups_dict = fixed_dict_init(arena, (u64)(num_temp_slice_group_nodes * SHEET_SLICE_LOOKUP_TABLE_BIN_RATIO)); + sheet.slice_groups_dict = dict_init(arena, (u64)(num_temp_slice_group_nodes * SHEET_SLICE_LOOKUP_TABLE_BIN_RATIO)); u64 index = 0; for (struct temp_slice_group_node *temp_slice_group_node = temp_slice_group_head; temp_slice_group_node; temp_slice_group_node = temp_slice_group_node->next) { @@ -579,7 +579,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str temp_slice_group_node->final_slice_group = slice_group; u64 hash = hash_fnv64(HASH_FNV64_BASIS, slice_group->name); - fixed_dict_set(arena, &sheet.slice_groups_dict, hash, slice_group); + dict_set(arena, &sheet.slice_groups_dict, hash, slice_group); ++index; } @@ -628,7 +628,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str struct string point_slice_name = ray_slice_name; point_slice_name.len -= ray_suffix.len; u64 hash = hash_fnv64(HASH_FNV64_BASIS, point_slice_name); - struct sprite_sheet_slice_group *point_slice_group = fixed_dict_get(&sheet.slice_groups_dict, hash); + struct sprite_sheet_slice_group *point_slice_group = dict_get(&sheet.slice_groups_dict, hash); if (point_slice_group) { u32 point_slices_per_frame = point_slice_group->per_frame_count; @@ -1064,7 +1064,7 @@ struct sprite_sheet_span sprite_sheet_get_span(struct sprite_sheet *sheet, struc struct sprite_sheet_span res = ZI; if (sheet->spans_count > 0) { u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - struct sprite_sheet_span *entry = fixed_dict_get(&sheet->spans_dict, hash); + struct sprite_sheet_span *entry = dict_get(&sheet->spans_dict, hash); if (entry) { res = *entry; } @@ -1076,7 +1076,7 @@ struct sprite_sheet_slice sprite_sheet_get_slice(struct sprite_sheet *sheet, str { if (sheet->slice_groups_count > 0) { u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, hash); + struct sprite_sheet_slice_group *group = dict_get(&sheet->slice_groups_dict, hash); if (group) { return group->frame_slices[frame_index * group->per_frame_count]; } @@ -1102,7 +1102,7 @@ struct sprite_sheet_slice_array sprite_sheet_get_slices(struct sprite_sheet *she struct sprite_sheet_slice_array res = ZI; if (sheet->slice_groups_count > 0) { u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, hash); + struct sprite_sheet_slice_group *group = dict_get(&sheet->slice_groups_dict, hash); if (group) { res.count = group->per_frame_count; res.slices = &group->frame_slices[frame_index * group->per_frame_count]; diff --git a/src/sprite.h b/src/sprite.h index f812a436..80f941aa 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -71,11 +71,11 @@ struct sprite_sheet { u32 spans_count; struct sprite_sheet_span *spans; - struct fixed_dict spans_dict; + struct dict spans_dict; u32 slice_groups_count; struct sprite_sheet_slice_group *slice_groups; - struct fixed_dict slice_groups_dict; + struct dict slice_groups_dict; }; struct sprite_sheet *sprite_sheet_from_tag_await(struct sprite_scope *scope, struct sprite_tag tag); diff --git a/src/tar.c b/src/tar.c index ba154e94..22eda27c 100644 --- a/src/tar.c +++ b/src/tar.c @@ -130,10 +130,10 @@ struct tar_archive tar_parse(struct arena *arena, struct string data, struct str } /* Build lookup table */ - archive.lookup = fixed_dict_init(arena, (u64)((f64)num_files * ARCHIVE_LOOKUP_TABLE_CAPACITY_FACTOR)); + archive.lookup = dict_init(arena, (u64)((f64)num_files * ARCHIVE_LOOKUP_TABLE_CAPACITY_FACTOR)); for (struct tar_entry *entry = archive.head; entry; entry = entry->next) { u64 hash = hash_fnv64(HASH_FNV64_BASIS, entry->file_name); - fixed_dict_set(arena, &archive.lookup, hash, entry); + dict_set(arena, &archive.lookup, hash, entry); } /* Build hierarchy */ @@ -147,7 +147,7 @@ struct tar_archive tar_parse(struct arena *arena, struct string data, struct str for (struct string parent_dir_name = entry->file_name; parent_dir_name.len > 0; --parent_dir_name.len) { if (parent_dir_name.text[parent_dir_name.len - 1] == '/') { u64 hash = hash_fnv64(HASH_FNV64_BASIS, parent_dir_name); - parent_entry = fixed_dict_get(&archive.lookup, hash); + parent_entry = dict_get(&archive.lookup, hash); break; } } @@ -165,5 +165,5 @@ struct tar_archive tar_parse(struct arena *arena, struct string data, struct str struct tar_entry *tar_get(const struct tar_archive *archive, struct string name) { u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); - return fixed_dict_get(&archive->lookup, hash); + return dict_get(&archive->lookup, hash); } diff --git a/src/tar.h b/src/tar.h index 444f34e6..2ee9d1a4 100644 --- a/src/tar.h +++ b/src/tar.h @@ -13,7 +13,7 @@ struct tar_entry { }; struct tar_archive { - struct fixed_dict lookup; + struct dict lookup; struct tar_entry *head; }; diff --git a/src/util.h b/src/util.h index bcaead4a..fe3dbc7e 100644 --- a/src/util.h +++ b/src/util.h @@ -100,78 +100,76 @@ INLINE void merge_sort(void *items, u64 item_count, u64 item_size, sort_compare_ } /* ========================== * - * Fixed Dict + * Dict * - * Simple fixed-bin-count string -> pointer chaining hash table for generic use + * Simple chaining hash -> 64 bit value table for generic use * ========================== */ -struct fixed_dict_entry { +struct dict_entry { u64 hash; - void *value; - struct fixed_dict_entry *next; + u64 value; + struct dict_entry *next; }; -struct fixed_dict_bin { - struct fixed_dict_entry *entry_head; +struct dict_bin { + struct dict_entry *entry_head; }; -struct fixed_dict { +struct dict { u64 bins_count; - struct fixed_dict_bin *bins; + struct dict_bin *bins; }; -INLINE struct fixed_dict fixed_dict_init(struct arena *arena, u64 bins_count) +INLINE struct dict dict_init(struct arena *arena, u64 bins_count) { __prof; - struct fixed_dict dict = ZI; + struct dict dict = ZI; bins_count = max_u64(bins_count, 1); /* Ensure at least 1 bin */ dict.bins_count = bins_count; - dict.bins = arena_push_array(arena, struct fixed_dict_bin, bins_count); + dict.bins = arena_push_array(arena, struct dict_bin, bins_count); return dict; } -/* arena and key must share lifetime with dict (this function does not copy the key) */ -INLINE void fixed_dict_set(struct arena *arena, struct fixed_dict *dict, u64 hash, void *value) +INLINE void dict_set(struct arena *arena, struct dict *dict, u64 hash, u64 value) { __prof; - u64 index = hash % dict->bins_count; - struct fixed_dict_bin *bin = &dict->bins[index]; + struct dict_bin *bin = &dict->bins[hash % dict->bins_count]; - struct fixed_dict_entry *entry = bin->entry_head; + struct dict_entry *entry = bin->entry_head; while (entry) { if (hash == entry->hash) { - /* Existing match found, replace its contents */ - entry->value = value; - return; + /* Existing match found */ + break; } entry = entry->next; } /* No match found, create new entry */ - entry = arena_push(arena, struct fixed_dict_entry); - entry->value = value; - entry->hash = hash; - entry->next = bin->entry_head; - - bin->entry_head = entry; -} - -INLINE void *fixed_dict_get(const struct fixed_dict *dict, u64 hash) -{ - __prof; - - u64 index = hash % dict->bins_count; - struct fixed_dict_bin *bin = &dict->bins[index]; - - for (struct fixed_dict_entry *entry = bin->entry_head; entry; entry = entry->next) { - if (hash == entry->hash) { - /* Match found */ - return entry->value; - } + if (!entry) { + entry = arena_push(arena, struct dict_entry); + entry->value = value; + entry->hash = hash; + entry->next = bin->entry_head; + bin->entry_head = entry; } - return NULL; + entry->value = value; +} + +INLINE u64 dict_get(const struct dict *dict, u64 hash) +{ + __prof; + u64 result = 0; + struct dict_bin *bin = &dict->bins[hash % dict->bins_count]; + for (struct dict_entry *entry = bin->entry_head; entry; entry = entry->next) { + if (hash == entry->hash) { + /* Match found */ + result = entry->value; + break; + } + } + return result; } /* ========================== *