pass hash directly into fixed dict funtions

This commit is contained in:
jacob 2025-05-21 14:03:46 -05:00
parent 6d561010cf
commit 9e804186c2
9 changed files with 149 additions and 53 deletions

View File

@ -244,7 +244,8 @@ INTERNAL void init_shader_table(void)
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);
fixed_dict_set(&G.arena, &G.shader_info_lookup, name, desc);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
fixed_dict_set(&G.arena, &G.shader_info_lookup, hash, desc);
}
}
@ -388,7 +389,8 @@ INTERNAL void reload_shader(struct dx11_shader *old_shader, struct dx11_shader_d
#if RESOURCE_RELOADING
INTERNAL RESOURCE_WATCH_CALLBACK_FUNC_DEF(shader_resource_watch_callback, name)
{
struct dx11_shader_desc *desc = (struct dx11_shader_desc *)fixed_dict_get(&G.shader_info_lookup, 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);
if (desc) {
logf_info("Shader source file \"%F\" has changed", FMT_STR(name));
atomic_i32_eval_exchange(&desc->should_reload, 1);

View File

@ -240,10 +240,11 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(resource_watch_dispatcher_thread_entry_
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;
if ((u64)fixed_dict_get(&dedup_dict, info->name) == 1) {
u64 hash = hash_fnv64(HASH_FNV64_BASIS, info->name);
if ((u64)fixed_dict_get(&dedup_dict, hash) == 1) {
skip = true;
} else {
fixed_dict_set(temp.arena, &dedup_dict, info->name, (void *)1);
fixed_dict_set(temp.arena, &dedup_dict, hash, (void *)1);
}
if (!skip) {
struct sys_lock callbacks_lock = sys_mutex_lock_s(&G.watch_callbacks_mutex);

View File

@ -145,9 +145,10 @@ enum sim_control_flag {
SIM_CONTROL_FLAG_SPAWN1_TEST = 1 << 5,
SIM_CONTROL_FLAG_SPAWN2_TEST = 1 << 6,
SIM_CONTROL_FLAG_SPAWN3_TEST = 1 << 7,
SIM_CONTROL_FLAG_TILE_TEST = 1 << 8,
SIM_CONTROL_FLAG_EXPLODE_TEST = 1 << 9,
SIM_CONTROL_FLAG_TELEPORT_TEST = 1 << 10,
SIM_CONTROL_FLAG_WALLS_TEST = 1 << 8,
SIM_CONTROL_FLAG_TILE_TEST = 1 << 9,
SIM_CONTROL_FLAG_EXPLODE_TEST = 1 << 10,
SIM_CONTROL_FLAG_TELEPORT_TEST = 1 << 11,
};
struct sim_control {

View File

@ -51,7 +51,7 @@ void sim_accel_reset(struct sim_snapshot *ss, struct sim_accel *accel)
/* TODO: Remove this */
INTERNAL struct sim_ent *spawn_test_smg(struct sim_ent *parent)
INTERNAL struct sim_ent *test_spawn_smg(struct sim_ent *parent)
{
struct sim_ent *e = sim_ent_alloc_sync_src(parent);
e->sprite = sprite_tag_from_path(LIT("res/graphics/gun.ase"));
@ -67,7 +67,7 @@ INTERNAL struct sim_ent *spawn_test_smg(struct sim_ent *parent)
return e;
}
INTERNAL struct sim_ent *spawn_test_launcher(struct sim_ent *parent)
INTERNAL struct sim_ent *test_spawn_launcher(struct sim_ent *parent)
{
struct sim_ent *e = sim_ent_alloc_sync_src(parent);
e->sprite = sprite_tag_from_path(LIT("res/graphics/gun.ase"));
@ -83,7 +83,7 @@ INTERNAL struct sim_ent *spawn_test_launcher(struct sim_ent *parent)
return e;
}
INTERNAL struct sim_ent *spawn_test_chucker(struct sim_ent *parent)
INTERNAL struct sim_ent *test_spawn_chucker(struct sim_ent *parent)
{
struct sim_ent *chucker = sim_ent_alloc_sync_src(parent);
chucker->sprite = sprite_tag_from_path(LIT("res/graphics/gun.ase"));
@ -118,7 +118,7 @@ INTERNAL struct sim_ent *spawn_test_chucker(struct sim_ent *parent)
return chucker;
}
INTERNAL struct sim_ent *spawn_test_employee(struct sim_ent *parent)
INTERNAL struct sim_ent *test_spawn_employee(struct sim_ent *parent)
{
/* Player */
struct sim_ent *employee = sim_ent_nil();
@ -177,18 +177,18 @@ INTERNAL struct sim_ent *spawn_test_employee(struct sim_ent *parent)
/* Player weapon */
if (employee->valid) {
(UNUSED)spawn_test_smg;
(UNUSED)spawn_test_launcher;
(UNUSED)spawn_test_chucker;
(UNUSED)test_spawn_smg;
(UNUSED)test_spawn_launcher;
(UNUSED)test_spawn_chucker;
struct sim_ent *e = spawn_test_chucker(employee);
struct sim_ent *e = test_spawn_chucker(employee);
employee->equipped = e->id;
}
return employee;
}
INTERNAL struct sim_ent *spawn_test_camera(struct sim_ent *parent, struct sim_ent *follow)
INTERNAL struct sim_ent *test_spawn_camera(struct sim_ent *parent, struct sim_ent *follow)
{
struct sim_ent *camera_ent = sim_ent_nil();
if (follow->valid) {
@ -207,7 +207,7 @@ INTERNAL struct sim_ent *spawn_test_camera(struct sim_ent *parent, struct sim_en
return camera_ent;
}
INTERNAL struct sim_ent *spawn_test_explosion(struct sim_ent *parent, struct v2 pos, f32 strength, f32 radius)
INTERNAL struct sim_ent *test_spawn_explosion(struct sim_ent *parent, struct v2 pos, f32 strength, f32 radius)
{
struct sim_ent *ent = sim_ent_alloc_sync_src(parent);
sim_ent_set_xform(ent, XFORM_POS(pos));
@ -231,20 +231,20 @@ INTERNAL void test_teleport(struct sim_ent *ent, struct v2 pos)
sim_ent_set_xform(ent, xf);
}
INTERNAL void spawn_test_entities1(struct sim_ent *parent, struct v2 pos)
INTERNAL void test_spawn_entities1(struct sim_ent *parent, struct v2 pos)
{
(UNUSED)pos;
/* Enemy */
{
struct sim_ent *e = spawn_test_employee(parent);
struct sim_ent *e = test_spawn_employee(parent);
struct xform xf = sim_ent_get_xform(e);
xf.og = pos;
sim_ent_set_xform(e, xf);
}
}
INTERNAL void spawn_test_entities2(struct sim_ent *parent, struct v2 pos)
INTERNAL void test_spawn_entities2(struct sim_ent *parent, struct v2 pos)
{
(UNUSED)pos;
@ -302,7 +302,7 @@ INTERNAL void spawn_test_entities2(struct sim_ent *parent, struct v2 pos)
#endif
}
INTERNAL void spawn_test_entities3(struct sim_ent *parent, struct v2 pos)
INTERNAL void test_spawn_entities3(struct sim_ent *parent, struct v2 pos)
{
(UNUSED)pos;
@ -327,7 +327,7 @@ INTERNAL void spawn_test_entities3(struct sim_ent *parent, struct v2 pos)
}
}
INTERNAL void spawn_test_tile(struct sim_ent *parent, struct v2 cursor_pos)
INTERNAL void test_spawn_tile(struct sim_ent *parent, struct v2 cursor_pos)
{
struct sim_ent *e = sim_ent_alloc_sync_src(parent);
@ -359,9 +359,86 @@ INTERNAL void spawn_test_tile(struct sim_ent *parent, struct v2 cursor_pos)
sim_ent_enable_prop(e, SEPROP_SOLID);
struct quad collider_quad = quad_from_rect(RECT(-tile_size.x / 2, -tile_size.y / 2, tile_size.y, tile_size.y));
e->local_collider = collider_from_quad(collider_quad);
}
/* TODO: Move this */
enum tile_kind {
TILE_KIND_NONE,
TILE_KIND_WALL
};
INTERNAL void test_generate_walls(struct sim_ent *parent)
{
#if 0
struct temp_arena scratch = scratch_begin_no_conflict();
struct sim_snapshot *world = parent->ss;
/* Release existing walls */
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
struct sim_ent *ent = &world->ents[ent_index];
if (!ent->valid) continue;
if (sim_ent_has_prop(ent, SEPROP_WALL)) {
sim_ent_enable_prop(ent, SEPROP_RELEASE);
}
}
/* Sort tile chunks */
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
struct sim_ent *ent = &world->ents[ent_index];
if (!ent->valid) continue;
if (sim_ent_has_prop(ent, SEPROP_TILE_CHUNK)) {
}
}
/* Generate horizontal walls */
for (u64 ent_index = 0; ent_index < world->num_ents_reserved; ++ent_index) {
struct sim_ent *ent = &world->ents[ent_index];
if (!ent->valid) continue;
if (sim_ent_has_prop(ent, SEPROP_TILE_CHUNK)) {
//struct v2i32 chunk_start = ent->tile_chunk_start;
//struct v2i32 chunk_end = v2_add(chunk_start, V2(SIM_TILES_PER_CHUNK_SQRT, SIM_TILES_PER_CHUNK_SQRT));
//struct sim_ent *left_chunk = sim_chunk_from_index(world, v2i32(chunk_start.x - 1, chunk_start.y));
//struct sim_ent *right_chunk = sim_chunk_from_index(world, v2i32(chunk_end.x, chunk_start.y));
//struct sim_ent *top_chunk = sim_chunk_from_index(world, v2i32(chunk_start.x, chunk_start.y - 1);
//struct sim_ent *bottom_chunk = sim_chunk_from_index(world, v2i32(chunk_start.x, chunk_start.y));
for (i64 tile_y = 0; tile_y < SIM_TILES_PER_CHUNK_SQRT; ++tile_y) {
i64 tile_x = 0;
while (true) {
}
}
}
}
scratch_end(scratch);
#else
(UNUSED)parent;
#endif
}
INTERNAL void test_clear_level(struct sim_step_ctx *ctx)
{
struct sim_snapshot *world = ctx->world;
@ -439,7 +516,7 @@ INTERNAL PHYS_COLLISION_CALLBACK_FUNC_DEF(on_collision, data, step_ctx)
/* Create explosion */
if (bullet->bullet_explosion_strength > 0) {
spawn_test_explosion(root, point, bullet->bullet_explosion_strength, bullet->bullet_explosion_radius);
test_spawn_explosion(root, point, bullet->bullet_explosion_strength, bullet->bullet_explosion_radius);
}
/* Update bullet */
@ -695,38 +772,41 @@ void sim_step(struct sim_step_ctx *ctx)
test_clear_level(ctx);
}
if (flags & SIM_CONTROL_FLAG_SPAWN1_TEST) {
logf_info("Spawn1 (test)");
logf_info("Spawn test 1");
u32 count = 1;
f32 spread = 0;
for (u32 j = 0; j < count; ++j) {
struct v2 pos = player->player_cursor_pos;
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
spawn_test_entities1(root, pos);
test_spawn_entities1(root, pos);
}
}
if (flags & SIM_CONTROL_FLAG_SPAWN2_TEST) {
logf_info("Spawn1 (test)");
logf_info("Spawn test 2");
u32 count = 1;
f32 spread = 0;
for (u32 j = 0; j < count; ++j) {
struct v2 pos = player->player_cursor_pos;
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
spawn_test_entities2(root, pos);
test_spawn_entities2(root, pos);
}
}
if (flags & SIM_CONTROL_FLAG_SPAWN3_TEST) {
logf_info("Spawn1 (test)");
logf_info("Spawn test 3");
u32 count = 1;
f32 spread = 0;
for (u32 j = 0; j < count; ++j) {
struct v2 pos = player->player_cursor_pos;
pos.y += (((f32)j / (f32)count) - 0.5) * spread;
spawn_test_entities3(root, pos);
test_spawn_entities3(root, pos);
}
}
if (flags & SIM_CONTROL_FLAG_WALLS_TEST) {
test_generate_walls(root);
}
if (flags & SIM_CONTROL_FLAG_EXPLODE_TEST) {
logf_info("Explosion (test)");
spawn_test_explosion(root, player->player_cursor_pos, 100, 2);
logf_info("Explosion test");
test_spawn_explosion(root, player->player_cursor_pos, 100, 2);
}
}
@ -760,7 +840,7 @@ void sim_step(struct sim_step_ctx *ctx)
sim_ent_set_tile_chunk_data(chunk_ent, new_data);
}
#else
spawn_test_tile(root, player->player_cursor_pos);
test_spawn_tile(root, player->player_cursor_pos);
#endif
}
}
@ -823,7 +903,7 @@ void sim_step(struct sim_step_ctx *ctx)
/* FIXME: Ents never released when client disconnects */
struct sim_ent *control_ent = sim_ent_from_id(world, ent->player_control_ent);
if (!control_ent->valid) {
control_ent = spawn_test_employee(root);
control_ent = test_spawn_employee(root);
control_ent->predictor = ent->id;
sim_ent_enable_prop(control_ent, SEPROP_CONTROLLED);
ent->player_control_ent = control_ent->id;
@ -831,7 +911,7 @@ void sim_step(struct sim_step_ctx *ctx)
}
struct sim_ent *camera_ent = sim_ent_from_id(world, ent->player_camera_ent);
if (!camera_ent->valid) {
camera_ent = spawn_test_camera(root, control_ent);
camera_ent = test_spawn_camera(root, control_ent);
camera_ent->predictor = ent->id;
ent->player_camera_ent = camera_ent->id;
}

View File

@ -455,7 +455,8 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str
span->name = name;
span->start = ase_span->start;
span->end = ase_span->end;
fixed_dict_set(arena, &sheet.spans_dict, name, span);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
fixed_dict_set(arena, &sheet.spans_dict, hash, span);
++index;
}
}
@ -489,12 +490,12 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str
struct fixed_dict temp_slice_dict = fixed_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;
struct temp_slice_group_node *temp_slice_group_node = fixed_dict_get(&temp_slice_dict, 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);
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, name, temp_slice_group_node);
fixed_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;
@ -577,7 +578,8 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str
}
temp_slice_group_node->final_slice_group = slice_group;
fixed_dict_set(arena, &sheet.slice_groups_dict, slice_group->name, slice_group);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, slice_group->name);
fixed_dict_set(arena, &sheet.slice_groups_dict, hash, slice_group);
++index;
}
@ -625,8 +627,8 @@ INTERNAL struct sprite_sheet init_sheet_from_ase_result(struct arena *arena, str
if (string_ends_with(ray_slice_name, ray_suffix)) {
struct string point_slice_name = ray_slice_name;
point_slice_name.len -= ray_suffix.len;
struct sprite_sheet_slice_group *point_slice_group = fixed_dict_get(&sheet.slice_groups_dict, 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);
if (point_slice_group) {
u32 point_slices_per_frame = point_slice_group->per_frame_count;
@ -1061,7 +1063,8 @@ struct sprite_sheet_span sprite_sheet_get_span(struct sprite_sheet *sheet, struc
__prof;
struct sprite_sheet_span res = ZI;
if (sheet->spans_count > 0) {
struct sprite_sheet_span *entry = fixed_dict_get(&sheet->spans_dict, name);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
struct sprite_sheet_span *entry = fixed_dict_get(&sheet->spans_dict, hash);
if (entry) {
res = *entry;
}
@ -1072,7 +1075,8 @@ struct sprite_sheet_span sprite_sheet_get_span(struct sprite_sheet *sheet, struc
struct sprite_sheet_slice sprite_sheet_get_slice(struct sprite_sheet *sheet, struct string name, u32 frame_index)
{
if (sheet->slice_groups_count > 0) {
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, hash);
if (group) {
return group->frame_slices[frame_index * group->per_frame_count];
}
@ -1097,7 +1101,8 @@ 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) {
struct sprite_sheet_slice_group *group = fixed_dict_get(&sheet->slice_groups_dict, name);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
struct sprite_sheet_slice_group *group = fixed_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];

View File

@ -132,7 +132,8 @@ 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));
for (struct tar_entry *entry = archive.head; entry; entry = entry->next) {
fixed_dict_set(arena, &archive.lookup, entry->file_name, entry);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, entry->file_name);
fixed_dict_set(arena, &archive.lookup, hash, entry);
}
/* Build hierarchy */
@ -145,7 +146,8 @@ struct tar_archive tar_parse(struct arena *arena, struct string data, struct str
struct tar_entry *parent_entry = NULL;
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] == '/') {
parent_entry = fixed_dict_get(&archive.lookup, parent_dir_name);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, parent_dir_name);
parent_entry = fixed_dict_get(&archive.lookup, hash);
break;
}
}
@ -162,5 +164,6 @@ 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)
{
return fixed_dict_get(&archive->lookup, name);
u64 hash = hash_fnv64(HASH_FNV64_BASIS, name);
return fixed_dict_get(&archive->lookup, hash);
}

