Skip to content

Commit

Permalink
Save column indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Jul 30, 2024
1 parent d5baf52 commit 023e7ac
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 130 deletions.
8 changes: 6 additions & 2 deletions benches/visual/query.bench.luau
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ return {
end,

Functions = {
Mirror = function()
for entityId, firstComponent in mcs:query(E1, E2, E3, E4) do
Matter = function()
for entityId, firstComponent in newWorld:query(A1, A2, A3, A4) do
end
end,

ECR = function()
for entityId, firstComponent in registry2:view(B1, B2, B3, B3) do
end
end,

Jecs = function()
Expand Down
Binary file modified image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed image.png
Binary file not shown.
258 changes: 137 additions & 121 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,15 @@ do
local A, B, C, D, E, F, G, H
local a, b, c, d, e, f, g, h

local init
local drain

local function query_init(query)
if init and drain then
return
end

init = true
lastArchetype = 1
archetype = compatible_archetypes[lastArchetype]

Expand All @@ -767,157 +775,159 @@ do

local records = archetype.records
if not B then
a = records[A]
a = columns[records[A]]
elseif not C then
a = records[A]
b = records[B]
a = columns[records[A]]
b = columns[records[B]]
elseif not D then
a = records[A]
b = records[B]
c = records[C]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
elseif not E then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
elseif not F then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
e = columns[records[E]]
elseif not G then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
f = records[F]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
e = columns[records[E]]
f = columns[records[F]]
elseif not H then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
f = records[F]
g = records[G]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
e = columns[records[E]]
f = columns[records[F]]
g = columns[records[G]]
elseif H then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
f = records[F]
g = records[G]
h = records[H]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[D]]
e = columns[records[E]]
f = columns[records[F]]
g = columns[records[G]]
h = columns[records[H]]
end
end

local function world_query_next(): any
local function world_query_iter_next(): any
local entityId = entities[i]
while entityId == nil do
lastArchetype += 1
archetype = compatible_archetypes[lastArchetype]
if not archetype then
return nil
end

entities = archetype.entities
i = #entities
if i == 0 then
continue
end
entityId = entities[i]
columns = archetype.columns
local records = archetype.records
if not B then
a = records[A]
a = columns[records[A]]
elseif not C then
a = records[A]
b = records[B]
a = columns[records[A]]
b = columns[records[B]]
elseif not D then
a = records[A]
b = records[B]
c = records[C]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
elseif not E then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
elseif not F then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
e = columns[records[E]]
elseif not G then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
f = records[F]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
e = columns[records[E]]
f = columns[records[F]]
elseif not H then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
f = records[F]
g = records[G]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[C]]
d = columns[records[D]]
e = columns[records[E]]
f = columns[records[F]]
g = columns[records[G]]
elseif H then
a = records[A]
b = records[B]
c = records[C]
d = records[D]
e = records[E]
f = records[F]
g = records[G]
h = records[H]
a = columns[records[A]]
b = columns[records[B]]
c = columns[records[D]]
e = columns[records[E]]
f = columns[records[F]]
g = columns[records[G]]
h = columns[records[H]]
end
columns = archetype.columns
entities = archetype.entities
i = #entities
entityId = entities[i]
end

local row = i
i-=1

if queryLength == 1 then
return entityId, columns[a][row]
return entityId, a[row]
elseif queryLength == 2 then
return entityId, columns[a][row], columns[b][row]
return entityId, a[row], b[row]
elseif queryLength == 3 then
return entityId, columns[a][row], columns[b][row], columns[c][row]
return entityId, a[row], b[row], c[row]
elseif queryLength == 4 then
return entityId, columns[a][row], columns[b][row], columns[c][row], columns[d][row]
return entityId, a[row], b[row], c[row], d[row]
elseif queryLength == 5 then
return entityId,
columns[a][row],
columns[b][row],
columns[c][row],
columns[d][row],
columns[e][row]
a[row],
b[row],
c[row],
d[row],
e[row]
elseif queryLength == 6 then
return entityId,
columns[a][row],
columns[b][row],
columns[c][row],
columns[d][row],
columns[e][row],
columns[f][row]
a[row],
b[row],
c[row],
d[row],
e[row],
f[row]
elseif queryLength == 7 then
return entityId,
columns[a][row],
columns[b][row],
columns[c][row],
columns[d][row],
columns[e][row],
columns[f][row],
columns[g][row]
a[row],
b[row],
c[row],
d[row],
e[row],
f[row],
g[row]
elseif queryLength == 8 then
return entityId,
columns[a][row],
columns[b][row],
columns[c][row],
columns[d][row],
columns[e][row],
columns[f][row],
columns[g][row],
columns[h][row]
a[row],
b[row],
c[row],
d[row],
e[row],
f[row],
g[row],
h[row]
end

local field = archetype.records
Expand Down Expand Up @@ -968,26 +978,26 @@ do
local tr = archetype.records
for row in archetype.entities do
if queryLength == 1 then
local va = columns[tr[a]]
local va = columns[tr[A]]
local pa = fn(va[row])

va[row] = pa
elseif queryLength == 2 then
local va = columns[tr[a]]
local vb = columns[tr[b]]
local va = columns[tr[A]]
local vb = columns[tr[B]]

va[row], vb[row] = fn(va[row], vb[row])
elseif queryLength == 3 then
local va = columns[tr[a]]
local vb = columns[tr[b]]
local vc = columns[tr[c]]
local va = columns[tr[A]]
local vb = columns[tr[B]]
local vc = columns[tr[C]]

va[row], vb[row], vc[row] = fn(va[row], vb[row], vc[row])
elseif queryLength == 4 then
local va = columns[tr[a]]
local vb = columns[tr[b]]
local vc = columns[tr[c]]
local vd = columns[tr[d]]
local va = columns[tr[A]]
local vb = columns[tr[B]]
local vc = columns[tr[C]]
local vd = columns[tr[D]]

va[row], vb[row], vc[row], vd[row] = fn(
va[row], vb[row], vc[row], vd[row])
Expand Down Expand Up @@ -1026,6 +1036,8 @@ do
end
end

query_init(query)

return query
end

Expand All @@ -1036,19 +1048,22 @@ do
return compatible_archetypes
end

local drain

local function world_query_drain(query)
drain = true
query_init(query)
return query
end

local function world_query_iter(query)
query_init(query)
return world_query_iter_next
end

local function world_query_next()
if not drain then
query_init(query)
error("Did you forget to call query:drain()?")
end
return world_query_next
return world_query_iter_next()
end

local it = {
Expand Down Expand Up @@ -1113,6 +1128,7 @@ do
end

drain = false
init = false
ids = components

return it
Expand Down
Loading

0 comments on commit 023e7ac

Please sign in to comment.