Skip to content

Commit b0d035c

Browse files
committed
migrate root chunks map to separated set/list
1 parent 8d88d55 commit b0d035c

2 files changed

Lines changed: 92 additions & 26 deletions

File tree

develop/testing/main_tests.lua

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,27 +2302,66 @@ do
23022302
evo.set(e2, f2, 44)
23032303

23042304
do
2305-
local iter, state = evo.execute(q)
2306-
local chunk = iter(state)
2307-
assert(chunk and chunk ~= evo.chunk(f1))
2305+
local e1_count = 0
2306+
local e2_count = 0
2307+
2308+
for _, entity_list, entity_count in evo.execute(q) do
2309+
for i = 1, entity_count do
2310+
if entity_list[i] == e1 then
2311+
e1_count = e1_count + 1
2312+
elseif entity_list[i] == e2 then
2313+
e2_count = e2_count + 1
2314+
end
2315+
end
2316+
end
2317+
2318+
assert(e1_count == 1)
2319+
assert(e2_count == 1)
23082320
end
23092321

23102322
evo.set(q, evo.EXCLUDES, { f2 })
23112323

23122324
do
2313-
local iter, state = evo.execute(q)
2314-
local chunk = iter(state)
2315-
assert(chunk and chunk ~= evo.chunk(f1))
2325+
local e1_count = 0
2326+
local e2_count = 0
2327+
2328+
for chunk, entity_list, entity_count in evo.execute(q) do
2329+
assert(not chunk:has(f2))
2330+
2331+
for i = 1, entity_count do
2332+
if entity_list[i] == e1 then
2333+
e1_count = e1_count + 1
2334+
elseif entity_list[i] == e2 then
2335+
e2_count = e2_count + 1
2336+
end
2337+
end
2338+
end
2339+
2340+
assert(e1_count == 1)
2341+
assert(e2_count == 0)
23162342
end
23172343

23182344
evo.set(q, evo.INCLUDES, { f1 })
23192345

23202346
do
2321-
local iter, state = evo.execute(q)
2322-
local chunk, entity_list, entity_count = iter(state)
2323-
assert(chunk == evo.chunk(f1))
2324-
assert(entity_list and entity_list[1] == e1)
2325-
assert(entity_count == 1)
2347+
local e1_count = 0
2348+
local e2_count = 0
2349+
2350+
for chunk, entity_list, entity_count in evo.execute(q) do
2351+
assert(chunk:has(f1))
2352+
assert(not chunk:has(f2))
2353+
2354+
for i = 1, entity_count do
2355+
if entity_list[i] == e1 then
2356+
e1_count = e1_count + 1
2357+
elseif entity_list[i] == e2 then
2358+
e2_count = e2_count + 1
2359+
end
2360+
end
2361+
end
2362+
2363+
assert(e1_count == 1)
2364+
assert(e2_count == 0)
23262365
end
23272366
end
23282367

evolved.lua

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ local __defer_points = {} ---@type integer[]
131131
local __defer_length = 0 ---@type integer
132132
local __defer_bytecode = {} ---@type any[]
133133

134-
local __root_chunks = {} ---@type table<evolved.fragment, evolved.chunk>
134+
local __root_set = {} ---@type table<evolved.fragment, integer>
135+
local __root_list = {} ---@type evolved.chunk[]
136+
local __root_count = 0 ---@type integer
137+
135138
local __major_chunks = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.chunk>>
136139
local __minor_chunks = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.chunk>>
137140

@@ -1281,11 +1284,17 @@ function __new_chunk(chunk_parent, chunk_fragment)
12811284
end
12821285

12831286
if not chunk_parent then
1284-
if __root_chunks[chunk_fragment] ~= nil then
1285-
__error_fmt('unexpected root chunk %s',
1286-
__lua_tostring(__root_chunks[chunk_fragment]))
1287+
local existing_root_index = __root_set[chunk_fragment]
1288+
local existing_root_chunk = __root_list[existing_root_index]
1289+
1290+
if existing_root_chunk ~= nil then
1291+
__error_fmt('unexpected root chunk (%s)',
1292+
__lua_tostring(existing_root_chunk))
12871293
end
1288-
__root_chunks[chunk_fragment] = chunk
1294+
1295+
__root_count = __root_count + 1
1296+
__root_set[chunk_fragment] = __root_count
1297+
__root_list[__root_count] = chunk
12891298
end
12901299

