Optimize world:has and improve type annotations

This commit is contained in:
Ukendio 2025-04-13 01:51:21 +02:00
parent 4c958071e0
commit 447bb76bb8
4 changed files with 17 additions and 10 deletions

View file

@ -4,7 +4,7 @@
"testkit": "tools/testkit",
"mirror": "mirror",
"tools": "tools",
"addons": "addons",
"addons": "addons"
},
"languageMode": "strict"
}

View file

@ -23,6 +23,8 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
- This should allow a more lenient window for modifying data
- Changed `OnRemove` to lazily lookup which archetype the entity will move to
- Can now have interior structural changes within `OnRemove` hooks
- Optimized `world:has` for both single component and multiple component presence.
- This comes at the cost that it cannot check the component presence for more than 4 components at a time. If this is important, consider calling to this function multiple times.
## [0.5.0] - 2024-12-26

View file

@ -466,7 +466,9 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b
return records[id] ~= nil
end
local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean
local function world_has(world: ecs_world_t, entity: i53,
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return false
@ -479,13 +481,11 @@ local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean
local records = archetype.records
for i = 1, select("#", ...) do
if not records[select(i, ...)] then
return false
end
end
return true
return records[a] ~= nil and
(b == nil or records[b] ~= nil) and
(c == nil or records[c] ~= nil) and
(d == nil or records[d] ~= nil) and
(e == nil or error("args exceeded"))
end
local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24?

View file

@ -32,7 +32,12 @@ local function pad()
end
end
local function lifetime_tracker_add(world: jecs.World, opt)
type PatchedWorld = jecs.World & {
print_entity_index: (world: PatchedWorld) -> (),
print_snapshot: (world: PatchedWorld) -> (),
}
local function lifetime_tracker_add(world: jecs.World, opt): PatchedWorld
local entity_index = world.entity_index
local dense_array = entity_index.dense_array
local component_index = world.component_index