From 5efd9ab2ae0c24c031d07e8485e604a1c14610ea Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 6 Feb 2026 07:57:26 +0700 Subject: [PATCH] ensure that entity_chunks and entity_places have only array-parts --- evolved.lua | 66 +++++++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 45 deletions(-) diff --git a/evolved.lua b/evolved.lua index 710b72a..fe4622b 100644 --- a/evolved.lua +++ b/evolved.lua @@ -141,8 +141,8 @@ local __minor_chunks = {} ---@type table> local __major_queries = {} ---@type table> -local __entity_chunks = {} ---@type table -local __entity_places = {} ---@type table +local __entity_chunks = {} ---@type (evolved.chunk|false)[] +local __entity_places = {} ---@type integer[] local __sorted_includes = {} ---@type table> local __sorted_excludes = {} ---@type table> @@ -674,6 +674,9 @@ local function __acquire_id() local acquired_id = acquired_primary + shifted_secondary freelist_ids[acquired_primary] = acquired_id + __entity_chunks[acquired_primary] = false + __entity_places[acquired_primary] = 0 + return acquired_id --[[@as evolved.id]] end end @@ -875,28 +878,6 @@ end --- --- -local __table_dup - ----@generic K, V ----@param table table ----@return table ----@nodiscard -function __table_dup(table) - local dup_table = {} - - for k, v in __lua_next, table do - dup_table[k] = v - end - - return dup_table -end - ---- ---- ---- ---- ---- - ---@class (exact) evolved.assoc_list: { --- __item_set: { [K]: integer }, --- __item_list: K[], @@ -3364,8 +3345,8 @@ function __clear_entity_one(entity) if chunk then __detach_entity(chunk, place) - entity_chunks[entity_primary] = nil - entity_places[entity_primary] = nil + entity_chunks[entity_primary] = false + entity_places[entity_primary] = 0 __structural_changes = __structural_changes + 1 end @@ -3431,8 +3412,8 @@ function __destroy_entity_one(entity) if chunk then __detach_entity(chunk, place) - entity_chunks[entity_primary] = nil - entity_places[entity_primary] = nil + entity_chunks[entity_primary] = false + entity_places[entity_primary] = 0 __structural_changes = __structural_changes + 1 end @@ -4144,8 +4125,8 @@ function __chunk_remove(old_chunk, ...) for old_place = 1, old_entity_count do local entity = old_entity_list[old_place] local entity_primary = entity % 2 ^ 20 - entity_chunks[entity_primary] = nil - entity_places[entity_primary] = nil + entity_chunks[entity_primary] = false + entity_places[entity_primary] = 0 end __detach_all_entities(old_chunk) @@ -4207,8 +4188,8 @@ function __chunk_clear(chunk) for place = 1, chunk_entity_count do local entity = chunk_entity_list[place] local entity_primary = entity % 2 ^ 20 - entity_chunks[entity_primary] = nil - entity_places[entity_primary] = nil + entity_chunks[entity_primary] = false + entity_places[entity_primary] = 0 end __detach_all_entities(chunk) @@ -5255,11 +5236,11 @@ function __evolved_set(entity, fragment, component) local old_chunk = entity_chunks[entity_primary] local old_place = entity_places[entity_primary] - local new_chunk = __chunk_with_fragment(old_chunk, fragment) + local new_chunk = __chunk_with_fragment(old_chunk or nil, fragment) __evolved_defer() - if old_chunk == new_chunk then + if old_chunk and old_chunk == new_chunk then local old_component_indices = old_chunk.__component_indices local old_component_storages = old_chunk.__component_storages @@ -5479,16 +5460,11 @@ function __evolved_remove(entity, ...) local old_chunk = entity_chunks[entity_primary] local old_place = entity_places[entity_primary] - local new_chunk = __chunk_without_fragments(old_chunk, ...) - - if old_chunk == new_chunk then - -- nothing to remove - return - end + local new_chunk = __chunk_without_fragments(old_chunk or nil, ...) __evolved_defer() - do + if old_chunk and old_chunk ~= new_chunk then local old_fragment_list = old_chunk.__fragment_list local old_fragment_count = old_chunk.__fragment_count local old_component_indices = old_chunk.__component_indices @@ -5550,8 +5526,8 @@ function __evolved_remove(entity, ...) do __detach_entity(old_chunk, old_place) - entity_chunks[entity_primary] = new_chunk - entity_places[entity_primary] = new_chunk and new_chunk.__entity_count + entity_chunks[entity_primary] = new_chunk or false + entity_places[entity_primary] = new_chunk and new_chunk.__entity_count or 0 __structural_changes = __structural_changes + 1 end @@ -6267,8 +6243,8 @@ function __evolved_collect_garbage(no_shrink) end do - __entity_chunks = __table_dup(__entity_chunks) - __entity_places = __table_dup(__entity_places) + __entity_chunks = __list_dup(__entity_chunks, __acquired_count) + __entity_places = __list_dup(__entity_places, __acquired_count) end do