From a6c53c9de8fce0924009448f32e9568eb1931773 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Sat, 19 Apr 2025 19:32:28 +0200 Subject: [PATCH 1/4] Add component registration and metadata API --- jecs.luau | 238 ++++++++++++++++++++++++++++++------------------ test/tests.luau | 40 ++++++-- 2 files changed, 179 insertions(+), 99 deletions(-) diff --git a/jecs.luau b/jecs.luau index ce9e63d..b7dae17 100644 --- a/jecs.luau +++ b/jecs.luau @@ -116,13 +116,47 @@ local ECS_ID_MASK = 0b00 local ECS_ENTITY_MASK = bit32.lshift(1, 24) local ECS_GENERATION_MASK = bit32.lshift(1, 16) -local NULL_ARRAY = table.freeze({}) +local NULL_ARRAY = table.freeze({}) :: Column +local NULL = newproxy(false) + local ECS_INTERNAL_ERROR = [[ This is an internal error, please file a bug report via the following link: https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md ]] +local ecs_metadata: Map> = {} +local ecs_max_component_id = 0 +local ecs_max_tag_id = EcsRest + +local function ECS_COMPONENT() + ecs_max_component_id += 1 + if ecs_max_component_id > HI_COMPONENT_ID then + error("Too many components") + end + return ecs_max_component_id +end + +local function ECS_TAG() + ecs_max_tag_id += 1 + return ecs_max_tag_id +end + +local function ECS_META(id: i53, ty: i53, value: any?) + local bundle = ecs_metadata[id] + if bundle then + bundle = {} + ecs_metadata[id] = bundle + end + bundle[ty] = if value == nil then NULL else value +end + +local function ECS_META_RESET() + ecs_metadata = {} + ecs_max_component_id = 0 + ecs_max_tag_id = EcsRest +end + local function ECS_COMBINE(id: number, generation: number): i53 return id + (generation * ECS_ENTITY_MASK) end @@ -529,62 +563,64 @@ end local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t local component_index = world.component_index local entity_index = world.entity_index - local idr: ecs_id_record_t = component_index[id] + local idr: ecs_id_record_t? = component_index[id] - if not idr then - local flags = ECS_ID_MASK - local relation = id - local target = 0 - local is_pair = ECS_IS_PAIR(id) - if is_pair then - relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53 - assert(relation and entity_index_is_alive( - entity_index, relation), ECS_INTERNAL_ERROR) - target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53 - assert(target and entity_index_is_alive( - entity_index, target), ECS_INTERNAL_ERROR) - end - - local cleanup_policy = world_target(world, relation, EcsOnDelete, 0) - local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0) - - local has_delete = false - - if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then - has_delete = true - end - - local on_add, on_change, on_remove = world_get(world, - relation, EcsOnAdd, EcsOnChange, EcsOnRemove) - - local is_tag = not world_has_one_inline(world, - relation, EcsComponent) - - if is_tag and is_pair then - is_tag = not world_has_one_inline(world, target, EcsComponent) - end - - flags = bit32.bor( - flags, - if has_delete then ECS_ID_DELETE else 0, - if is_tag then ECS_ID_IS_TAG else 0 - ) - - idr = { - size = 0, - cache = {}, - counts = {}, - flags = flags, - hooks = { - on_add = on_add, - on_change = on_change, - on_remove = on_remove, - }, - } - - component_index[id] = idr + if idr then + return idr end + local flags = ECS_ID_MASK + local relation = id + local target = 0 + local is_pair = ECS_IS_PAIR(id) + if is_pair then + relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53 + assert(relation and entity_index_is_alive( + entity_index, relation), ECS_INTERNAL_ERROR) + target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53 + assert(target and entity_index_is_alive( + entity_index, target), ECS_INTERNAL_ERROR) + end + + local cleanup_policy = world_target(world, relation, EcsOnDelete, 0) + local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0) + + local has_delete = false + + if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then + has_delete = true + end + + local on_add, on_change, on_remove = world_get(world, + relation, EcsOnAdd, EcsOnChange, EcsOnRemove) + + local is_tag = not world_has_one_inline(world, + relation, EcsComponent) + + if is_tag and is_pair then + is_tag = not world_has_one_inline(world, target, EcsComponent) + end + + flags = bit32.bor( + flags, + if has_delete then ECS_ID_DELETE else 0, + if is_tag then ECS_ID_IS_TAG else 0 + ) + + idr = { + size = 0, + cache = {}, + counts = {}, + flags = flags, + hooks = { + on_add = on_add, + on_change = on_change, + on_remove = on_remove, + }, + } :: ecs_id_record_t + + component_index[id] = idr + return idr end @@ -621,7 +657,7 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: local columns = (table.create(length) :: any) :: { Column } local records: { number } = {} - local counts: {number} = {} + local counts: { number } = {} local archetype: ecs_archetype_t = { columns = columns, @@ -670,7 +706,7 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: world.archetype_index[ty] = archetype world.archetypes[archetype_id] = archetype - world.archetype_edges[archetype.id] = {} + world.archetype_edges[archetype.id] = {} :: Map return archetype end @@ -747,17 +783,17 @@ local function archetype_traverse_remove( local edges = world.archetype_edges local edge = edges[from.id] - local to = edge[id] - if not to then + local to: ecs_archetype_t = edge[id] + if to == nil then to = find_archetype_without(world, from, id) edge[id] = to edges[to.id][id] = from end - return to :: ecs_archetype_t + return to end -local function find_archetype_with(world, id, from) +local function find_archetype_with(world, id, from): ecs_archetype_t local id_types = from.types local at = find_insert(id_types, id) @@ -767,7 +803,7 @@ local function find_archetype_with(world, id, from) return archetype_ensure(world, dst) end -local function archetype_traverse_add(world, id, from: ecs_archetype_t) +local function archetype_traverse_add(world, id, from: ecs_archetype_t): ecs_archetype_t from = from or world.ROOT_ARCHETYPE if from.records[id] then return from @@ -830,7 +866,7 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown local idr_hooks = idr.hooks if from == to then - local tr = to.records[id] + local tr = (to :: ecs_archetype_t).records[id] local column = from.columns[tr] column[record.row] = data @@ -984,8 +1020,8 @@ local function world_clear(world: ecs_world_t, entity: i53) end if idr_t then - local queue - local ids + local queue: { i53 } + local ids: Map local count = 0 local archetype_ids = idr_t.cache @@ -1175,8 +1211,8 @@ local function world_delete(world: ecs_world_t, entity: i53) end if idr_t then - local children - local ids + local children: { i53 } + local ids: Map local count = 0 local archetype_ids = idr_t.cache @@ -1240,7 +1276,7 @@ local function world_delete(world: ecs_world_t, entity: i53) if idr_r then local archetype_ids = idr_r.cache local flags = idr_r.flags - if bit32.band(flags, ECS_ID_DELETE) ~= 0 then + if (bit32.band(flags, ECS_ID_DELETE) :: number) ~= 0 then for archetype_id in archetype_ids do local idr_r_archetype = archetypes[archetype_id] local entities = idr_r_archetype.entities @@ -1754,7 +1790,7 @@ local function query_cached(query: ecs_query_data_t) local observable = world.observable :: ecs_observable_t local on_create_action = observable[EcsOnArchetypeCreate] if not on_create_action then - on_create_action = {} + on_create_action = {} :: Map observable[EcsOnArchetypeCreate] = on_create_action end local query_cache_on_create = on_create_action[A] @@ -1765,7 +1801,7 @@ local function query_cached(query: ecs_query_data_t) local on_delete_action = observable[EcsOnArchetypeDelete] if not on_delete_action then - on_delete_action = {} + on_delete_action = {} :: Map observable[EcsOnArchetypeDelete] = on_delete_action end local query_cache_on_delete = on_delete_action[A] @@ -2168,12 +2204,12 @@ local function world_query(world: ecs_world_t, ...) return q end - if idr == nil or map.size < idr.size then + if idr == nil or (map.size :: number) < (idr.size :: number) then idr = map end end - if not idr then + if idr == nil then return q end @@ -2306,7 +2342,7 @@ local function world_new() ROOT_ARCHETYPE = (nil :: any) :: Archetype, max_archetype_id = 0, - max_component_id = 0, + max_component_id = ecs_max_component_id, observable = {} :: Observable, }, World) :: any @@ -2323,6 +2359,21 @@ local function world_new() entity_index_new_id(entity_index) end + for i = EcsRest + 1, ecs_max_tag_id do + -- Initialize built-in components + entity_index_new_id(entity_index) + end + + for i, bundle in ecs_metadata do + for ty, value in bundle do + if value == NULL then + world_add(self, i, ty) + else + world_set(self, i, ty, value) + end + end + end + world_add(self, EcsName, EcsComponent) world_add(self, EcsOnChange, EcsComponent) world_add(self, EcsOnAdd, EcsComponent) @@ -2350,22 +2401,25 @@ end World.new = world_new -export type Entity = { __T: T } -export type Id = { __T: T } +export type Entity = { __T: T } +export type Id = { __T: T } export type Pair = Id

