mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2025-12-15 12:19:47 +07:00
cache query major chunks: first impl
This commit is contained in:
@@ -21,6 +21,8 @@ local basics = require 'develop.basics'
|
|||||||
print '----------------------------------------'
|
print '----------------------------------------'
|
||||||
basics.describe_fuzz 'develop.fuzzing.destroy_fuzz'
|
basics.describe_fuzz 'develop.fuzzing.destroy_fuzz'
|
||||||
print '----------------------------------------'
|
print '----------------------------------------'
|
||||||
|
basics.describe_fuzz 'develop.fuzzing.execute_fuzz'
|
||||||
|
print '----------------------------------------'
|
||||||
basics.describe_fuzz 'develop.fuzzing.batch_destroy_fuzz'
|
basics.describe_fuzz 'develop.fuzzing.batch_destroy_fuzz'
|
||||||
print '----------------------------------------'
|
print '----------------------------------------'
|
||||||
basics.describe_fuzz 'develop.fuzzing.explicit_fuzz'
|
basics.describe_fuzz 'develop.fuzzing.explicit_fuzz'
|
||||||
|
|||||||
237
develop/fuzzing/execute_fuzz.lua
Normal file
237
develop/fuzzing/execute_fuzz.lua
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
local evo = require 'evolved'
|
||||||
|
|
||||||
|
evo.debug_mode(true)
|
||||||
|
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
|
||||||
|
local __table_unpack = (function()
|
||||||
|
---@diagnostic disable-next-line: deprecated
|
||||||
|
return table.unpack or unpack
|
||||||
|
end)()
|
||||||
|
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
|
||||||
|
local all_fragment_list = {} ---@type evolved.fragment[]
|
||||||
|
|
||||||
|
for i = 1, math.random(1, 10) do
|
||||||
|
local fragment = evo.id()
|
||||||
|
all_fragment_list[i] = fragment
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query evolved.query
|
||||||
|
local function generate_query(query)
|
||||||
|
local include_set = {}
|
||||||
|
local include_list = {}
|
||||||
|
local include_count = 0
|
||||||
|
|
||||||
|
for _ = 1, math.random(0, #all_fragment_list) do
|
||||||
|
local include = all_fragment_list[math.random(1, #all_fragment_list)]
|
||||||
|
|
||||||
|
if not include_set[include] then
|
||||||
|
include_count = include_count + 1
|
||||||
|
include_set[include] = include_count
|
||||||
|
include_list[include_count] = include
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local exclude_set = {}
|
||||||
|
local exclude_list = {}
|
||||||
|
local exclude_count = 0
|
||||||
|
|
||||||
|
for _ = 1, math.random(0, #all_fragment_list) do
|
||||||
|
local exclude = all_fragment_list[math.random(1, #all_fragment_list)]
|
||||||
|
|
||||||
|
if not exclude_set[exclude] then
|
||||||
|
exclude_count = exclude_count + 1
|
||||||
|
exclude_set[exclude] = exclude_count
|
||||||
|
exclude_list[exclude_count] = exclude
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if include_count > 0 then
|
||||||
|
evo.set(query, evo.INCLUDES, include_list)
|
||||||
|
end
|
||||||
|
|
||||||
|
if exclude_count > 0 then
|
||||||
|
evo.set(query, evo.EXCLUDES, exclude_list)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param query_count integer
|
||||||
|
---@return evolved.query[] query_list
|
||||||
|
---@return integer query_count
|
||||||
|
---@nodiscard
|
||||||
|
local function generate_queries(query_count)
|
||||||
|
local query_list = {} ---@type evolved.query[]
|
||||||
|
|
||||||
|
for i = 1, query_count do
|
||||||
|
local query = evo.id()
|
||||||
|
query_list[i] = query
|
||||||
|
generate_query(query)
|
||||||
|
end
|
||||||
|
|
||||||
|
return query_list, query_count
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param entity evolved.entity
|
||||||
|
local function generate_entity(entity)
|
||||||
|
for _ = 0, math.random(0, #all_fragment_list) do
|
||||||
|
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
|
||||||
|
evo.set(entity, fragment)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param entity_count integer
|
||||||
|
---@return evolved.entity[] entity_list
|
||||||
|
---@return integer entity_count
|
||||||
|
local function generate_entities(entity_count)
|
||||||
|
local entity_list = {} ---@type evolved.entity[]
|
||||||
|
|
||||||
|
for i = 1, entity_count do
|
||||||
|
local entity = evo.id()
|
||||||
|
entity_list[i] = entity
|
||||||
|
generate_entity(entity)
|
||||||
|
end
|
||||||
|
|
||||||
|
return entity_list, entity_count
|
||||||
|
end
|
||||||
|
|
||||||
|
local pre_query_list, pre_query_count = generate_queries(math.random(1, 10))
|
||||||
|
local pre_entity_list, pre_entity_count = generate_entities(math.random(1, 10))
|
||||||
|
|
||||||
|
for _ = 1, math.random(1, 5) do
|
||||||
|
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
|
||||||
|
|
||||||
|
evo.set(fragment, evo.EXPLICIT)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _ = 1, math.random(1, 5) do
|
||||||
|
local query = pre_query_list[math.random(1, pre_query_count)]
|
||||||
|
|
||||||
|
if math.random(1, 2) == 1 then
|
||||||
|
generate_query(query)
|
||||||
|
else
|
||||||
|
if math.random(1, 2) == 1 then
|
||||||
|
evo.remove(query, evo.INCLUDES)
|
||||||
|
else
|
||||||
|
evo.remove(query, evo.EXCLUDES)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local post_query_list, post_query_count = generate_queries(math.random(1, 10))
|
||||||
|
local post_entity_list, post_entity_count = generate_entities(math.random(1, 10))
|
||||||
|
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
|
||||||
|
local all_query_list = {}
|
||||||
|
local all_query_count = 0
|
||||||
|
local all_entity_list = {}
|
||||||
|
local all_entity_count = 0
|
||||||
|
|
||||||
|
for i = 1, pre_query_count do
|
||||||
|
all_query_count = all_query_count + 1
|
||||||
|
all_query_list[all_query_count] = pre_query_list[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, post_query_count do
|
||||||
|
all_query_count = all_query_count + 1
|
||||||
|
all_query_list[all_query_count] = post_query_list[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, pre_entity_count do
|
||||||
|
all_entity_count = all_entity_count + 1
|
||||||
|
all_entity_list[all_entity_count] = pre_entity_list[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, post_entity_count do
|
||||||
|
all_entity_count = all_entity_count + 1
|
||||||
|
all_entity_list[all_entity_count] = post_entity_list[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
|
||||||
|
local function execute_query(query)
|
||||||
|
local query_chunk_set = {}
|
||||||
|
local query_entity_set = {}
|
||||||
|
|
||||||
|
local query_include_list = evo.get(query, evo.INCLUDES) or {}
|
||||||
|
local query_exclude_list = evo.get(query, evo.EXCLUDES) or {}
|
||||||
|
|
||||||
|
local query_include_set = {}
|
||||||
|
for _, include in ipairs(query_include_list) do
|
||||||
|
query_include_set[include] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
for chunk, entity_list, entity_count in evo.execute(query) do
|
||||||
|
assert(not query_chunk_set[chunk])
|
||||||
|
query_chunk_set[chunk] = true
|
||||||
|
|
||||||
|
for i = 1, entity_count do
|
||||||
|
local entity = entity_list[i]
|
||||||
|
assert(not query_entity_set[entity])
|
||||||
|
query_entity_set[entity] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(chunk:has_all(__table_unpack(query_include_list)))
|
||||||
|
assert(not chunk:has_any(__table_unpack(query_exclude_list)))
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, all_entity_count do
|
||||||
|
local entity = all_entity_list[i]
|
||||||
|
|
||||||
|
local is_entity_matched =
|
||||||
|
evo.has_all(entity, __table_unpack(query_include_list))
|
||||||
|
and not evo.has_any(entity, __table_unpack(query_exclude_list))
|
||||||
|
|
||||||
|
for fragment in evo.each(entity) do
|
||||||
|
if evo.has(fragment, evo.EXPLICIT) and not query_include_set[fragment] then
|
||||||
|
is_entity_matched = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if is_entity_matched then
|
||||||
|
assert(query_entity_set[entity])
|
||||||
|
else
|
||||||
|
assert(not query_entity_set[entity])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, all_query_count do
|
||||||
|
execute_query(all_query_list[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
---
|
||||||
|
|
||||||
|
if math.random(1, 2) == 1 then
|
||||||
|
evo.collect_garbage()
|
||||||
|
end
|
||||||
|
|
||||||
|
evo.destroy(__table_unpack(all_query_list))
|
||||||
|
evo.destroy(__table_unpack(all_entity_list))
|
||||||
|
evo.destroy(__table_unpack(all_fragment_list))
|
||||||
|
|
||||||
|
if math.random(1, 2) == 1 then
|
||||||
|
evo.collect_garbage()
|
||||||
|
end
|
||||||
@@ -6449,3 +6449,161 @@ do
|
|||||||
assert(b:has_all() and not b:has_any())
|
assert(b:has_all() and not b:has_any())
|
||||||
assert(b:has(ff) and b:has(ft) and b:has_all(ff, ft) and b:has_any(ff, ft))
|
assert(b:has(ff) and b:has(ft) and b:has_all(ff, ft) and b:has_any(ff, ft))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
do
|
||||||
|
local f = evo.id()
|
||||||
|
local q = evo.builder():include(f):spawn()
|
||||||
|
local e = evo.builder():set(f, 42):spawn()
|
||||||
|
|
||||||
|
local iter, state = evo.execute(q)
|
||||||
|
local chunk, entity_list, entity_count = iter(state)
|
||||||
|
assert(chunk and entity_list and entity_count)
|
||||||
|
assert(chunk == evo.chunk(f) and entity_count == 1 and entity_list[1] == e)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local f = evo.id()
|
||||||
|
local e = evo.builder():set(f, 42):spawn()
|
||||||
|
local q = evo.builder():include(f):spawn()
|
||||||
|
|
||||||
|
local iter, state = evo.execute(q)
|
||||||
|
local chunk, entity_list, entity_count = iter(state)
|
||||||
|
assert(chunk and entity_list and entity_count)
|
||||||
|
assert(chunk == evo.chunk(f) and entity_count == 1 and entity_list[1] == e)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local f1, f2 = evo.id(2)
|
||||||
|
local q = evo.builder():exclude(f2):spawn()
|
||||||
|
local e = evo.builder():set(f1, 42):spawn()
|
||||||
|
|
||||||
|
local e_count = 0
|
||||||
|
|
||||||
|
for chunk, entity_list, entity_count in evo.execute(q) do
|
||||||
|
for i = 1, entity_count do
|
||||||
|
if entity_list[i] == e then
|
||||||
|
e_count = e_count + 1
|
||||||
|
assert(chunk == evo.chunk(f1))
|
||||||
|
assert(chunk:components(f1)[1] == 42)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(e_count == 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local f1, f2, f3 = evo.id(3)
|
||||||
|
local q = evo.builder():exclude(f2):spawn()
|
||||||
|
local e1 = evo.builder():set(f1, 42):spawn()
|
||||||
|
local e2 = evo.builder():set(f1, 42):set(f3, 21):spawn()
|
||||||
|
|
||||||
|
local e1_count, e2_count = 0, 0
|
||||||
|
|
||||||
|
for chunk, entity_list, entity_count in evo.execute(q) do
|
||||||
|
for i = 1, entity_count do
|
||||||
|
if entity_list[i] == e1 then
|
||||||
|
e1_count = e1_count + 1
|
||||||
|
assert(chunk == evo.chunk(f1))
|
||||||
|
assert(chunk:components(f1)[1] == 42)
|
||||||
|
end
|
||||||
|
|
||||||
|
if entity_list[i] == e2 then
|
||||||
|
e2_count = e2_count + 1
|
||||||
|
assert(chunk == evo.chunk(f1, f3))
|
||||||
|
assert(chunk:components(f1)[1] == 42)
|
||||||
|
assert(chunk:components(f3)[1] == 21)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(e1_count == 1)
|
||||||
|
assert(e2_count == 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local f1, f2 = evo.id(2)
|
||||||
|
local e = evo.builder():set(f1, 42):spawn()
|
||||||
|
local q = evo.builder():exclude(f2):spawn()
|
||||||
|
|
||||||
|
local e_count = 0
|
||||||
|
|
||||||
|
for chunk, entity_list, entity_count in evo.execute(q) do
|
||||||
|
for i = 1, entity_count do
|
||||||
|
if entity_list[i] == e then
|
||||||
|
e_count = e_count + 1
|
||||||
|
assert(chunk == evo.chunk(f1))
|
||||||
|
assert(chunk:components(f1)[1] == 42)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(e_count == 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local f1, f2, f3 = evo.id(3)
|
||||||
|
local e1 = evo.builder():set(f1, 42):spawn()
|
||||||
|
local e2 = evo.builder():set(f1, 42):set(f3, 21):spawn()
|
||||||
|
local q = evo.builder():exclude(f2):spawn()
|
||||||
|
|
||||||
|
local e1_count, e2_count = 0, 0
|
||||||
|
|
||||||
|
for chunk, entity_list, entity_count in evo.execute(q) do
|
||||||
|
for i = 1, entity_count do
|
||||||
|
if entity_list[i] == e1 then
|
||||||
|
e1_count = e1_count + 1
|
||||||
|
assert(chunk == evo.chunk(f1))
|
||||||
|
assert(chunk:components(f1)[1] == 42)
|
||||||
|
end
|
||||||
|
|
||||||
|
if entity_list[i] == e2 then
|
||||||
|
e2_count = e2_count + 1
|
||||||
|
assert(chunk == evo.chunk(f1, f3))
|
||||||
|
assert(chunk:components(f1)[1] == 42)
|
||||||
|
assert(chunk:components(f3)[1] == 21)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(e1_count == 1)
|
||||||
|
assert(e2_count == 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local f1, f2 = evo.id(2)
|
||||||
|
local q1 = evo.builder():include(f1):spawn()
|
||||||
|
local q2 = evo.builder():include(f2):spawn()
|
||||||
|
local e12 = evo.builder():set(f1, 42):set(f2, 21):spawn()
|
||||||
|
|
||||||
|
do
|
||||||
|
local iter, state = evo.execute(q1)
|
||||||
|
local chunk, entity_list, entity_count = iter(state)
|
||||||
|
assert(chunk and entity_list and entity_count)
|
||||||
|
assert(chunk == evo.chunk(f1, f2) and entity_count == 1 and entity_list[1] == e12)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local iter, state = evo.execute(q2)
|
||||||
|
local chunk, entity_list, entity_count = iter(state)
|
||||||
|
assert(chunk and entity_list and entity_count)
|
||||||
|
assert(chunk == evo.chunk(f1, f2) and entity_count == 1 and entity_list[1] == e12)
|
||||||
|
end
|
||||||
|
|
||||||
|
evo.set(f1, evo.EXPLICIT)
|
||||||
|
|
||||||
|
do
|
||||||
|
local iter, state = evo.execute(q1)
|
||||||
|
local chunk, entity_list, entity_count = iter(state)
|
||||||
|
assert(chunk and entity_list and entity_count)
|
||||||
|
assert(chunk == evo.chunk(f1, f2) and entity_count == 1 and entity_list[1] == e12)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local iter, state = evo.execute(q2)
|
||||||
|
local chunk, entity_list, entity_count = iter(state)
|
||||||
|
assert(not chunk and not entity_list and not entity_count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
288
evolved.lua
288
evolved.lua
@@ -126,6 +126,9 @@ local __root_chunks = {} ---@type table<evolved.fragment, evolved.chunk>
|
|||||||
local __major_chunks = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.chunk>>
|
local __major_chunks = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.chunk>>
|
||||||
local __minor_chunks = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.chunk>>
|
local __minor_chunks = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.chunk>>
|
||||||
|
|
||||||
|
local __query_chunks = {} ---@type table<evolved.query, evolved.assoc_list<evolved.chunk>>
|
||||||
|
local __major_queries = {} ---@type table<evolved.fragment, evolved.assoc_list<evolved.query>>
|
||||||
|
|
||||||
local __entity_chunks = {} ---@type table<integer, evolved.chunk>
|
local __entity_chunks = {} ---@type table<integer, evolved.chunk>
|
||||||
local __entity_places = {} ---@type table<integer, integer>
|
local __entity_places = {} ---@type table<integer, integer>
|
||||||
|
|
||||||
@@ -865,11 +868,14 @@ local __id_name
|
|||||||
local __new_chunk
|
local __new_chunk
|
||||||
|
|
||||||
local __update_chunk_caches
|
local __update_chunk_caches
|
||||||
|
local __update_chunk_queries
|
||||||
local __update_chunk_storages
|
local __update_chunk_storages
|
||||||
|
|
||||||
local __trace_major_chunks
|
local __trace_major_chunks
|
||||||
local __trace_minor_chunks
|
local __trace_minor_chunks
|
||||||
|
|
||||||
|
local __update_query_chunks
|
||||||
|
|
||||||
local __update_major_chunks
|
local __update_major_chunks
|
||||||
local __update_major_chunks_trace
|
local __update_major_chunks_trace
|
||||||
|
|
||||||
@@ -879,6 +885,7 @@ local __chunk_without_fragment
|
|||||||
local __chunk_without_fragments
|
local __chunk_without_fragments
|
||||||
local __chunk_without_unique_fragments
|
local __chunk_without_unique_fragments
|
||||||
|
|
||||||
|
local __chunk_matches
|
||||||
local __chunk_fragments
|
local __chunk_fragments
|
||||||
local __chunk_components
|
local __chunk_components
|
||||||
|
|
||||||
@@ -1064,6 +1071,7 @@ function __new_chunk(chunk_parent, chunk_fragment)
|
|||||||
end
|
end
|
||||||
|
|
||||||
__update_chunk_caches(chunk)
|
__update_chunk_caches(chunk)
|
||||||
|
__update_chunk_queries(chunk)
|
||||||
__update_chunk_storages(chunk)
|
__update_chunk_storages(chunk)
|
||||||
|
|
||||||
return chunk
|
return chunk
|
||||||
@@ -1127,6 +1135,26 @@ function __update_chunk_caches(chunk)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param chunk evolved.chunk
|
||||||
|
function __update_chunk_queries(chunk)
|
||||||
|
local chunk_major = chunk.__fragment
|
||||||
|
|
||||||
|
local major_queries = __major_queries[chunk_major]
|
||||||
|
local major_query_list = major_queries and major_queries.__item_list
|
||||||
|
local major_query_count = major_queries and major_queries.__item_count or 0
|
||||||
|
|
||||||
|
for major_query_index = 1, major_query_count do
|
||||||
|
local major_query = major_query_list[major_query_index]
|
||||||
|
local major_query_chunks = __query_chunks[major_query]
|
||||||
|
|
||||||
|
if __chunk_matches(chunk, major_query) then
|
||||||
|
__assoc_list_insert(major_query_chunks, chunk)
|
||||||
|
else
|
||||||
|
__assoc_list_remove(major_query_chunks, chunk)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param chunk evolved.chunk
|
---@param chunk evolved.chunk
|
||||||
function __update_chunk_storages(chunk)
|
function __update_chunk_storages(chunk)
|
||||||
local entity_count = chunk.__entity_count
|
local entity_count = chunk.__entity_count
|
||||||
@@ -1273,6 +1301,32 @@ function __trace_minor_chunks(minor, trace, ...)
|
|||||||
__release_table(__table_pool_tag.chunk_list, chunk_stack, true)
|
__release_table(__table_pool_tag.chunk_list, chunk_stack, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param query evolved.query
|
||||||
|
function __update_query_chunks(query)
|
||||||
|
local query_chunks = __assoc_list_new(4)
|
||||||
|
__query_chunks[query] = query_chunks
|
||||||
|
|
||||||
|
local query_includes = __sorted_includes[query]
|
||||||
|
local query_include_list = query_includes and query_includes.__item_list
|
||||||
|
local query_include_count = query_includes and query_includes.__item_count or 0
|
||||||
|
|
||||||
|
if query_include_count > 0 then
|
||||||
|
local query_major = query_include_list[query_include_count]
|
||||||
|
|
||||||
|
local major_chunks = __major_chunks[query_major]
|
||||||
|
local major_chunk_list = major_chunks and major_chunks.__item_list
|
||||||
|
local major_chunk_count = major_chunks and major_chunks.__item_count or 0
|
||||||
|
|
||||||
|
for major_chunk_index = 1, major_chunk_count do
|
||||||
|
local major_chunk = major_chunk_list[major_chunk_index]
|
||||||
|
|
||||||
|
if __chunk_matches(major_chunk, query) then
|
||||||
|
__assoc_list_insert(query_chunks, major_chunk)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param major evolved.fragment
|
---@param major evolved.fragment
|
||||||
function __update_major_chunks(major)
|
function __update_major_chunks(major)
|
||||||
__trace_major_chunks(major, __update_major_chunks_trace)
|
__trace_major_chunks(major, __update_major_chunks_trace)
|
||||||
@@ -1281,6 +1335,7 @@ end
|
|||||||
---@param chunk evolved.chunk
|
---@param chunk evolved.chunk
|
||||||
function __update_major_chunks_trace(chunk)
|
function __update_major_chunks_trace(chunk)
|
||||||
__update_chunk_caches(chunk)
|
__update_chunk_caches(chunk)
|
||||||
|
__update_chunk_queries(chunk)
|
||||||
__update_chunk_storages(chunk)
|
__update_chunk_storages(chunk)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1448,6 +1503,50 @@ function __chunk_without_unique_fragments(chunk)
|
|||||||
return sib_chunk
|
return sib_chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param chunk evolved.chunk
|
||||||
|
---@param query evolved.query
|
||||||
|
---@return boolean
|
||||||
|
---@nodiscard
|
||||||
|
function __chunk_matches(chunk, query)
|
||||||
|
local query_includes = __sorted_includes[query]
|
||||||
|
local query_include_set = query_includes and query_includes.__item_set
|
||||||
|
local query_include_list = query_includes and query_includes.__item_list
|
||||||
|
local query_include_count = query_includes and query_includes.__item_count or 0
|
||||||
|
|
||||||
|
if query_include_count > 0 then
|
||||||
|
if not __chunk_has_all_fragment_list(chunk, query_include_list, query_include_count) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
elseif chunk.__has_explicit_fragments then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local query_excludes = __sorted_excludes[query]
|
||||||
|
local query_exclude_list = query_excludes and query_excludes.__item_list
|
||||||
|
local query_exclude_count = query_excludes and query_excludes.__item_count or 0
|
||||||
|
|
||||||
|
if query_exclude_count > 0 then
|
||||||
|
if __chunk_has_any_fragment_list(chunk, query_exclude_list, query_exclude_count) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if chunk.__has_explicit_fragments then
|
||||||
|
local chunk_fragment_list = chunk.__fragment_list
|
||||||
|
local chunk_fragment_count = chunk.__fragment_count
|
||||||
|
|
||||||
|
for chunk_fragment_index = 1, chunk_fragment_count do
|
||||||
|
local chunk_fragment = chunk_fragment_list[chunk_fragment_index]
|
||||||
|
|
||||||
|
if not query_include_set[chunk_fragment] and __evolved_has(chunk_fragment, __EXPLICIT) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
---@param head_fragment evolved.fragment
|
---@param head_fragment evolved.fragment
|
||||||
---@param ... evolved.fragment tail_fragments
|
---@param ... evolved.fragment tail_fragments
|
||||||
---@return evolved.chunk
|
---@return evolved.chunk
|
||||||
@@ -5838,8 +5937,6 @@ function __evolved_execute(query)
|
|||||||
local chunk_stack_size = 0
|
local chunk_stack_size = 0
|
||||||
|
|
||||||
local query_includes = __sorted_includes[query]
|
local query_includes = __sorted_includes[query]
|
||||||
local query_include_set = query_includes and query_includes.__item_set
|
|
||||||
local query_include_list = query_includes and query_includes.__item_list
|
|
||||||
local query_include_count = query_includes and query_includes.__item_count or 0
|
local query_include_count = query_includes and query_includes.__item_count or 0
|
||||||
|
|
||||||
local query_excludes = __sorted_excludes[query]
|
local query_excludes = __sorted_excludes[query]
|
||||||
@@ -5848,87 +5945,49 @@ function __evolved_execute(query)
|
|||||||
local query_exclude_count = query_excludes and query_excludes.__item_count or 0
|
local query_exclude_count = query_excludes and query_excludes.__item_count or 0
|
||||||
|
|
||||||
if query_include_count > 0 then
|
if query_include_count > 0 then
|
||||||
local query_major = query_include_list[query_include_count]
|
local query_chunks = __query_chunks[query]
|
||||||
|
local query_chunk_list = query_chunks and query_chunks.__item_list
|
||||||
|
local query_chunk_count = query_chunks and query_chunks.__item_count or 0
|
||||||
|
|
||||||
local major_chunks = __major_chunks[query_major]
|
if query_chunk_count > 0 then
|
||||||
local major_chunk_list = major_chunks and major_chunks.__item_list
|
__lua_table_move(
|
||||||
local major_chunk_count = major_chunks and major_chunks.__item_count or 0
|
query_chunk_list, 1, query_chunk_count,
|
||||||
|
chunk_stack_size + 1, chunk_stack)
|
||||||
|
|
||||||
for major_chunk_index = 1, major_chunk_count do
|
chunk_stack_size = chunk_stack_size + query_chunk_count
|
||||||
local major_chunk = major_chunk_list[major_chunk_index]
|
|
||||||
|
|
||||||
local is_major_chunk_matched = true
|
|
||||||
|
|
||||||
if is_major_chunk_matched and query_include_count > 1 then
|
|
||||||
is_major_chunk_matched = __chunk_has_all_fragment_list(
|
|
||||||
major_chunk, query_include_list, query_include_count - 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_major_chunk_matched and query_exclude_count > 0 then
|
|
||||||
is_major_chunk_matched = not __chunk_has_any_fragment_list(
|
|
||||||
major_chunk, query_exclude_list, query_exclude_count)
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_major_chunk_matched and major_chunk.__has_explicit_minors then
|
|
||||||
local major_chunk_minor_list = major_chunk.__fragment_list
|
|
||||||
local major_chunk_minor_count = major_chunk.__fragment_count - 1
|
|
||||||
|
|
||||||
for major_chunk_fragment_index = 1, major_chunk_minor_count do
|
|
||||||
local major_chunk_minor = major_chunk_minor_list[major_chunk_fragment_index]
|
|
||||||
|
|
||||||
local is_major_chunk_minor_included = query_include_set[major_chunk_minor]
|
|
||||||
|
|
||||||
if not is_major_chunk_minor_included and __evolved_has(major_chunk_minor, __EXPLICIT) then
|
|
||||||
is_major_chunk_matched = false
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_major_chunk_matched then
|
|
||||||
chunk_stack_size = chunk_stack_size + 1
|
|
||||||
chunk_stack[chunk_stack_size] = major_chunk
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
elseif query_exclude_count > 0 then
|
||||||
---@type evolved.execute_state
|
|
||||||
local execute_state = __acquire_table(__table_pool_tag.execute_state)
|
|
||||||
|
|
||||||
execute_state[1] = __structural_changes
|
|
||||||
execute_state[2] = chunk_stack
|
|
||||||
execute_state[3] = chunk_stack_size
|
|
||||||
execute_state[4] = query_exclude_set
|
|
||||||
|
|
||||||
return __iterator_fns.__execute_iterator, execute_state
|
|
||||||
else
|
|
||||||
for _, root_chunk in __lua_next, __root_chunks do
|
for _, root_chunk in __lua_next, __root_chunks do
|
||||||
local is_root_chunk_matched = true
|
local is_root_chunk_matched =
|
||||||
|
(not root_chunk.__has_explicit_fragments) and
|
||||||
if is_root_chunk_matched and root_chunk.__has_explicit_fragments then
|
(not __chunk_has_any_fragment_list(root_chunk, query_exclude_list, query_exclude_count))
|
||||||
is_root_chunk_matched = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_root_chunk_matched and query_exclude_count > 0 then
|
|
||||||
is_root_chunk_matched = not __chunk_has_any_fragment_list(
|
|
||||||
root_chunk, query_exclude_list, query_exclude_count)
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_root_chunk_matched then
|
if is_root_chunk_matched then
|
||||||
chunk_stack_size = chunk_stack_size + 1
|
chunk_stack_size = chunk_stack_size + 1
|
||||||
chunk_stack[chunk_stack_size] = root_chunk
|
chunk_stack[chunk_stack_size] = root_chunk
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
for _, root_chunk in __lua_next, __root_chunks do
|
||||||
|
local is_root_chunk_matched =
|
||||||
|
(not root_chunk.__has_explicit_fragments)
|
||||||
|
|
||||||
---@type evolved.execute_state
|
if is_root_chunk_matched then
|
||||||
local execute_state = __acquire_table(__table_pool_tag.execute_state)
|
chunk_stack_size = chunk_stack_size + 1
|
||||||
|
chunk_stack[chunk_stack_size] = root_chunk
|
||||||
execute_state[1] = __structural_changes
|
end
|
||||||
execute_state[2] = chunk_stack
|
end
|
||||||
execute_state[3] = chunk_stack_size
|
|
||||||
execute_state[4] = query_exclude_set
|
|
||||||
|
|
||||||
return __iterator_fns.__execute_iterator, execute_state
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@type evolved.execute_state
|
||||||
|
local execute_state = __acquire_table(__table_pool_tag.execute_state)
|
||||||
|
|
||||||
|
execute_state[1] = __structural_changes
|
||||||
|
execute_state[2] = chunk_stack
|
||||||
|
execute_state[3] = chunk_stack_size
|
||||||
|
execute_state[4] = query_exclude_set
|
||||||
|
|
||||||
|
return __iterator_fns.__execute_iterator, execute_state
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param entity evolved.entity
|
---@param entity evolved.entity
|
||||||
@@ -6904,27 +6963,69 @@ __evolved_set(__ON_REMOVE, __UNIQUE)
|
|||||||
---
|
---
|
||||||
---
|
---
|
||||||
|
|
||||||
|
local function __insert_major_query(query)
|
||||||
|
local query_includes = __sorted_includes[query]
|
||||||
|
local query_include_list = query_includes and query_includes.__item_list
|
||||||
|
local query_include_count = query_includes and query_includes.__item_count or 0
|
||||||
|
|
||||||
|
if query_include_count > 0 then
|
||||||
|
local query_major = query_include_list[query_include_count]
|
||||||
|
local major_queries = __major_queries[query_major]
|
||||||
|
|
||||||
|
if not major_queries then
|
||||||
|
major_queries = __assoc_list_new(4)
|
||||||
|
__major_queries[query_major] = major_queries
|
||||||
|
end
|
||||||
|
|
||||||
|
__assoc_list_insert(major_queries, query)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function __remove_major_query(query)
|
||||||
|
local query_includes = __sorted_includes[query]
|
||||||
|
local query_include_list = query_includes and query_includes.__item_list
|
||||||
|
local query_include_count = query_includes and query_includes.__item_count or 0
|
||||||
|
|
||||||
|
if query_include_count > 0 then
|
||||||
|
local query_major = query_include_list[query_include_count]
|
||||||
|
local major_queries = __major_queries[query_major]
|
||||||
|
|
||||||
|
if major_queries and __assoc_list_remove(major_queries, query) == 0 then
|
||||||
|
__major_queries[query_major] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param query evolved.query
|
---@param query evolved.query
|
||||||
---@param include_list evolved.fragment[]
|
---@param include_list evolved.fragment[]
|
||||||
__evolved_set(__INCLUDES, __ON_SET, function(query, _, include_list)
|
__evolved_set(__INCLUDES, __ON_SET, function(query, _, include_list)
|
||||||
|
__remove_major_query(query)
|
||||||
|
|
||||||
local include_count = #include_list
|
local include_count = #include_list
|
||||||
|
|
||||||
if include_count == 0 then
|
if include_count > 0 then
|
||||||
|
---@type evolved.assoc_list<evolved.fragment>
|
||||||
|
local sorted_includes = __assoc_list_new(include_count)
|
||||||
|
|
||||||
|
__assoc_list_move(include_list, 1, include_count, sorted_includes)
|
||||||
|
__assoc_list_sort(sorted_includes)
|
||||||
|
|
||||||
|
__sorted_includes[query] = sorted_includes
|
||||||
|
else
|
||||||
__sorted_includes[query] = nil
|
__sorted_includes[query] = nil
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type evolved.assoc_list<evolved.fragment>
|
__insert_major_query(query)
|
||||||
local sorted_includes = __assoc_list_new(include_count)
|
__update_query_chunks(query)
|
||||||
|
|
||||||
__assoc_list_move(include_list, 1, include_count, sorted_includes)
|
|
||||||
__assoc_list_sort(sorted_includes)
|
|
||||||
|
|
||||||
__sorted_includes[query] = sorted_includes
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
__evolved_set(__INCLUDES, __ON_REMOVE, function(query)
|
__evolved_set(__INCLUDES, __ON_REMOVE, function(query)
|
||||||
|
if not __sorted_excludes[query] then
|
||||||
|
__remove_major_query(query)
|
||||||
|
end
|
||||||
|
|
||||||
__sorted_includes[query] = nil
|
__sorted_includes[query] = nil
|
||||||
|
__update_query_chunks(query)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -6936,24 +7037,33 @@ end)
|
|||||||
---@param query evolved.query
|
---@param query evolved.query
|
||||||
---@param exclude_list evolved.fragment[]
|
---@param exclude_list evolved.fragment[]
|
||||||
__evolved_set(__EXCLUDES, __ON_SET, function(query, _, exclude_list)
|
__evolved_set(__EXCLUDES, __ON_SET, function(query, _, exclude_list)
|
||||||
|
__remove_major_query(query)
|
||||||
|
|
||||||
local exclude_count = #exclude_list
|
local exclude_count = #exclude_list
|
||||||
|
|
||||||
if exclude_count == 0 then
|
if exclude_count > 0 then
|
||||||
|
---@type evolved.assoc_list<evolved.fragment>
|
||||||
|
local sorted_excludes = __assoc_list_new(exclude_count)
|
||||||
|
|
||||||
|
__assoc_list_move(exclude_list, 1, exclude_count, sorted_excludes)
|
||||||
|
__assoc_list_sort(sorted_excludes)
|
||||||
|
|
||||||
|
__sorted_excludes[query] = sorted_excludes
|
||||||
|
else
|
||||||
__sorted_excludes[query] = nil
|
__sorted_excludes[query] = nil
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type evolved.assoc_list<evolved.fragment>
|
__insert_major_query(query)
|
||||||
local sorted_excludes = __assoc_list_new(exclude_count)
|
__update_query_chunks(query)
|
||||||
|
|
||||||
__assoc_list_move(exclude_list, 1, exclude_count, sorted_excludes)
|
|
||||||
__assoc_list_sort(sorted_excludes)
|
|
||||||
|
|
||||||
__sorted_excludes[query] = sorted_excludes
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
__evolved_set(__EXCLUDES, __ON_REMOVE, function(query)
|
__evolved_set(__EXCLUDES, __ON_REMOVE, function(query)
|
||||||
|
if not __sorted_includes[query] then
|
||||||
|
__remove_major_query(query)
|
||||||
|
end
|
||||||
|
|
||||||
__sorted_excludes[query] = nil
|
__sorted_excludes[query] = nil
|
||||||
|
__update_query_chunks(query)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
Reference in New Issue
Block a user