From b1a2bc48a779f9be2fa960330e6bd28ed2c087d4 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Fri, 25 Apr 2025 23:41:32 +0200 Subject: [PATCH] No nullable records --- CHANGELOG.md | 1 + benches/visual/spawn.bench.luau | 24 +++++++++++++++--------- jecs.luau | 17 +++++------------ test/tests.luau | 8 +++----- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fdd827..9725a5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog][kac], and this project adheres to ## [Unreleased] - `[world]`: + - Added `world:range` to allow for creating - Changed `world:clear` to also look through the component record for the cleared `ID` - Removes the cleared ID from every entity that has it - Changed entity ID layouts by putting the index in the lower bits, which should make every world function 1-5 nanoseconds faster diff --git a/benches/visual/spawn.bench.luau b/benches/visual/spawn.bench.luau index 393407c..698ff8d 100644 --- a/benches/visual/spawn.bench.luau +++ b/benches/visual/spawn.bench.luau @@ -5,23 +5,29 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage") 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 ecs = jecs.world() + ecs:range(1000, 20000) + local mcs = mirror.World.new() + return ecs, mcs end, Functions = { - Mirror = function() - for i = 1000, 1100 do - mcs:entity(i) + Mirror = function(_, ecs, mcs) + for i = 1, 100 do + + mcs:entity() end end, - Jecs = function() - for i = 1, 1000 do - ecs:entity(i) + + Jecs = function(_, ecs, mcs) + for i = 1, 100 do + + ecs:entity() end end, - }, +}, } diff --git a/jecs.luau b/jecs.luau index d39de85..764296c 100644 --- a/jecs.luau +++ b/jecs.luau @@ -120,7 +120,6 @@ 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: @@ -751,11 +750,12 @@ local function world_range(world: ecs_world_t, range_begin: number, range_end: n local dense_array = entity_index.dense_array local sparse_array = entity_index.sparse_array - for i = max_id, range_begin - 1 do - dense_array[i] = 0 - sparse_array[i] = NULL_RECORD + for i = max_id, range_begin do + dense_array[i] = i + sparse_array[i] = { + dense = 0 + } :: ecs_record_t end - sparse_array[range_begin] = { dense = 0 } :: ecs_record_t entity_index.max_id = range_begin - 1 entity_index.alive_count = range_begin - 1 end @@ -765,13 +765,6 @@ 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 - local range_end = entity_index.range_end - ecs_assert(range_begin == nil or index >= range_begin, - ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY) - ecs_assert(range_end == nil or index <= range_end, - ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY) - local max_id = entity_index.max_id local sparse_array = entity_index.sparse_array local dense_array = entity_index.dense_array diff --git a/test/tests.luau b/test/tests.luau index b9cdfce..c055c6e 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -634,7 +634,7 @@ TEST("world:each()", function() end) TEST("world:range()", function() - do CASE "range" + do CASE "" local world = jecs.world() world = lifetime_tracker_add(world, {}) world:range(1000, 2000) @@ -642,15 +642,13 @@ TEST("world:range()", function() CHECK(e == 1000) world:entity(1590) - CHECK_EXPECT_ERR(function() - world:entity(5000) - end) + CHECK(world:entity(5000) == 5000) CHECK(world:contains(1590)) world:set(591, jecs.Name, "9888") CHECK(not world:contains(591)) - CHECK(not world:contains(5000)) + CHECK(world:contains(5000)) CHECK(not world:contains(988)) local e = world:entity(2000)