View File

@ -154,6 +154,7 @@ GLOBAL READONLY enum user_bind_kind g_binds[SYS_BTN_COUNT] = {
[SYS_BTN_1] = USER_BIND_KIND_DEBUG_SPAWN1,
[SYS_BTN_2] = USER_BIND_KIND_DEBUG_SPAWN2,
[SYS_BTN_3] = USER_BIND_KIND_DEBUG_SPAWN3,
[SYS_BTN_G] = USER_BIND_KIND_DEBUG_WALLS,
[SYS_BTN_N] = USER_BIND_KIND_DEBUG_STEP,
[SYS_BTN_Q] = USER_BIND_KIND_DEBUG_FOLLOW,
[SYS_BTN_F1] = USER_BIND_KIND_DEBUG_PAUSE,
@ -1631,6 +1632,7 @@ INTERNAL void user_update(void)
struct bind_state spawn1_state = G.bind_states[USER_BIND_KIND_DEBUG_SPAWN1];
struct bind_state spawn2_state = G.bind_states[USER_BIND_KIND_DEBUG_SPAWN2];
struct bind_state spawn3_state = G.bind_states[USER_BIND_KIND_DEBUG_SPAWN3];
struct bind_state walls_state = G.bind_states[USER_BIND_KIND_DEBUG_WALLS];
struct bind_state pause_state = G.bind_states[USER_BIND_KIND_DEBUG_PAUSE];
struct bind_state step_state = G.bind_states[USER_BIND_KIND_DEBUG_STEP];
struct bind_state tile_state = G.bind_states[USER_BIND_KIND_TILE_TEST];
@ -1661,6 +1663,9 @@ INTERNAL void user_update(void)
if (spawn3_state.num_presses_and_repeats) {
control.flags |= SIM_CONTROL_FLAG_SPAWN3_TEST;
}
if (walls_state.num_presses_and_repeats) {
control.flags |= SIM_CONTROL_FLAG_WALLS_TEST;
}
if (tile_state.num_presses_and_repeats) {
control.flags |= SIM_CONTROL_FLAG_TILE_TEST;
}

View File

@ -32,6 +32,7 @@ enum user_bind_kind {
USER_BIND_KIND_DEBUG_SPAWN1,
USER_BIND_KIND_DEBUG_SPAWN2,
USER_BIND_KIND_DEBUG_SPAWN3,
USER_BIND_KIND_DEBUG_WALLS,
USER_BIND_KIND_DEBUG_FOLLOW,
USER_BIND_KIND_DEBUG_DRAW,
USER_BIND_KIND_DEBUG_CAMERA,

View File

@ -106,8 +106,8 @@ INLINE void merge_sort(void *items, u64 item_count, u64 item_size, sort_compare_
* ========================== */
struct fixed_dict_entry {
void *value;
u64 hash;
void *value;
struct fixed_dict_entry *next;
};
@ -131,11 +131,10 @@ INLINE struct fixed_dict fixed_dict_init(struct arena *arena, u64 bins_count)
}
/* 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, struct string key, void *value)
INLINE void fixed_dict_set(struct arena *arena, struct fixed_dict *dict, u64 hash, void *value)
{
__prof;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, key);
u64 index = hash % dict->bins_count;
struct fixed_dict_bin *bin = &dict->bins[index];
@ -158,11 +157,10 @@ INLINE void fixed_dict_set(struct arena *arena, struct fixed_dict *dict, struct
bin->entry_head = entry;
}
INLINE void *fixed_dict_get(const struct fixed_dict *dict, struct string key)
INLINE void *fixed_dict_get(const struct fixed_dict *dict, u64 hash)
{
__prof;
u64 hash = hash_fnv64(HASH_FNV64_BASIS, key);
u64 index = hash % dict->bins_count;
struct fixed_dict_bin *bin = &dict->bins[index];