rename 'fixed_dict' -> 'dict'

This commit is contained in:
jacob 2025-05-21 14:14:05 -05:00
parent 9e804186c2
commit fa4f750beb
7 changed files with 64 additions and 66 deletions

View File

@ -174,7 +174,7 @@ GLOBAL struct {
struct dx11_shader shaders[NUM_SHADERS]; struct dx11_shader shaders[NUM_SHADERS];
struct dx11_shader_desc shader_info[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); } 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) { for (u64 i = SHADER_NONE + 1; i < ARRAY_COUNT(G.shader_info); ++i) {
struct dx11_shader_desc *desc = &G.shader_info[i]; struct dx11_shader_desc *desc = &G.shader_info[i];
struct string name = string_from_cstr_no_limit(desc->name_cstr); struct string name = string_from_cstr_no_limit(desc->name_cstr);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); 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) INTERNAL RESOURCE_WATCH_CALLBACK_FUNC_DEF(shader_resource_watch_callback, name)
{ {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, 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) { if (desc) {
logf_info("Shader source file \"%F\" has changed", FMT_STR(name)); logf_info("Shader source file \"%F\" has changed", FMT_STR(name));
atomic_i32_eval_exchange(&desc->should_reload, 1); atomic_i32_eval_exchange(&desc->should_reload, 1);

View File

@ -236,15 +236,15 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(resource_watch_dispatcher_thread_entry_
sys_mutex_unlock(&watch_dispatcher_lock); sys_mutex_unlock(&watch_dispatcher_lock);
{ {
__profscope(run_resource_watch_callbacks); __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) { 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 */ /* Do not run callbacks for the same file more than once */
b32 skip = false; b32 skip = false;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, info->name); 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; skip = true;
} else { } else {
fixed_dict_set(temp.arena, &dedup_dict, hash, (void *)1); dict_set(temp.arena, &dedup_dict, hash, 1);
} }
if (!skip) { if (!skip) {
struct sys_lock callbacks_lock = sys_mutex_lock_s(&G.watch_callbacks_mutex); struct sys_lock callbacks_lock = sys_mutex_lock_s(&G.watch_callbacks_mutex);

View File

@ -447,7 +447,7 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str
if (ase.num_spans > 0) { if (ase.num_spans > 0) {
__profscope(init_spans); __profscope(init_spans);
sheet.spans = arena_push_array(arena, struct sprite_sheet_span, sheet.spans_count); 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; u64 index = 0;
for (struct ase_span *ase_span = ase.span_head; ase_span; ase_span = ase_span->next) { 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); 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->start = ase_span->start;
span->end = ase_span->end; span->end = ase_span->end;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); 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; ++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; u64 num_temp_slice_group_nodes = 0;
struct temp_slice_group_node *temp_slice_group_head = NULL; 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) { 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; struct string name = ase_slice_key->name;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, 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) { if (!temp_slice_group_node) {
temp_slice_group_node = arena_push(scratch.arena, struct temp_slice_group_node); temp_slice_group_node = arena_push(scratch.arena, struct temp_slice_group_node);
temp_slice_group_node->name = name; 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; ++num_temp_slice_group_nodes;
temp_slice_group_node->next = temp_slice_group_head; 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 */ /* Allocate slice groups & fill originals in 2d array */
sheet.slice_groups_count = num_temp_slice_group_nodes; 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 = 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; 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) { 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; temp_slice_group_node->final_slice_group = slice_group;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, slice_group->name); 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; ++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; struct string point_slice_name = ray_slice_name;
point_slice_name.len -= ray_suffix.len; point_slice_name.len -= ray_suffix.len;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, point_slice_name); 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) { if (point_slice_group) {
u32 point_slices_per_frame = point_slice_group->per_frame_count; 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; struct sprite_sheet_span res = ZI;
if (sheet->spans_count > 0) { if (sheet->spans_count > 0) {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); 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) { if (entry) {
res = *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) { if (sheet->slice_groups_count > 0) {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); 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) { if (group) {
return group->frame_slices[frame_index * group->per_frame_count]; 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; struct sprite_sheet_slice_array res = ZI;
if (sheet->slice_groups_count > 0) { if (sheet->slice_groups_count > 0) {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); 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) { if (group) {
res.count = group->per_frame_count; res.count = group->per_frame_count;
res.slices = &group->frame_slices[frame_index * group->per_frame_count]; res.slices = &group->frame_slices[frame_index * group->per_frame_count];

View File

@ -71,11 +71,11 @@ struct sprite_sheet {
u32 spans_count; u32 spans_count;
struct sprite_sheet_span *spans; struct sprite_sheet_span *spans;
struct fixed_dict spans_dict; struct dict spans_dict;
u32 slice_groups_count; u32 slice_groups_count;
struct sprite_sheet_slice_group *slice_groups; 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); struct sprite_sheet *sprite_sheet_from_tag_await(struct sprite_scope *scope, struct sprite_tag tag);

View File

@ -130,10 +130,10 @@ struct tar_archive tar_parse(struct arena *arena, struct string data, struct str
} }
/* Build lookup table */ /* 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) { for (struct tar_entry *entry = archive.head; entry; entry = entry->next) {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, entry->file_name); 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 */ /* 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) { 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] == '/') { if (parent_dir_name.text[parent_dir_name.len - 1] == '/') {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, parent_dir_name); 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; 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) struct tar_entry *tar_get(const struct tar_archive *archive, struct string name)
{ {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name); u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
return fixed_dict_get(&archive->lookup, hash); return dict_get(&archive->lookup, hash);
} }

View File

@ -13,7 +13,7 @@ struct tar_entry {
}; };
struct tar_archive { struct tar_archive {
struct fixed_dict lookup; struct dict lookup;
struct tar_entry *head; struct tar_entry *head;
}; };

View File

@ -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; u64 hash;
void *value; u64 value;
struct fixed_dict_entry *next; struct dict_entry *next;
}; };
struct fixed_dict_bin { struct dict_bin {
struct fixed_dict_entry *entry_head; struct dict_entry *entry_head;
}; };
struct fixed_dict { struct dict {
u64 bins_count; 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; __prof;
struct fixed_dict dict = ZI; struct dict dict = ZI;
bins_count = max_u64(bins_count, 1); /* Ensure at least 1 bin */ bins_count = max_u64(bins_count, 1); /* Ensure at least 1 bin */
dict.bins_count = bins_count; 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; return dict;
} }
/* arena and key must share lifetime with dict (this function does not copy the key) */ INLINE void dict_set(struct arena *arena, struct dict *dict, u64 hash, u64 value)
INLINE void fixed_dict_set(struct arena *arena, struct fixed_dict *dict, u64 hash, void *value)
{ {
__prof; __prof;
u64 index = hash % dict->bins_count; struct dict_bin *bin = &dict->bins[hash % dict->bins_count];
struct fixed_dict_bin *bin = &dict->bins[index];
struct fixed_dict_entry *entry = bin->entry_head; struct dict_entry *entry = bin->entry_head;
while (entry) { while (entry) {
if (hash == entry->hash) { if (hash == entry->hash) {
/* Existing match found, replace its contents */ /* Existing match found */
entry->value = value; break;
return;
} }
entry = entry->next; entry = entry->next;
} }
/* No match found, create new entry */ /* No match found, create new entry */
entry = arena_push(arena, struct fixed_dict_entry); if (!entry) {
entry = arena_push(arena, struct dict_entry);
entry->value = value; entry->value = value;
entry->hash = hash; entry->hash = hash;
entry->next = bin->entry_head; entry->next = bin->entry_head;
bin->entry_head = entry; bin->entry_head = entry;
}
entry->value = value;
} }
INLINE void *fixed_dict_get(const struct fixed_dict *dict, u64 hash) INLINE u64 dict_get(const struct dict *dict, u64 hash)
{ {
__prof; __prof;
u64 result = 0;
u64 index = hash % dict->bins_count; struct dict_bin *bin = &dict->bins[hash % dict->bins_count];
struct fixed_dict_bin *bin = &dict->bins[index]; for (struct dict_entry *entry = bin->entry_head; entry; entry = entry->next) {
for (struct fixed_dict_entry *entry = bin->entry_head; entry; entry = entry->next) {
if (hash == entry->hash) { if (hash == entry->hash) {
/* Match found */ /* Match found */
return entry->value; result = entry->value;
break;
} }
} }
return result;
return NULL;
} }
/* ========================== * /* ========================== *