diff --git a/.luaurc b/.luaurc index 5518c4c..f856eba 100644 --- a/.luaurc +++ b/.luaurc @@ -4,7 +4,7 @@ "testkit": "tools/testkit", "mirror": "mirror", "tools": "tools", - "addons": "addons", + "addons": "addons" }, "languageMode": "strict" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 19a8ef1..8fdd827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/jecs.luau b/jecs.luau index 29161ed..4622c65 100644 --- a/jecs.luau +++ b/jecs.luau @@ -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? diff --git a/tools/lifetime_tracker.luau b/tools/lifetime_tracker.luau index b5e74fe..62a5887 100644 --- a/tools/lifetime_tracker.luau +++ b/tools/lifetime_tracker.luau @@ -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