12911300
do
@@ -1877,7 +1886,7 @@ end
18771886
---@nodiscard
18781887
function __chunk_with_fragment(chunk, fragment)
18791888
if not chunk then
1880-
local root_chunk = __root_chunks[fragment]
1889+
local root_chunk = __root_list[__root_set[fragment]]
18811890
return root_chunk or __new_chunk(nil, fragment)
18821891
end
18831892

@@ -2063,7 +2072,7 @@ end
20632072
---@return evolved.chunk
20642073
---@nodiscard
20652074
function __chunk_fragments(head_fragment, ...)
2066-
local chunk = __root_chunks[head_fragment]
2075+
local chunk = __root_list[__root_set[head_fragment]]
20672076
or __new_chunk(nil, head_fragment)
20682077

20692078
for tail_fragment_index = 1, __lua_select('#', ...) do
@@ -2086,7 +2095,7 @@ function __chunk_components(components)
20862095
return
20872096
end
20882097

2089-
local chunk = __root_chunks[head_fragment]
2098+
local chunk = __root_list[__root_set[head_fragment]]
20902099
or __new_chunk(nil, head_fragment)
20912100

20922101
for tail_fragment in __lua_next, components, head_fragment do
@@ -2946,11 +2955,23 @@ function __purge_chunk(chunk)
29462955
local without_fragment_edges = chunk.__without_fragment_edges
29472956

29482957
if not chunk_parent then
2949-
if __root_chunks[chunk_fragment] ~= chunk then
2950-
__error_fmt('unexpected root chunk %s',
2951-
__lua_tostring(__root_chunks[chunk_fragment]))
2958+
local existing_root_index = __root_set[chunk_fragment]
2959+
local existing_root_chunk = __root_list[existing_root_index]
2960+
2961+
if existing_root_chunk ~= chunk then
2962+
__error_fmt('unexpected root chunk (%s)',
2963+
__lua_tostring(existing_root_chunk))
2964+
end
2965+
2966+
for root_index = existing_root_index, __root_count - 1 do
2967+
local next_root = __root_list[root_index + 1]
2968+
__root_set[next_root.__fragment] = root_index
2969+
__root_list[root_index] = next_root
29522970
end
2953-
__root_chunks[chunk_fragment] = nil
2971+
2972+
__root_set[chunk_fragment] = nil
2973+
__root_list[__root_count] = nil
2974+
__root_count = __root_count - 1
29542975
end
29552976

29562977
do
@@ -5885,7 +5906,9 @@ function __evolved_execute(query)
58855906
chunk_stack_size = chunk_stack_size + query_chunk_count
58865907
end
58875908
elseif query_exclude_count > 0 then
5888-
for _, root_chunk in __lua_next, __root_chunks do
5909+
for root_index = 1, __root_count do
5910+
local root_chunk = __root_list[root_index]
5911+
58895912
local is_root_chunk_matched =
58905913
not root_chunk.__has_explicit_fragments and
58915914
not query_exclude_set[root_chunk.__fragment]
@@ -5896,7 +5919,9 @@ function __evolved_execute(query)
58965919
end
58975920
end
58985921
else
5899-
for _, root_chunk in __lua_next, __root_chunks do
5922+
for root_index = 1, __root_count do
5923+
local root_chunk = __root_list[root_index]
5924+
59005925
local is_root_chunk_matched =
59015926
not root_chunk.__has_explicit_fragments
59025927

@@ -5997,7 +6022,9 @@ function __evolved_collect_garbage()
59976022
local postorder_chunk_stack ---@type evolved.chunk[]?
59986023
local postorder_chunk_stack_size = 0
59996024

6000-
for _, root_chunk in __lua_next, __root_chunks do
6025+
for root_index = 1, __root_count do
6026+
local root_chunk = __root_list[root_index]
6027+
60016028
if not working_chunk_stack then
60026029
---@type evolved.chunk[]
60036030
working_chunk_stack = __acquire_table(__table_pool_tag.chunk_list)

0 commit comments

Comments
 (0)