Skip to content

Commit a325976

Browse files
committed
make batch destroying a little faster
1 parent 1775d57 commit a325976

2 files changed

Lines changed: 123 additions & 38 deletions

File tree

develop/unbench.lua

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -610,28 +610,6 @@ basics.describe_bench(string.format('create and destroy %d entities with 5 compo
610610

611611
print '----------------------------------------'
612612

613-
basics.describe_bench(string.format('create and destroy %d entities / spawn_at', N),
614-
---@param entities evolved.id[]
615-
function(entities)
616-
local destroy = evo.destroy
617-
local spawn_at = evo.spawn_at
618-
619-
local fragments = {}
620-
local components = {}
621-
622-
local chunk = nil
623-
624-
for i = 1, N do
625-
entities[i] = spawn_at(chunk, fragments, components)
626-
end
627-
628-
for i = #entities, 1, -1 do
629-
destroy(entities[i])
630-
end
631-
end, function()
632-
return {}
633-
end)
634-
635613
basics.describe_bench(string.format('create and destroy %d entities with 1 components / spawn_at', N),
636614
---@param entities evolved.id[]
637615
function(entities)
@@ -729,26 +707,103 @@ basics.describe_bench(string.format('create and destroy %d entities with 5 compo
729707

730708
print '----------------------------------------'
731709

732-
basics.describe_bench(string.format('create and destroy %d entities / spawn_with', N),
710+
basics.describe_bench(string.format('create and destroy %d entities with 1 components / spawn_as', N),
733711
---@param entities evolved.id[]
734712
function(entities)
735-
local destroy = evo.destroy
736-
local spawn_with = evo.spawn_with
713+
local spawn_as = evo.spawn_as
714+
715+
local fragments = { F1 }
716+
local components = { true }
737717

738-
local fragments = {}
739-
local components = {}
718+
local prefab = evo.spawn_with(fragments, components)
740719

741720
for i = 1, N do
742-
entities[i] = spawn_with(fragments, components)
721+
entities[i] = spawn_as(prefab, fragments, components)
743722
end
744723

745-
for i = #entities, 1, -1 do
746-
destroy(entities[i])
724+
evo.batch_destroy(Q1)
725+
end, function()
726+
return {}
727+
end)
728+
729+
basics.describe_bench(string.format('create and destroy %d entities with 2 components / spawn_as', N),
730+
---@param entities evolved.id[]
731+
function(entities)
732+
local spawn_as = evo.spawn_as
733+
734+
local fragments = { F1, F2 }
735+
local components = { true, true }
736+
737+
local prefab = evo.spawn_with(fragments, components)
738+
739+
for i = 1, N do
740+
entities[i] = spawn_as(prefab, fragments, components)
741+
end
742+
743+
evo.batch_destroy(Q1)
744+
end, function()
745+
return {}
746+
end)
747+
748+
basics.describe_bench(string.format('create and destroy %d entities with 3 components / spawn_as', N),
749+
---@param entities evolved.id[]
750+
function(entities)
751+
local spawn_as = evo.spawn_as
752+
753+
local fragments = { F1, F2, F3 }
754+
local components = { true, true, true }
755+
756+
local prefab = evo.spawn_with(fragments, components)
757+
758+
for i = 1, N do
759+
entities[i] = spawn_as(prefab, fragments, components)
760+
end
761+
762+
evo.batch_destroy(Q1)
763+
end, function()
764+
return {}
765+
end)
766+
767+
basics.describe_bench(string.format('create and destroy %d entities with 4 components / spawn_as', N),
768+
---@param entities evolved.id[]
769+
function(entities)
770+
local spawn_as = evo.spawn_as
771+
772+
local fragments = { F1, F2, F3, F4 }
773+
local components = { true, true, true, true }
774+
775+
local prefab = evo.spawn_with(fragments, components)
776+
777+
for i = 1, N do
778+
entities[i] = spawn_as(prefab, fragments, components)
779+
end
780+
781+
evo.batch_destroy(Q1)
782+
end, function()
783+
return {}
784+
end)
785+
786+
basics.describe_bench(string.format('create and destroy %d entities with 5 components / spawn_as', N),
787+
---@param entities evolved.id[]
788+
function(entities)
789+
local spawn_as = evo.spawn_as
790+
791+
local fragments = { F1, F2, F3, F4, F5 }
792+
local components = { true, true, true, true, true }
793+
794+
local prefab = evo.spawn_with(fragments, components)
795+
796+
for i = 1, N do
797+
entities[i] = spawn_as(prefab, fragments, components)
747798
end
799+
800+
evo.batch_destroy(Q1)
748801
end, function()
749802
return {}
750803
end)
751804

805+
print '----------------------------------------'
806+
752807
basics.describe_bench(string.format('create and destroy %d entities with 1 components / spawn_with', N),
753808
---@param entities evolved.id[]
754809
function(entities)

evolved.lua

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,23 @@ local function __purge_chunk(chunk)
22242224
chunk.__unreachable_or_collected = true
22252225
end
22262226

2227+
---@param chunk_list evolved.chunk[]
2228+
---@param chunk_count integer
2229+
local function __clear_chunk_list(chunk_list, chunk_count)
2230+
if __defer_depth <= 0 then
2231+
__error_fmt('this operation should be deferred')
2232+
end
2233+
2234+
if chunk_count == 0 then
2235+
return
2236+
end
2237+
2238+
for i = 1, chunk_count do
2239+
local chunk = chunk_list[i]
2240+
__chunk_clear(chunk)
2241+
end
2242+
end
2243+
22272244
---@param entity_list evolved.entity[]
22282245
---@param entity_count integer
22292246
local function __destroy_entity_list(entity_list, entity_count)
@@ -5711,6 +5728,9 @@ __evolved_batch_destroy = function(...)
57115728
__evolved_defer()
57125729

57135730
do
5731+
local clearing_chunk_list = __acquire_table(__table_pool_tag.chunk_stack)
5732+
local clearing_chunk_count = 0
5733+
57145734
local purging_entity_list = __acquire_table(__table_pool_tag.entity_list)
57155735
local purging_entity_count = 0
57165736

@@ -5725,7 +5745,10 @@ __evolved_batch_destroy = function(...)
57255745
if __freelist_ids[query_index] ~= query then
57265746
-- this query is not alive, nothing to destroy
57275747
else
5728-
for _, entity_list, entity_count in __evolved_execute(query) do
5748+
for chunk, entity_list, entity_count in __evolved_execute(query) do
5749+
clearing_chunk_count = clearing_chunk_count + 1
5750+
clearing_chunk_list[clearing_chunk_count] = chunk
5751+
57295752
for i = 1, entity_count do
57305753
local entity = entity_list[i]
57315754

@@ -5741,19 +5764,26 @@ __evolved_batch_destroy = function(...)
57415764
end
57425765
end
57435766

5744-
if purging_entity_count > 0 then
5745-
__destroy_entity_list(purging_entity_list, purging_entity_count)
5746-
__release_table(__table_pool_tag.entity_list, purging_entity_list)
5747-
else
5748-
__release_table(__table_pool_tag.entity_list, purging_entity_list, true)
5749-
end
5750-
57515767
if purging_fragment_count > 0 then
57525768
__destroy_fragment_list(purging_fragment_list, purging_fragment_count)
57535769
__release_table(__table_pool_tag.fragment_list, purging_fragment_list)
57545770
else
57555771
__release_table(__table_pool_tag.fragment_list, purging_fragment_list, true)
57565772
end
5773+
5774+
if clearing_chunk_count > 0 then
5775+
__clear_chunk_list(clearing_chunk_list, clearing_chunk_count)
5776+
__release_table(__table_pool_tag.chunk_stack, clearing_chunk_list)
5777+
else
5778+
__release_table(__table_pool_tag.chunk_stack, clearing_chunk_list, true)
5779+
end
5780+
5781+
if purging_entity_count > 0 then
5782+
__destroy_entity_list(purging_entity_list, purging_entity_count)
5783+
__release_table(__table_pool_tag.entity_list, purging_entity_list)
5784+
else
5785+
__release_table(__table_pool_tag.entity_list, purging_entity_list, true)
5786+
end
57575787
end
57585788

57595789
__evolved_commit()

0 commit comments

Comments
 (0)