From 31179d21c471fb22dfc0439724176516e77a6dd8 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Mon, 25 Nov 2024 16:56:39 +0100 Subject: [PATCH] Less allocation better?! --- CHANGELOG.md | 1 + jecs.luau | 44 ++++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3180c6..eac5cf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog][kac], and this project adheres to - `[world]`: - 16% faster `world:get` + - 22% faster at deleting children - `[typescript]` - Fixed Entity type to default to `undefined | unknown` instead of just `undefined` diff --git a/jecs.luau b/jecs.luau index b455ae7..8d005bb 100644 --- a/jecs.luau +++ b/jecs.luau @@ -1058,37 +1058,40 @@ do local idr = component_index[delete] if idr then - local children = {} - for archetype_id in idr.cache do - local idr_archetype = archetypes[archetype_id] - - for i, child in idr_archetype.entities do - table.insert(children, child) - end - end local flags = idr.flags if bit32.band(flags, ECS_ID_DELETE) ~= 0 then - for _, child in children do - -- Cascade deletion to children - world_delete(world, child) + for archetype_id in idr.cache do + local idr_archetype = archetypes[archetype_id] + + local ents = idr_archetype.entities + local n = #ents + for i = n, 1, -1 do + local child = ents[i] + world_delete(world, child) + end end else - for _, child in children do - world_remove(world, child, delete) + for archetype_id in idr.cache do + local idr_archetype = archetypes[archetype_id] + + local ents = idr_archetype.entities + local n = #ents + for i = n, 1, -1 do + local child = ents[i] + world_remove(world, child, delete) + end end end end if idr_t then for archetype_id in idr_t.cache do - local children = {} local idr_t_archetype = archetypes[archetype_id] local idr_t_types = idr_t_archetype.types - for _, child in idr_t_archetype.entities do - table.insert(children, child) - end + local ents = idr_t_archetype.entities + local n = #ents for _, id in idr_t_types do if not ECS_IS_PAIR(id) then @@ -1100,13 +1103,14 @@ do local flags = id_record.flags local flags_delete_mask: number = bit32.band(flags, ECS_ID_DELETE) if flags_delete_mask ~= 0 then - for _, child in children do - -- Cascade deletions of it has Delete as component trait + for i = n, 1, -1 do + local child = ents[i] world_delete(world, child, destruct) end break else - for _, child in children do + for i = n, 1, -1 do + local child = ents[i] world_remove(world, child, id) end end