@@ -131,7 +131,10 @@ local __defer_points = {} ---@type integer[]
131131local __defer_length = 0 --- @type integer
132132local __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+
135138local __major_chunks = {} --- @type table<evolved.fragment , evolved.assoc_list<evolved.chunk>>
136139local __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
18781887function __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
20652074function __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