From d75dcda3e460ce9eea909b3796fd22288a89e158 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 24 Nov 2024 04:22:14 +0700 Subject: [PATCH] cache chunk transitions, chunk children as array --- ROADMAP.md | 4 ++-- evolved/registry.lua | 45 ++++++++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 722ff5a..a08cf0e 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -9,5 +9,5 @@ - [ ] add check inserts and removes after destroying entity - [ ] add multi remove fragments function from entities - [ ] cache matched chunks in queries -- [ ] cache transitions between chunks -- [ ] chunk's children should be sorted by id and stored in an array instead of a table \ No newline at end of file +- [x] cache transitions between chunks +- [x] chunk's children should be stored in an array instead of a table \ No newline at end of file diff --git a/evolved/registry.lua b/evolved/registry.lua index daed130..3f0b31d 100644 --- a/evolved/registry.lua +++ b/evolved/registry.lua @@ -36,9 +36,11 @@ evolved_query_mt.__index = evolved_query_mt ---@class evolved.chunk ---@field package __parent? evolved.chunk ---@field package __fragment evolved.entity ----@field package __children table +---@field package __children evolved.chunk[] ---@field package __entities evolved.entity[] ---@field package __components table +---@field package __with_fragment_cache table +---@field package __without_fragment_cache table local evolved_chunk_mt = {} evolved_chunk_mt.__index = evolved_chunk_mt @@ -162,6 +164,8 @@ local function __root_chunk(fragment) __children = {}, __entities = {}, __components = { [fragment] = {} }, + __with_fragment_cache = {}, + __without_fragment_cache = {}, } setmetatable(root_chunk, evolved_chunk_mt) @@ -183,18 +187,22 @@ local function __chunk_with_fragment(chunk, fragment) return __root_chunk(fragment) end + do + local cached_chunk = chunk.__with_fragment_cache[fragment] + if cached_chunk then return cached_chunk end + end + if fragment.__guid == chunk.__fragment.__guid then return chunk end if fragment.__guid < chunk.__fragment.__guid then - local sibling_chunk = __chunk_with_fragment(chunk.__parent, fragment) - return __chunk_with_fragment(sibling_chunk, chunk.__fragment) - end - - do - local child_chunk = chunk.__children[fragment] - if child_chunk then return child_chunk end + local sibling_chunk = __chunk_with_fragment( + __chunk_with_fragment(chunk.__parent, fragment), + chunk.__fragment) + chunk.__with_fragment_cache[fragment] = sibling_chunk + sibling_chunk.__without_fragment_cache[fragment] = chunk + return sibling_chunk end ---@type evolved.chunk @@ -204,6 +212,8 @@ local function __chunk_with_fragment(chunk, fragment) __children = {}, __entities = {}, __components = { [fragment] = {} }, + __with_fragment_cache = {}, + __without_fragment_cache = {}, } for f, _ in pairs(chunk.__components) do @@ -213,7 +223,9 @@ local function __chunk_with_fragment(chunk, fragment) setmetatable(child_chunk, evolved_chunk_mt) do - chunk.__children[fragment] = child_chunk + table.insert(chunk.__children, child_chunk) + chunk.__with_fragment_cache[fragment] = child_chunk + child_chunk.__without_fragment_cache[fragment] = chunk end __on_new_chunk(child_chunk) @@ -229,13 +241,22 @@ local function __chunk_without_fragment(chunk, fragment) return nil end + do + local cached_chunk = chunk.__without_fragment_cache[fragment] + if cached_chunk then return cached_chunk end + end + if fragment.__guid == chunk.__fragment.__guid then return chunk.__parent end if fragment.__guid < chunk.__fragment.__guid then - local sibling_chunk = __chunk_without_fragment(chunk.__parent, fragment) - return __chunk_with_fragment(sibling_chunk, chunk.__fragment) + local sibling_chunk = __chunk_with_fragment( + __chunk_without_fragment(chunk.__parent, fragment), + chunk.__fragment) + chunk.__without_fragment_cache[fragment] = sibling_chunk + sibling_chunk.__with_fragment_cache[fragment] = chunk + return sibling_chunk end return chunk @@ -463,7 +484,7 @@ function registry.execute(query) local matched_chunk = matched_chunk_stack[#matched_chunk_stack] matched_chunk_stack[#matched_chunk_stack] = nil - for _, matched_chunk_child in pairs(matched_chunk.__children) do + for _, matched_chunk_child in ipairs(matched_chunk.__children) do matched_chunk_stack[#matched_chunk_stack + 1] = matched_chunk_child end