From 13ff5097dba3a1d506b0563ade5839ca8551532b Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 26 May 2024 18:03:57 -0700 Subject: [PATCH] module: fix potential archetype hash collision lookup issues Signed-off-by: Stephen Gutekanst --- src/module/entities.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/module/entities.zig b/src/module/entities.zig index 83a3aa88..76c5036a 100644 --- a/src/module/entities.zig +++ b/src/module/entities.zig @@ -196,7 +196,15 @@ pub fn Database(comptime modules: anytype) type { if (entities.buckets[bucket_index]) |bucket| { // Bucket already exists var archetype = &entities.archetypes.items[bucket]; - if (archetype.next) |_| { + var archetype_index: usize = bucket; + if (archetype.next != null) { + if (archetype.hash == hash) { + // Probably a match + // TODO: technically a hash collision could occur here, so maybe check + // column IDs are equal here too? + return .{ .found_existing = true, .hash = hash, .index = bucket, .ptr = archetype }; + } + // Multiple archetypes in bucket (there were collisions) while (archetype.next) |collision_index| { const collision = &entities.archetypes.items[collision_index]; @@ -207,10 +215,12 @@ pub fn Database(comptime modules: anytype) type { return .{ .found_existing = true, .hash = hash, .index = collision_index, .ptr = collision }; } archetype = collision; + archetype_index = collision_index; } // New collision try entities.archetypes.append(entities.allocator, undefined); + archetype = &entities.archetypes.items[archetype_index]; // handle potential pointer invalidation const index = entities.archetypes.items.len - 1; const ptr = &entities.archetypes.items[index]; archetype.next = @intCast(index); @@ -222,6 +232,7 @@ pub fn Database(comptime modules: anytype) type { // New collision try entities.archetypes.append(entities.allocator, undefined); + archetype = &entities.archetypes.items[archetype_index]; // handle potential pointer invalidation const index = entities.archetypes.items.len - 1; const ptr = &entities.archetypes.items[index]; archetype.next = @intCast(index);