From 1f46e925058e19c3e41f5a8dbc364559f99a1f88 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Wed, 28 Aug 2024 19:19:00 +0200 Subject: [PATCH] Add tests --- benches/insert.luau | 67 +++++++++++++++++++++++++++++++++++++++++++++ src/init.luau | 11 +++++--- test/tests.luau | 10 +++++++ 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 benches/insert.luau diff --git a/benches/insert.luau b/benches/insert.luau new file mode 100644 index 0000000..ed94684 --- /dev/null +++ b/benches/insert.luau @@ -0,0 +1,67 @@ +--!optimize 2 +--!native + +local testkit = require("@testkit") +local BENCH, START = testkit.benchmark() +local function TITLE(title: string) +print() +print(testkit.color.white(title)) +end + +local jecs = require("@jecs") + +type i53 = number + +local N = 1000 + +do TITLE(testkit.color.white_underline("Jecs insert")) + local ecs = jecs.World.new() + + local ctypes = {} + + local A = ecs:component() + local B = ecs:component() + local C = ecs:component() + local D = ecs:component() + local E = ecs:component() + local F = ecs:component() + local G = ecs:component() + local H = ecs:component() + ecs:add(A, jecs.Tag) + ecs:add(B, jecs.Tag) + ecs:add(C, jecs.Tag) + ecs:add(D, jecs.Tag) + ecs:add(E, jecs.Tag) + ecs:add(F, jecs.Tag) + ecs:add(G, jecs.Tag) + ecs:add(H, jecs.Tag) + + + BENCH("insert 1 components on entity with 16 components", function() + local entities = {} + + for i = 1, N do + entities[i] = ecs:entity() + end + + for _, entity in entities do + ecs:add(entity, A) + ecs:add(entity, B) + ecs:add(entity, C) + ecs:add(entity, D) + ecs:add(entity, E) + ecs:add(entity, F) + ecs:add(entity, G) + ecs:add(entity, H) + end + + for i = 1, N do + ecs:remove(entities[i], A) + end + + for i = 1, START(N) do + local entity = entities[i] + ecs:add(entity, A) + end + end) +end diff --git a/src/init.luau b/src/init.luau index dd1f8e9..cb2bc91 100644 --- a/src/init.luau +++ b/src/init.luau @@ -80,7 +80,7 @@ local ECS_ID_HAS_HOOKS = 0b0010 --local EcsIdExclusive = 0b0100 local ECS_ID_IS_TAG = 0b1000 -local TAG_OBJ = {} +local NULL_ARRAY = table.freeze({}) local function FLAGS_ADD(is_pair: boolean): number local flags = 0x0 @@ -201,12 +201,15 @@ local function archetype_move(entity_index: EntityIndex, to: Archetype, local records = to.records for i, column in src_columns do - -- Retrieves the new column index from the source archetype's record from each component + if column == NULL_ARRAY then + continue + end + -- Retrieves the new column index from the source archetype's record from each component -- We have to do this because the columns are tightly packed and indexes may not correspond to each other. local tr = records[types[i]] -- Sometimes target column may not exist, e.g. when you remove a component. - if tr and column ~= TAG_OBJ then + if tr then dst_columns[tr.column][dst_row] = column[src_row] end -- If the entity is the last row in the archetype then swapping it would be meaningless. @@ -501,7 +504,7 @@ local function archetype_create(world: World, types: { i24 }, prev: Archetype?): if bit32.band(idr.flags, ECS_ID_IS_TAG) == 0 then columns[i] = {} else - columns[i] = TAG_OBJ + columns[i] = NULL_ARRAY end end diff --git a/test/tests.luau b/test/tests.luau index 3bfb0fb..4876492 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -211,6 +211,16 @@ TEST("world:add()", function() end) TEST("world:query()", function() + do CASE "tag" + local world = jecs.World.new() + local A = world:component() + world:add(A, jecs.Tag) + local e = world:entity() + world:set(e, A, "test") + for id, a in world:query(A) do + CHECK(a == nil) + end + end do CASE "query single component" do local world = jecs.World.new()