From 74fe5be0c058c268fab76eebd84a0bf7e692ee55 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Thu, 25 Apr 2024 16:26:30 +0200 Subject: [PATCH] Index into archetypemap --- benches/query.bench.lua | 20 ++++++-- lib/init.lua | 15 +++--- lib/init.spec.lua | 88 ++++++++++++++++++++++++++++------- mirror/init.lua | 100 +++++++++------------------------------- 4 files changed, 119 insertions(+), 104 deletions(-) diff --git a/benches/query.bench.lua b/benches/query.bench.lua index e7f104d..ad31315 100644 --- a/benches/query.bench.lua +++ b/benches/query.bench.lua @@ -65,12 +65,14 @@ local E8 = mcs:entity() local registry2 = ecr.registry() local function flip() - return math.random() >= 0.5 + return math.random() >= 0.15 end local common = 0 local N = 2^16-2 local archetypes = {} + +local hm = 0 for i = 1, N do local id = registry2.create() local combination = "" @@ -153,8 +155,15 @@ for i = 1, N do end + if combination:find("BCDF") then + if not archetypes[combination] then + print(combination) + end + hm += 1 + end archetypes[combination] = true end +print("TEST", hm) local white = rgb.white local yellow = rgb.yellow @@ -179,6 +188,8 @@ print( ..yellow("Total Archetypes: "..numberOfArchetypes) ) +local q = ecs:query(D4, D2, D3, D6) + return { ParameterGenerator = function() return @@ -187,16 +198,17 @@ return { Functions = { Mirror = function() local matched = 0 - for entityId, firstComponent in mcs:query(E1, E2, E3, E4) do + for entityId, firstComponent in mcs:query(E2, E3, E4, E1) do matched += 1 end end, Jecs = function() local matched = 0 - for entityId, firstComponent in ecs:query(D1, D2, D3, D4) do + for entityId, firstComponent in ecs:query(E2, E3, E4, E1) do matched += 1 end - end + + end, }, } diff --git a/lib/init.lua b/lib/init.lua index 9166fbc..94e4292 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -276,7 +276,7 @@ end local function get(componentIndex: { [i24]: ArchetypeMap }, record: Record, componentId: i24) local archetype = record.archetype - local archetypeRecord = componentIndex[componentId][archetype.id] + local archetypeRecord = componentIndex[componentId].map[archetype.id] if not archetypeRecord then return nil @@ -347,15 +347,15 @@ function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> ( local columns = archetype.columns local archetypeRecords = archetype.records local indices = {} - local skip = false + for i, componentId in components do - local data = columns[archetypeRecords[componentId]] - if not data then + local index = archetypeRecords[componentId] + if not index then skip = true break end - indices[i] = data + indices[i] = columns[index] end if skip then @@ -369,9 +369,12 @@ function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> ( end local lastArchetype, compatibleArchetype = next(compatibleArchetypes) + if not compatibleArchetype then + return noop() + end local lastRow - + return function() local archetype = compatibleArchetype.archetype local indices = compatibleArchetype.indices diff --git a/lib/init.spec.lua b/lib/init.spec.lua index af54cf0..f2a40e8 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -11,22 +11,77 @@ print("F", F) print("G", G) print("H", H) -for i = 1, 256 do - local entity = ecs:entity() - ecs:set(entity, A, true) - ecs:set(entity, B, true) - ecs:set(entity, C, true) - ecs:set(entity, D, true) - - --[[ - ecs:set(entity, E, true) - ecs:set(entity, F, true) - ecs:set(entity, G, true) - ecs:set(entity, H, true) - print("end") - ]] +local common = 0 +local N = 2^16-2 +local archetypes = {} +local function flip() + return math.random() >= 0.5 end +local hm = 0 +for i = 1, N do + local entity = ecs:entity() + local combination = "" + + if flip() then + combination ..= "2_" + ecs:set(entity, B, { value = true}) + end + if flip() then + combination ..= "3_" + ecs:set(entity, C, { value = true}) + end + if flip() then + combination ..= "4_" + ecs:set(entity, D, { value = true}) + end + if flip() then + combination ..= "5_" + ecs:set(entity, E, { value = true}) + end + if flip() then + combination ..= "6_" + ecs:set(entity, F, { value = true}) + end + if flip() then + combination ..= "7_" + ecs:set(entity, G, { value = true}) + end + if flip() then + combination ..= "8" + ecs:set(entity, H, { value = true}) + end + + if #combination == 7 then + combination = "1_" .. combination + common += 1 + ecs:set(entity, A, { value = true}) + end + + if combination:find("2") + and combination:find("3") + and combination:find("4") + and combination:find("6") + then + hm += 1 + end + archetypes[combination] = true +end + + +local arch = 0 +for combination in archetypes do + if combination:find("2") + and combination:find("3") + and combination:find("4") + and combination:find("6") + then + print(combination) + arch += 1 + end +end +print("TEST", hm, arch) + return function() describe("World", function() it("should add component", function() @@ -60,10 +115,11 @@ return function() end) it("query", function() local added = 0 - for e, a, b, c, d in ecs:query(A, B, C, D) do + for _ in ecs:query(B, C, D, F) do added += 1 end - expect(added).to.equal(256) + expect(added).to.equal(hm) + print(added, hm) end) end) diff --git a/mirror/init.lua b/mirror/init.lua index 47dac46..94e4292 100644 --- a/mirror/init.lua +++ b/mirror/init.lua @@ -276,7 +276,7 @@ end local function get(componentIndex: { [i24]: ArchetypeMap }, record: Record, componentId: i24) local archetype = record.archetype - local archetypeRecord = componentIndex[componentId][archetype.id] + local archetypeRecord = componentIndex[componentId].map[archetype.id] if not archetypeRecord then return nil @@ -338,8 +338,6 @@ function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> ( local queryLength = #components local firstArchetypeMap = getSmallestMap(world.componentIndex, components) - local a, b, c, d, e, f, g, h = ... - if not firstArchetypeMap then return noop() end @@ -349,85 +347,31 @@ function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> ( local columns = archetype.columns local archetypeRecords = archetype.records local indices = {} - if queryLength == 1 then - indices = { - columns[archetypeRecords[a]], - } - elseif queryLength == 2 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]] - } - elseif queryLength == 3 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]], - columns[archetypeRecords[c]] - } - elseif queryLength == 4 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]], - columns[archetypeRecords[c]], - columns[archetypeRecords[d]] - } - elseif queryLength == 5 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]], - columns[archetypeRecords[c]], - columns[archetypeRecords[d]], - columns[archetypeRecords[e]] - } - elseif queryLength == 6 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]], - columns[archetypeRecords[c]], - columns[archetypeRecords[d]], - columns[archetypeRecords[e]], - columns[archetypeRecords[f]], - } - elseif queryLength == 7 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]], - columns[archetypeRecords[c]], - columns[archetypeRecords[d]], - columns[archetypeRecords[e]], - columns[archetypeRecords[f]], - columns[archetypeRecords[g]], - } - elseif queryLength == 8 then - indices = { - columns[archetypeRecords[a]], - columns[archetypeRecords[b]], - columns[archetypeRecords[c]], - columns[archetypeRecords[d]], - columns[archetypeRecords[e]], - columns[archetypeRecords[f]], - columns[archetypeRecords[g]], - columns[archetypeRecords[h]], - } - else - for i, componentId in components do - indices[i] = columns[archetypeRecords[componentId]] - end - end - - local i = 0 - for _ in indices do - i += 1 - end - if queryLength == i then - table.insert(compatibleArchetypes, { - archetype = archetype, - indices = indices - }) + local skip = false + + for i, componentId in components do + local index = archetypeRecords[componentId] + if not index then + skip = true + break + end + indices[i] = columns[index] end + + if skip then + continue + end + + table.insert(compatibleArchetypes, { + archetype = archetype, + indices = indices + }) end local lastArchetype, compatibleArchetype = next(compatibleArchetypes) + if not compatibleArchetype then + return noop() + end local lastRow