Skip to content

Commit 447bb76

Browse files
committed
Optimize world:has and improve type annotations
1 parent 4c95807 commit 447bb76

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

.luaurc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"testkit": "tools/testkit",
55
"mirror": "mirror",
66
"tools": "tools",
7-
"addons": "addons",
7+
"addons": "addons"
88
},
99
"languageMode": "strict"
1010
}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
2323
- This should allow a more lenient window for modifying data
2424
- Changed `OnRemove` to lazily lookup which archetype the entity will move to
2525
- Can now have interior structural changes within `OnRemove` hooks
26+
- Optimized `world:has` for both single component and multiple component presence.
27+
- 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.
2628

2729
## [0.5.0] - 2024-12-26
2830

jecs.luau

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b
466466
return records[id] ~= nil
467467
end
468468

469-
local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean
469+
local function world_has(world: ecs_world_t, entity: i53,
470+
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean
471+
470472
local record = entity_index_try_get_fast(world.entity_index, entity)
471473
if not record then
472474
return false
@@ -479,13 +481,11 @@ local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean
479481

480482
local records = archetype.records
481483

482-
for i = 1, select("#", ...) do
483-
if not records[select(i, ...)] then
484-
return false
485-
end
486-
end
487-
488-
return true
484+
return records[a] ~= nil and
485+
(b == nil or records[b] ~= nil) and
486+
(c == nil or records[c] ~= nil) and
487+
(d == nil or records[d] ~= nil) and
488+
(e == nil or error("args exceeded"))
489489
end
490490

491491
local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24?

tools/lifetime_tracker.luau

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ local function pad()
3232
end
3333
end
3434

35-
local function lifetime_tracker_add(world: jecs.World, opt)
35+
type PatchedWorld = jecs.World & {
36+
print_entity_index: (world: PatchedWorld) -> (),
37+
print_snapshot: (world: PatchedWorld) -> (),
38+
}
39+
40+
local function lifetime_tracker_add(world: jecs.World, opt): PatchedWorld
3641
local entity_index = world.entity_index
3742
local dense_array = entity_index.dense_array
3843
local component_index = world.component_index

0 commit comments

Comments
 (0)