type ecs_id_t = Id | Pair | Pair<"Tag", T> export type Item = (self: Query) -> (Entity, T...) export type Iter = (query: Query) -> () -> (Entity, T...) -export type Query = typeof(setmetatable({}, { - __iter = (nil :: any) :: Iter, -})) & { - iter: Iter, - with: (self: Query, ...Id) -> Query, - without: (self: Query, ...Id) -> Query, - archetypes: (self: Query) -> { Archetype }, - cached: (self: Query) -> Query, -} +export type Query = typeof(setmetatable( + {} :: { + iter: Iter, + with: (self: Query, ...Id) -> Query, + without: (self: Query, ...Id) -> Query, + archetypes: (self: Query) -> { Archetype }, + cached: (self: Query) -> Query, + }, + {} :: { + __iter: Iter + } +)) export type Observer = { callback: (archetype: Archetype) -> (), @@ -2399,20 +2453,20 @@ export type World = { component: (self: World) -> Entity, --- Gets the target of an relationship. For example, when a user calls --- `world:target(id, ChildOf(parent), 0)`, you will obtain the parent entity. - target: (self: World, id: Entity, relation: Id, index: number?) -> Entity?, + target: (self: World, id: Entity, relation: Id, index: number?) -> Entity?, --- Deletes an entity and all it's related components and relationships. delete: (self: World, id: Entity) -> (), --- Adds a component to the entity with no value - add: (self: World, id: Entity, component: Id) -> (), + add: (self: World, id: Entity, component: Id) -> (), --- Assigns a value to a component on the given entity set: (self: World, id: Entity, component: Id, data: T) -> (), cleanup: (self: World) -> (), -- Clears an entity from the world - clear: (self: World, id: Entity) -> (), + clear: (self: World, id: Id) -> (), --- Removes a component from the given entity - remove: (self: World, id: Entity, component: Id) -> (), + remove: (self: World, id: Entity, component: Id) -> (), --- Retrieves the value of up to 4 components. These values may be nil. get: ((self: World, id: Entity, Id) -> A?) & ((self: World, id: Entity, Id, Id) -> (A?, B?)) @@ -2431,9 +2485,9 @@ export type World = { --- Checks if the world contains the given entity contains:(self: World, entity: Entity) -> boolean, - each: (self: World, id: Id) -> () -> Entity, + each: (self: World, id: Id) -> () -> Entity, - children: (self: World, id: Id) -> () -> Entity, + children: (self: World, id: Id) -> () -> Entity, --- Searches the world for entities that match a given query query: ((World, Id) -> Query) @@ -2461,10 +2515,14 @@ export type World = { -- return first -- end -- end +-- return { World = World :: { new: () -> World }, world = world_new :: () -> World, + component = (ECS_COMPONENT :: any) :: () -> Entity, + tag = (ECS_TAG :: any) :: () -> Entity, + meta = (ECS_META :: any) :: (id: Entity, id: Id, value: T) -> Entity, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, @@ -2487,8 +2545,8 @@ return { ECS_GENERATION_INC = ECS_GENERATION_INC, ECS_GENERATION = ECS_GENERATION, ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD, - ECS_ID_DELETE = ECS_ID_DELETE, + ECS_META_RESET = ECS_META_RESET, IS_PAIR = (ECS_IS_PAIR :: any) :: (pair: Pair) -> boolean, pair_first = (ecs_pair_first :: any) :: (world: World, pair: Pair) -> Id

, diff --git a/test/tests.luau b/test/tests.luau index cb95c90..f4c1b78 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -62,8 +62,8 @@ end) TEST("world:children()", function() local world = jecs.world() - local C = world:component() - local T = world:entity() + local C = jecs.component() + local T = jecs.tag() local e1 = world:entity() world:set(e1, C, true) @@ -95,6 +95,8 @@ TEST("world:children()", function() end CHECK(count == 1) + + jecs.ECS_META_RESET() end) TEST("world:clear()", function() @@ -195,6 +197,26 @@ TEST("world:clear()", function() end) TEST("world:component()", function() + do CASE "allow IDs to be registered" + local A = jecs.component() + local B = jecs.component() + + local world = jecs.world() + local C = world:component() + CHECK((A :: any) == 1) + CHECK((B :: any) == 2) + CHECK((C :: any) == 3) + + local e = world:entity() + + world:set(e, A, "foo") + world:set(e, B, "foo") + world:set(e, C, "foo") + + CHECK(world:has(e, A, B, C)) + + jecs.ECS_META_RESET() -- Reset the ECS metadata because they may have side effects + end do CASE "only components should have EcsComponent trait" local world = jecs.world() local A = world:component() @@ -720,21 +742,21 @@ TEST("world:query()", function() local i = 0 local iter = 0 - for _, e in q:iter() do + for _ in q:iter() do iter += 1 i=1 end CHECK (iter == 1) CHECK(i == 1) - for _, e in q:iter() do + for _ in q:iter() do i=2 end CHECK(i == 2) - for _, e in q :: any do + for _ in q do i=3 end CHECK(i == 3) - for _, e in q :: any do + for _ in q do i=4 end CHECK(i == 4) @@ -746,8 +768,8 @@ TEST("world:query()", function() end do CASE "multiple iter" local world = jecs.World.new() - local A = world:component() - local B = world:component() + local A = world:component() :: jecs.Entity + local B = world:component() :: jecs.Entity local e = world:entity() world:add(e, A) world:add(e, B) @@ -756,7 +778,7 @@ TEST("world:query()", function() for x in q:iter() do counter += 1 end - for x in q:iter() do + for x, tets in q do counter += 1 end CHECK(counter == 2) From 6811b6de0edb686cd045a579a062c591f0829387 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Sat, 19 Apr 2025 19:33:53 +0200 Subject: [PATCH 2/4] Fix test case erroring --- test/tests.luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests.luau b/test/tests.luau index f4c1b78..1aea9e1 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -778,7 +778,7 @@ TEST("world:query()", function() for x in q:iter() do counter += 1 end - for x, tets in q do + for x in q:iter() do counter += 1 end CHECK(counter == 2) From 38f189b54dca84a7289fd984f4f44367e831f3cc Mon Sep 17 00:00:00 2001 From: Ukendio Date: Tue, 22 Apr 2025 04:10:45 +0200 Subject: [PATCH 3/4] Initial commit --- addons/observers.luau | 19 +-- benches/visual/insertion.bench.luau | 5 +- benches/visual/spawn.bench.luau | 26 ++-- jecs.luau | 196 +++++++++++++++++++++++----- test/addons/observers.luau | 19 +++ test/tests.luau | 66 +++++++++- tools/lifetime_tracker.luau | 5 +- tools/testkit.luau | 18 +-- 8 files changed, 281 insertions(+), 73 deletions(-) diff --git a/addons/observers.luau b/addons/observers.luau index cffce81..29c261f 100644 --- a/addons/observers.luau +++ b/addons/observers.luau @@ -104,12 +104,13 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl if not listeners then listeners = {} signals.added[component] = listeners - local idr = jecs.id_record_ensure(world, component) - idr.hooks.on_add = function(entity) + + local function on_add(entity: number, id: number, value: any) for _, listener in listeners do - listener(entity, component) + listener(entity, id, value) end end + world:set(component, jecs.OnAdd, on_add) end table.insert(listeners, fn) end @@ -119,12 +120,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl if not listeners then listeners = {} signals.emplaced[component] = listeners - local idr = jecs.id_record_ensure(world, component) - idr.hooks.on_change = function(entity, value) + local function on_change(entity: number, id: number, value: any) for _, listener in listeners do - listener(entity, component, value) + listener(entity, id, value) end end + world:set(component, jecs.OnChange, on_change) end table.insert(listeners, fn) end @@ -134,12 +135,12 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl if not listeners then listeners = {} signals.removed[component] = listeners - local idr = jecs.id_record_ensure(world, component) - idr.hooks.on_remove = function(entity) + local function on_remove(entity: number, id: number, value: any) for _, listener in listeners do - listener(entity, component) + listener(entity, id, value) end end + world:set(component, jecs.OnRemove, on_remove) end table.insert(listeners, fn) end diff --git a/benches/visual/insertion.bench.luau b/benches/visual/insertion.bench.luau index f33821d..802b406 100644 --- a/benches/visual/insertion.bench.luau +++ b/benches/visual/insertion.bench.luau @@ -4,10 +4,9 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage") local Matter = require(ReplicatedStorage.DevPackages.Matter) local ecr = require(ReplicatedStorage.DevPackages.ecr) -local jecs = require(ReplicatedStorage.Lib) -local newWorld = Matter.World.new() +local jecs = require(ReplicatedStorage.Lib:Clone()) local ecs = jecs.World.new() -local mirror = require(ReplicatedStorage.mirror) +local mirror = require(ReplicatedStorage.mirror:Clone()) local mcs = mirror.World.new() local A1 = Matter.component() diff --git a/benches/visual/spawn.bench.luau b/benches/visual/spawn.bench.luau index 5dc2b84..393407c 100644 --- a/benches/visual/spawn.bench.luau +++ b/benches/visual/spawn.bench.luau @@ -2,35 +2,25 @@ --!native local ReplicatedStorage = game:GetService("ReplicatedStorage") -local Matter = require(ReplicatedStorage.DevPackages.Matter) -local ecr = require(ReplicatedStorage.DevPackages.ecr) -local jecs = require(ReplicatedStorage.Lib) -local newWorld = Matter.World.new() +local jecs = require(ReplicatedStorage.Lib:Clone()) +local mirror = require(ReplicatedStorage.mirror:Clone()) + local ecs = jecs.World.new() +local mcs = mirror.world() return { ParameterGenerator = function() - local registry2 = ecr.registry() - - return registry2 end, Functions = { - Matter = function() - for i = 1, 1000 do - newWorld:spawn() + Mirror = function() + for i = 1000, 1100 do + mcs:entity(i) end end, - - ECR = function(_, registry2) - for i = 1, 1000 do - registry2.create() - end - end, - Jecs = function() for i = 1, 1000 do - ecs:entity() + ecs:entity(i) end end, }, diff --git a/jecs.luau b/jecs.luau index b7dae17..458e3c2 100644 --- a/jecs.luau +++ b/jecs.luau @@ -45,9 +45,9 @@ type ecs_id_record_t = { flags: number, size: number, hooks: { - on_add: ((entity: i53, data: any?) -> ())?, - on_change: ((entity: i53, data: any) -> ())?, - on_remove: ((entity: i53) -> ())?, + on_add: ((entity: i53, id: i53, data: any?) -> ())?, + on_change: ((entity: i53, id: i53, data: any) -> ())?, + on_remove: ((entity: i53, id: i53) -> ())?, }, } @@ -62,6 +62,8 @@ type ecs_entity_index_t = { sparse_array: Map, alive_count: number, max_id: number, + range_begin: number?, + range_end: number? } type ecs_query_data_t = { @@ -118,6 +120,7 @@ local ECS_GENERATION_MASK = bit32.lshift(1, 16) local NULL_ARRAY = table.freeze({}) :: Column local NULL = newproxy(false) +local NULL_RECORD = table.freeze({ dense = 0 }) :: ecs_record_t local ECS_INTERNAL_ERROR = [[ This is an internal error, please file a bug report via the following link: @@ -144,7 +147,7 @@ end local function ECS_META(id: i53, ty: i53, value: any?) local bundle = ecs_metadata[id] - if bundle then + if bundle == nil then bundle = {} ecs_metadata[id] = bundle end @@ -185,6 +188,10 @@ local function ECS_ENTITY_T_LO(e: i53): i24 return e % ECS_ENTITY_MASK end +local function ECS_ID(e: i53) + return e % ECS_ENTITY_MASK +end + local function ECS_GENERATION(e: i53) return e // ECS_ENTITY_MASK end @@ -249,10 +256,10 @@ local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i return entity_index_try_get(entity_index, entity) ~= nil end -local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53? - local r = entity_index_try_get_any(index, entity) +local function entity_index_get_alive(entity_index: ecs_entity_index_t, entity: i53): i53? + local r = entity_index_try_get_any(entity_index, entity) if r then - return index.dense_array[r.dense] + return entity_index.dense_array[r.dense] end return nil end @@ -283,8 +290,10 @@ end local function entity_index_new_id(entity_index: ecs_entity_index_t): i53 local dense_array = entity_index.dense_array local alive_count = entity_index.alive_count + local sparse_array = entity_index.sparse_array local max_id = entity_index.max_id - if alive_count ~= max_id then + + if alive_count < max_id then alive_count += 1 entity_index.alive_count = alive_count local id = dense_array[alive_count] @@ -292,11 +301,15 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53 end local id = max_id + 1 + local range_end = entity_index.range_end + if range_end then + assert(id < range_end) + end entity_index.max_id = id alive_count += 1 entity_index.alive_count = alive_count dense_array[alive_count] = id - entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t + sparse_array[id] = { dense = alive_count } :: ecs_record_t return id end @@ -500,6 +513,14 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b return records[id] ~= nil end +local function ecs_is_tag(world: ecs_world_t, entity: i53): boolean + local idr = world.component_index[entity] + if idr then + return bit32.band(idr.flags, ECS_ID_IS_TAG) ~= 0 + end + return not world_has_one_inline(world, entity, EcsComponent) +end + local function world_has(world: ecs_world_t, entity: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean @@ -711,8 +732,117 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: return archetype end -local function world_entity(world: ecs_world_t): i53 - return entity_index_new_id(world.entity_index) +local function world_range(world: ecs_world_t, range_begin: number, range_end: number?) + local entity_index = world.entity_index + if range_end then + range_end += 1 + end + + entity_index.range_begin = range_begin + entity_index.range_end = range_end + + local max_id = entity_index.max_id + local dense_array = entity_index.dense_array + local sparse_array = entity_index.sparse_array + + if range_begin > max_id then + for i = max_id, range_begin - 1 do + dense_array[i] = 0 + sparse_array[i] = NULL_RECORD + end + sparse_array[range_begin] = { dense = 0 } :: ecs_record_t + end + + entity_index.max_id = range_begin + entity_index.alive_count = range_begin +end + +local function world_entity(world: ecs_world_t, entity: i53?): i53 + local entity_index = world.entity_index + if entity then + local index = ECS_ID(entity) + local range_begin = entity_index.range_begin + if range_begin then + assert(index > range_begin) + end + local max_id = entity_index.max_id + local sparse_array = entity_index.sparse_array + local dense_array = entity_index.dense_array + local alive_count = entity_index.alive_count + local r = sparse_array[index] + if r then + local dense = r.dense + if not dense or dense == 0 then + dense = index + end + local any = dense_array[dense] + if any == entity then + if alive_count > dense then + return entity + end + local e_swap = dense_array[alive_count] + local r_swap = sparse_array[alive_count] + r_swap.dense = dense + r.dense = alive_count + dense_array[alive_count] = entity + dense_array[dense] = e_swap + return entity + end + + if any ~= 0 then + local e_swap = dense_array[alive_count] + local r_swap = sparse_array[alive_count] + + if dense > alive_count then + r_swap.dense = dense + r.dense = alive_count + dense_array[alive_count] = any + dense_array[dense] = e_swap + else + r_swap.dense = dense + -- alive_count += 1 + alive_count += 1 + entity_index.alive_count = alive_count + r.dense = alive_count + dense_array[alive_count] = any + dense_array[dense] = e_swap + end + return any + else + error("should never happen") + end + else + local range_end = entity_index.range_end + if range_end then + assert(index < range_end) + end + for i = max_id + 1, index do + sparse_array[i] = { dense = i } :: ecs_record_t + dense_array[i] = i + end + entity_index.max_id = index + + local e_swap = dense_array[alive_count] + local r_swap = sparse_array[alive_count] + r_swap.dense = index + + alive_count += 1 + entity_index.alive_count = alive_count + + r = sparse_array[index] + + r.dense = alive_count + + sparse_array[index] = r + + dense_array[index] = e_swap + dense_array[alive_count] = entity + + + return entity + end + end + return entity_index_new_id(entity_index, entity) end local function world_parent(world: ecs_world_t, entity: i53) @@ -849,7 +979,7 @@ local function world_add( local on_add = idr.hooks.on_add if on_add then - on_add(entity) + on_add(entity, id) end end @@ -874,7 +1004,7 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown -- and just set the data directly. local on_change = idr_hooks.on_change if on_change then - on_change(entity, data) + on_change(entity, id, data) end return @@ -897,7 +1027,7 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown local on_add = idr_hooks.on_add if on_add then - on_add(entity, data) + on_add(entity, id, data) end end @@ -929,7 +1059,7 @@ local function world_remove(world: ecs_world_t, entity: i53, id: i53) local idr = world.component_index[id] local on_remove = idr.hooks.on_remove if on_remove then - on_remove(entity) + on_remove(entity, id) end local to = archetype_traverse_remove(world, id, record.archetype) @@ -981,7 +1111,7 @@ local function archetype_delete(world: ecs_world_t, archetype: ecs_archetype_t, local idr = component_index[id] local on_remove = idr.hooks.on_remove if on_remove then - on_remove(delete) + on_remove(delete, id) end end @@ -1041,7 +1171,7 @@ local function world_clear(world: ecs_world_t, entity: i53) continue end if not ids then - ids = {} + ids = {} :: { [i53]: boolean } end ids[id] = true removal_queued = true @@ -1052,7 +1182,7 @@ local function world_clear(world: ecs_world_t, entity: i53) end if not queue then - queue = {} + queue = {} :: { i53 } end local n = #entities @@ -1242,7 +1372,7 @@ local function world_delete(world: ecs_world_t, entity: i53) break else if not ids then - ids = {} + ids = {} :: { [i53]: boolean } end ids[id] = true removal_queued = true @@ -1253,7 +1383,7 @@ local function world_delete(world: ecs_world_t, entity: i53) continue end if not children then - children = {} + children = {} :: { i53 } end local n = #entities table.move(entities, 1, n, count + 1, children) @@ -2303,6 +2433,8 @@ export type EntityIndex = { sparse_array: Map, alive_count: number, max_id: number, + range_begin: number?, + range_end: number? } local World = {} @@ -2324,6 +2456,7 @@ World.contains = world_contains World.cleanup = world_cleanup World.each = world_each World.children = world_children +World.range = world_range local function world_new() local entity_index = { @@ -2364,16 +2497,6 @@ local function world_new() entity_index_new_id(entity_index) end - for i, bundle in ecs_metadata do - for ty, value in bundle do - if value == NULL then - world_add(self, i, ty) - else - world_set(self, i, ty, value) - end - end - end - world_add(self, EcsName, EcsComponent) world_add(self, EcsOnChange, EcsComponent) world_add(self, EcsOnAdd, EcsComponent) @@ -2396,6 +2519,16 @@ local function world_new() world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete)) + for i, bundle in ecs_metadata do + for ty, value in bundle do + if value == NULL then + world_add(self, i, ty) + else + world_set(self, i, ty, value) + end + end + end + return self end @@ -2523,6 +2656,7 @@ return { component = (ECS_COMPONENT :: any) :: () -> Entity, tag = (ECS_TAG :: any) :: () -> Entity, meta = (ECS_META :: any) :: (id: Entity, id: Id, value: T) -> Entity, + is_tag = (ecs_is_tag :: any) :: (World, Id) -> boolean, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, @@ -2549,6 +2683,8 @@ return { ECS_META_RESET = ECS_META_RESET, IS_PAIR = (ECS_IS_PAIR :: any) :: (pair: Pair) -> boolean, + ECS_PAIR_FIRST = ECS_PAIR_FIRST, + ECS_PAIR_SECOND = ECS_PAIR_SECOND, pair_first = (ecs_pair_first :: any) :: (world: World, pair: Pair) -> Id

, pair_second = (ecs_pair_second :: any) :: (world: World, pair: Pair) -> Id, entity_index_get_alive = entity_index_get_alive, diff --git a/test/addons/observers.luau b/test/addons/observers.luau index cf5e10a..26461c7 100644 --- a/test/addons/observers.luau +++ b/test/addons/observers.luau @@ -82,6 +82,25 @@ TEST("addons/observers", function() world:set(e, A, true) CHECK(count == 3) end + + do CASE "Call on pairs" + local A = world:component() + + local callcount = 0 + world:added(A, function(entity) + callcount += 1 + end) + world:added(A, function(entity) + callcount += 1 + end) + + local e = world:entity() + local e1 = world:entity() + + world:add(e1, jecs.pair(A, e)) + world:add(e, jecs.pair(A, e1)) + CHECK(callcount == 4) + end end) return FINISH() diff --git a/test/tests.luau b/test/tests.luau index 1aea9e1..c1630ee 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -22,6 +22,7 @@ type Entity = jecs.Entity type Id = jecs.Id local entity_visualiser = require("@tools/entity_visualiser") +local lifetime_tracker_add = require("@tools/lifetime_tracker") local dwi = entity_visualiser.stringify TEST("world:add()", function() @@ -246,6 +247,8 @@ TEST("world:component()", function() end) TEST("world:contains()", function() + + local tag = jecs.tag() local world = jecs.world() local id = world:entity() CHECK(world:contains(id)) @@ -255,6 +258,10 @@ TEST("world:contains()", function() world:delete(id) CHECK(not world:contains(id)) end + + + CHECK(world:contains(tag)) + jecs.ECS_META_RESET() end) TEST("world:delete()", function() @@ -626,8 +633,65 @@ TEST("world:each()", function() end end) +FOCUS() TEST("world:entity()", function() + do CASE "range" + local world = jecs.world() + world = lifetime_tracker_add(world, {}) + world:range(1000, 2000) + + world:entity(1590) + CHECK_EXPECT_ERR(function() + + world:entity(5000) + end) + + CHECK(world:contains(1590)) + world:set(591, jecs.Name, "9888") + CHECK(not world:contains(591)) + CHECK(not world:contains(5000)) + CHECK(not world:contains(988)) + end + do CASE "desired id" + local world = jecs.world() + world = lifetime_tracker_add(world, {}) + local id = world:entity() + local e = world:entity(id + 5) + CHECK(e == id + 5) + CHECK(world:contains(e)) + local e2 = world:entity() + CHECK(world:contains(e2)) + + -- world:print_entity_index() + local e3 = world:entity(275) + + CHECK(e3 == 275) + CHECK(world:contains(e3)) + + CHECK(e3 == world:entity(e3)) + + world:delete(e3) + + print("---------------------------") + local e3v1 = world:entity(275) + CHECK(not world:contains(275)) + CHECK(jecs.ECS_GENERATION(e3v1) == 1) + CHECK(jecs.ECS_ID(e3v1) == 275) + + + CHECK(world:contains(e3v1)) + -- world:print_entity_index() + print("--------begin") + world:entity(e3) + print("-----end") + + world:print_entity_index() + + world:entity(e3) + world:entity(275) + end local N = 2^8 + do CASE "unique IDs" local world = jecs.world() local set = {} @@ -1427,8 +1491,6 @@ TEST("#adding a recycled target", function() end) - - TEST("#repro2", function() local world = jecs.world() local Lifetime = world:component() :: Id diff --git a/tools/lifetime_tracker.luau b/tools/lifetime_tracker.luau index 62a5887..fa486a4 100644 --- a/tools/lifetime_tracker.luau +++ b/tools/lifetime_tracker.luau @@ -28,7 +28,7 @@ end local padding_enabled = false local function pad() if padding_enabled then - print("") + print("------------------") end end @@ -64,7 +64,8 @@ local function lifetime_tracker_add(world: jecs.World, opt): PatchedWorld world.print_entity_index = function() local max_id = entity_index.max_id local alive_count = entity_index.alive_count - local alive = table.move(dense_array, 1 + jecs.Rest :: any, alive_count, 1, {}) + local range_begin = entity_index.range_begin or jecs.Rest + 1 + local alive = table.move(dense_array, range_begin :: any, alive_count, 1, {}) local dead = table.move(dense_array, alive_count + 1, max_id, 1, {}) local sep = "|--------|" diff --git a/tools/testkit.luau b/tools/testkit.luau index 223eb0c..89a6c4a 100644 --- a/tools/testkit.luau +++ b/tools/testkit.luau @@ -226,8 +226,9 @@ local function CHECK(value: T, stack: number?): T? return value end -local function TEST(name: string, fn: () -> ()) +local test_focused = false +local function TEST(name: string, fn: () -> ()) test = { name = name, cases = {}, @@ -235,21 +236,20 @@ local function TEST(name: string, fn: () -> ()) focus = false, fn = fn } - + local t = test + if check_for_focused and not test_focused then + test.focus = true + test_focused = true + end + table.insert(tests, t) end local function FOCUS() - assert(test, "no active test") - check_for_focused = true - if test.case then - test.case.focus = true - else - test.focus = true - end + test_focused = false end local function FINISH(): number From cb238ab29e9686ff53ab9c9b915d35d519be8916 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Tue, 22 Apr 2025 05:02:33 +0200 Subject: [PATCH 4/4] Simplify entity swap logic in world_entity function --- jecs.luau | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/jecs.luau b/jecs.luau index 458e3c2..8978ea1 100644 --- a/jecs.luau +++ b/jecs.luau @@ -789,28 +789,21 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53 return entity end - if any ~= 0 then - local e_swap = dense_array[alive_count] - local r_swap = sparse_array[alive_count] + -- assert(any ~= 0) should never happen - if dense > alive_count then - r_swap.dense = dense - r.dense = alive_count - dense_array[alive_count] = any - dense_array[dense] = e_swap - else - r_swap.dense = dense - -- alive_count += 1 - alive_count += 1 - entity_index.alive_count = alive_count - r.dense = alive_count - dense_array[alive_count] = any - dense_array[dense] = e_swap - end - return any - else - error("should never happen") + local e_swap = dense_array[alive_count] + local r_swap = sparse_array[alive_count] + + if dense <= alive_count then + alive_count += 1 + entity_index.alive_count = alive_count end + + r_swap.dense = dense + r.dense = alive_count + dense_array[alive_count] = any + dense_array[dense] = e_swap + return any else local range_end = entity_index.range_end if range_end then