From 2ace031fa2da292c2779c1791a84689b70ce749a Mon Sep 17 00:00:00 2001 From: Marcus Date: Tue, 30 Apr 2024 16:36:38 +0200 Subject: [PATCH] Provide better error messages (#7) * Provide better error messages * Inline getSmallestMap --- benches/query.bench.lua | 26 +------------------------- lib/init.lua | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/benches/query.bench.lua b/benches/query.bench.lua index 6da4e02..90443b9 100644 --- a/benches/query.bench.lua +++ b/benches/query.bench.lua @@ -4,11 +4,8 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage") local rgb = require(ReplicatedStorage.rgb) local Matter = require(ReplicatedStorage.DevPackages.Matter) -local Rewrite = require(ReplicatedStorage.rewrite) local ecr = require(ReplicatedStorage.DevPackages.ecr) local newWorld = Matter.World.new() -local world = Rewrite.World.new() -local component = Rewrite.component local jecs = require(ReplicatedStorage.Lib) local mirror = require(ReplicatedStorage.mirror) @@ -33,15 +30,6 @@ local B6 = ecr.component() local B7 = ecr.component() local B8 = ecr.component() -local C1 = component() -local C2 = component() -local C3 = component() -local C4 = component() -local C5 = component() -local C6 = component() -local C7 = component() -local C8 = component() - local D1 = ecs:entity() local D2 = ecs:entity() local D3 = ecs:entity() @@ -78,13 +66,11 @@ for i = 1, N do local combination = "" local n = newWorld:spawn() local entity = ecs:entity() - local e = world:spawn() local m = mcs:entity() if flip() then combination ..= "B" registry2:set(id, B2, {value = true}) - world:insert(e, C2({ value = true})) ecs:set(entity, D2, { value = true}) mcs:set(m, E2, { value = 2}) newWorld:insert(n, A2({value = true})) @@ -92,7 +78,6 @@ for i = 1, N do if flip() then combination ..= "C" registry2:set(id, B3, {value = true}) - world:insert(e, C3({ value = true})) ecs:set(entity, D3, { value = true}) mcs:set(m, E3, { value = 2}) newWorld:insert(n, A3({value = true})) @@ -100,7 +85,6 @@ for i = 1, N do if flip() then combination ..= "D" registry2:set(id, B4, {value = true}) - world:insert(e, C4({ value = true})) ecs:set(entity, D4, { value = true}) mcs:set(m, E4, { value = 2}) @@ -109,7 +93,6 @@ for i = 1, N do if flip() then combination ..= "E" registry2:set(id, B5, {value = true}) - world:insert(e, C5({value = true})) ecs:set(entity, D5, { value = true}) mcs:set(m, E5, { value = 2}) @@ -118,26 +101,20 @@ for i = 1, N do if flip() then combination ..= "F" registry2:set(id, B6, {value = true}) - world:insert(e, C6({value = true})) ecs:set(entity, D6, { value = true}) mcs:set(m, E6, { value = 2}) - newWorld:insert(n, A6({value = true})) end if flip() then combination ..= "G" registry2:set(id, B7, {value = true}) - world:insert(e, C7{ value = true}) ecs:set(entity, D7, { value = true}) mcs:set(m, E7, { value = 2}) - - newWorld:insert(n, A7({value = true})) end if flip() then combination ..= "H" registry2:set(id, B8, {value = true}) - world:insert(e, C8{ value = true}) newWorld:insert(n, A8({value = true})) ecs:set(entity, D8, { value = true}) mcs:set(m, E8, { value = 2}) @@ -148,7 +125,6 @@ for i = 1, N do combination = "A" .. combination common += 1 registry2:set(id, B1, {value = true}) - world:insert(e, C1{ value = true}) ecs:set(entity, D1, { value = true}) newWorld:insert(n, A1({value = true})) mcs:set(m, E1, { value = 2}) @@ -194,7 +170,7 @@ return { end, Functions = { - Mater = function() + Matter = function() local matched = 0 for entityId, firstComponent in newWorld:query(A1, A4, A6, A8) do matched += 1 diff --git a/lib/init.lua b/lib/init.lua index 3374970..410cb72 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -198,7 +198,7 @@ export type World = typeof(World.new()) local function ensureArchetype(world: World, types, prev) if #types < 1 then - + return world.ROOT_ARCHETYPE end local ty = hash(types) local archetype = world.archetypeIndex[ty] @@ -350,31 +350,31 @@ local function noop(): any end end -local function getSmallestMap(componentIndex, components) - local s: any - - for i, componentId in components do - local map = componentIndex[componentId] - if s == nil or map.size < s.size then - s = map - end - end - - return s.sparse -end - function World.query(world: World, ...: i53): any local compatibleArchetypes = {} local components = { ... } local archetypes = world.archetypes local queryLength = #components - local firstArchetypeMap = getSmallestMap(world.componentIndex, components) - if not firstArchetypeMap then - return noop() + if queryLength == 0 then + error("Missing components") end - for id in firstArchetypeMap do + local firstArchetypeMap + local componentIndex = world.componentIndex + + for i, componentId in components do + local map = componentIndex[componentId] + if not map then + error(tostring(componentId).." has not been added to an entity") + end + + if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then + firstArchetypeMap = map + end + end + + for id in firstArchetypeMap.sparse do local archetype = archetypes[id] local columns = archetype.columns local archetypeRecords = archetype.records