improve entity builder performance

This commit is contained in:
BlackMATov
2025-04-10 14:53:22 +07:00
parent 8c90df60e1
commit 7e15e57154
3 changed files with 52 additions and 27 deletions

View File

@@ -3,6 +3,7 @@
## Backlog ## Backlog
- builders should be rewritten :/ - builders should be rewritten :/
- add PREFAB entity trait
- is/has_all/any for lists? - is/has_all/any for lists?
## After first release ## After first release

View File

@@ -3158,6 +3158,20 @@ do
end end
end end
do
local f1, f2 = evo.id(2)
local eb = evo.entity()
local e1 = eb:set(f1, 1):set(f2, 2):build()
local e2 = eb:set(f1, 11):build()
assert(evo.has(e1, f1) and evo.get(e1, f1) == 1)
assert(evo.has(e1, f2) and evo.get(e1, f2) == 2)
assert(evo.has(e2, f1) and evo.get(e2, f1) == 11)
assert(not evo.has(e2, f2) and evo.get(e2, f2) == nil)
end
do do
local f1, f2, f3 = evo.id(3) local f1, f2, f3 = evo.id(3)

View File

@@ -4139,6 +4139,10 @@ end
---@param components evolved.component[] ---@param components evolved.component[]
---@param component_count integer ---@param component_count integer
function __defer_spawn_entity_at(entity, chunk, fragments, fragment_count, components, component_count) function __defer_spawn_entity_at(entity, chunk, fragments, fragment_count, components, component_count)
if component_count > fragment_count then
component_count = fragment_count
end
---@type evolved.fragment[] ---@type evolved.fragment[]
local fragment_list = __acquire_table(__table_pool_tag.fragment_list) local fragment_list = __acquire_table(__table_pool_tag.fragment_list)
__lua_table_move(fragments, 1, fragment_count, 1, fragment_list) __lua_table_move(fragments, 1, fragment_count, 1, fragment_list)
@@ -4190,6 +4194,10 @@ end
---@param components evolved.component[] ---@param components evolved.component[]
---@param component_count integer ---@param component_count integer
function __defer_spawn_entity_as(entity, prefab, fragments, fragment_count, components, component_count) function __defer_spawn_entity_as(entity, prefab, fragments, fragment_count, components, component_count)
if component_count > fragment_count then
component_count = fragment_count
end
---@type evolved.fragment[] ---@type evolved.fragment[]
local fragment_list = __acquire_table(__table_pool_tag.fragment_list) local fragment_list = __acquire_table(__table_pool_tag.fragment_list)
__lua_table_move(fragments, 1, fragment_count, 1, fragment_list) __lua_table_move(fragments, 1, fragment_count, 1, fragment_list)
@@ -4241,6 +4249,10 @@ end
---@param components evolved.component[] ---@param components evolved.component[]
---@param component_count integer ---@param component_count integer
function __defer_spawn_entity_with(entity, chunk, fragments, fragment_count, components, component_count) function __defer_spawn_entity_with(entity, chunk, fragments, fragment_count, components, component_count)
if component_count > fragment_count then
component_count = fragment_count
end
---@type evolved.fragment[] ---@type evolved.fragment[]
local fragment_list = __acquire_table(__table_pool_tag.fragment_list) local fragment_list = __acquire_table(__table_pool_tag.fragment_list)
__lua_table_move(fragments, 1, fragment_count, 1, fragment_list) __lua_table_move(fragments, 1, fragment_count, 1, fragment_list)
@@ -6154,9 +6166,9 @@ end
local __builder_fns = {} local __builder_fns = {}
---@class evolved.entity_builder ---@class evolved.entity_builder
---@field package __fragment_list? evolved.fragment[] ---@field package __fragment_list evolved.fragment[]
---@field package __component_list? evolved.component[] ---@field package __component_list evolved.component[]
---@field package __component_count? integer ---@field package __component_count integer
__builder_fns.entity_builder = {} __builder_fns.entity_builder = {}
__builder_fns.entity_builder.__index = __builder_fns.entity_builder __builder_fns.entity_builder.__index = __builder_fns.entity_builder
@@ -6203,7 +6215,11 @@ __builder_fns.system_builder.__index = __builder_fns.system_builder
---@return evolved.entity_builder builder ---@return evolved.entity_builder builder
---@nodiscard ---@nodiscard
function __evolved_entity() function __evolved_entity()
return __lua_setmetatable({}, __builder_fns.entity_builder) return __lua_setmetatable({
__fragment_list = {},
__component_list = {},
__component_count = 0,
}, __builder_fns.entity_builder)
end end
---@param fragment evolved.fragment ---@param fragment evolved.fragment
@@ -6212,19 +6228,8 @@ end
function __builder_fns.entity_builder:set(fragment, component) function __builder_fns.entity_builder:set(fragment, component)
local fragment_list = self.__fragment_list local fragment_list = self.__fragment_list
local component_list = self.__component_list local component_list = self.__component_list
local component_count = self.__component_count or 0
if component_count == 0 then local component_count = self.__component_count + 1
fragment_list = __acquire_table(__table_pool_tag.fragment_list)
component_list = __acquire_table(__table_pool_tag.component_list)
self.__fragment_list = fragment_list
self.__component_list = component_list
end
---@cast fragment_list -?
---@cast component_list -?
component_count = component_count + 1
self.__component_count = component_count self.__component_count = component_count
fragment_list[component_count] = fragment fragment_list[component_count] = fragment
@@ -6237,23 +6242,28 @@ end
function __builder_fns.entity_builder:build() function __builder_fns.entity_builder:build()
local fragment_list = self.__fragment_list local fragment_list = self.__fragment_list
local component_list = self.__component_list local component_list = self.__component_list
local component_count = self.__component_count or 0
self.__fragment_list = nil local component_count = self.__component_count
self.__component_list = nil self.__component_count = 0
self.__component_count = nil
if component_count == 0 then if __debug_mode then
return __evolved_id() __debug_fns.validate_fragment_list(fragment_list, component_count)
end end
---@cast fragment_list -? local entity = __acquire_id()
---@cast component_list -? local entity_chunk = __chunk_fragment_list(fragment_list, component_count)
local entity = __evolved_spawn_with(fragment_list, component_list) if __defer_depth > 0 then
__defer_spawn_entity_with(entity, entity_chunk,
fragment_list, component_count,
component_list, component_count)
end
__release_table(__table_pool_tag.fragment_list, fragment_list) __evolved_defer()
__release_table(__table_pool_tag.component_list, component_list) do
__spawn_entity_with(entity, entity_chunk, fragment_list, component_count, component_list)
end
__evolved_commit()
return entity return entity
end end