the brand new destroying entity/fragment process

This commit is contained in:
BlackMATov
2025-04-05 00:56:52 +07:00
parent cfdf740b76
commit 1629e4ecc3
10 changed files with 545 additions and 192 deletions

View File

@@ -2,3 +2,8 @@ require 'develop.example'
require 'develop.unbench'
require 'develop.untests'
require 'develop.usbench'
local basics = require 'develop.basics'
basics.describe_fuzz 'develop.fuzzing.destroy_fuzz'
basics.describe_fuzz 'develop.fuzzing.batch_destroy_fuzz'

View File

@@ -12,6 +12,48 @@ local __table_unpack = (function()
return table.unpack or unpack
end)()
---@param pattern string
function basics.unload(pattern)
for name, _ in pairs(package.loaded) do
if name:match(pattern) then
package.loaded[name] = nil
end
end
end
---@param modname string
function basics.describe_fuzz(modname)
print(string.format('| %s ... |', modname))
do
local iters = 0
local start_s = os.clock()
local start_kb = collectgarbage('count')
local success, result = pcall(function()
repeat
iters = iters + 1
basics.unload(modname)
require(modname)
until os.clock() - start_s > 0.5
end)
local finish_s = os.clock()
local finish_kb = collectgarbage('count')
if success then
print(string.format('|-- PASS | us: %.2f | op/s: %.2f | kb/i: %.2f | iters: %d',
(finish_s - start_s) * 1e6 / iters,
iters / (finish_s - start_s),
(finish_kb - start_kb) / iters,
iters))
else
print('|-- FUZZ FAIL: ' .. result)
end
end
end
---@param name string
---@param loop fun(...): ...
---@param init? fun(): ...

View File

@@ -0,0 +1,121 @@
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_entity_list = {} ---@type evolved.entity[]
for i = 1, math.random(1, 10) do
local entity = evo.id()
all_entity_list[i] = entity
end
for _, entity in ipairs(all_entity_list) do
for _ = 0, math.random(0, #all_entity_list) do
local fragment = all_entity_list[math.random(1, #all_entity_list)]
evo.set(entity, fragment)
end
if math.random(1, 5) == 1 then
evo.set(entity, evo.DESTROY_POLICY, evo.DESTROY_POLICY_DESTROY_ENTITY)
end
if math.random(1, 5) == 1 then
evo.set(entity, evo.DESTROY_POLICY, evo.DESTROY_POLICY_REMOVE_FRAGMENT)
end
end
---
---
---
---
---
local should_be_destroyed_entity_set = {} ---@type table<evolved.entity, integer>
local should_be_destroyed_entity_list = {} ---@type evolved.entity[]
local should_be_destroyed_entity_count = 0 ---@type integer
local function collect_destroyed_entities_with(entity)
local entity_destroy_policy = evo.get(entity, evo.DESTROY_POLICY)
or evo.DESTROY_POLICY_REMOVE_FRAGMENT
if entity_destroy_policy == evo.DESTROY_POLICY_DESTROY_ENTITY then
for _, other_entity in ipairs(all_entity_list) do
if evo.has(other_entity, entity) and not should_be_destroyed_entity_set[other_entity] then
should_be_destroyed_entity_count = should_be_destroyed_entity_count + 1
should_be_destroyed_entity_list[should_be_destroyed_entity_count] = other_entity
should_be_destroyed_entity_set[other_entity] = should_be_destroyed_entity_count
end
end
end
end
local destroying_include_list = {} ---@type evolved.entity[]
for i = 1, math.random(1, #all_entity_list) do
local destroying_include = all_entity_list[math.random(1, #all_entity_list)]
destroying_include_list[i] = destroying_include
end
for _, entity in ipairs(all_entity_list) do
if evo.has_all(entity, __table_unpack(destroying_include_list)) then
collect_destroyed_entities_with(entity)
end
end
do
local r = math.random(1, 2)
local q = evo.query():include(__table_unpack(destroying_include_list)):build()
if r == 1 then
evo.batch_destroy(q)
elseif r == 2 then
assert(evo.defer())
evo.batch_destroy(q)
assert(evo.commit())
end
end
---
---
---
---
---
local all_chunk_query = evo.query():build()
for chunk in evo.execute(all_chunk_query) do
assert(not evo.has_any(chunk, __table_unpack(should_be_destroyed_entity_list)))
for _, fragment in ipairs(evo.fragments(chunk)) do
assert(not evo.has_all(fragment, __table_unpack(destroying_include_list)))
end
end
for _, destroyed_entity in ipairs(should_be_destroyed_entity_list) do
assert(not evo.is_alive(destroyed_entity))
end
---
---
---
---
---
evo.destroy(__table_unpack(all_entity_list))
evo.collect_garbage()

View File

@@ -0,0 +1,127 @@
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_entity_list = {} ---@type evolved.entity[]
for i = 1, math.random(1, 10) do
local entity = evo.id()
all_entity_list[i] = entity
end
for _, entity in ipairs(all_entity_list) do
for _ = 0, math.random(0, #all_entity_list) do
local fragment = all_entity_list[math.random(1, #all_entity_list)]
evo.set(entity, fragment)
end
if math.random(1, 5) == 1 then
evo.set(entity, evo.DESTROY_POLICY, evo.DESTROY_POLICY_DESTROY_ENTITY)
end
if math.random(1, 5) == 1 then
evo.set(entity, evo.DESTROY_POLICY, evo.DESTROY_POLICY_REMOVE_FRAGMENT)
end
end
---
---
---
---
---
local should_be_destroyed_entity_set = {} ---@type table<evolved.entity, integer>
local should_be_destroyed_entity_list = {} ---@type evolved.entity[]
local should_be_destroyed_entity_count = 0 ---@type integer
local function collect_destroyed_entities_with(entity)
local entity_destroy_policy = evo.get(entity, evo.DESTROY_POLICY)
or evo.DESTROY_POLICY_REMOVE_FRAGMENT
if entity_destroy_policy == evo.DESTROY_POLICY_DESTROY_ENTITY then
for _, other_entity in ipairs(all_entity_list) do
if evo.has(other_entity, entity) and not should_be_destroyed_entity_set[other_entity] then
should_be_destroyed_entity_count = should_be_destroyed_entity_count + 1
should_be_destroyed_entity_list[should_be_destroyed_entity_count] = other_entity
should_be_destroyed_entity_set[other_entity] = should_be_destroyed_entity_count
end
end
end
end
local destroying_entity_list = {} ---@type evolved.entity[]
for i = 1, math.random(1, #all_entity_list) do
local destroying_entity = all_entity_list[math.random(1, #all_entity_list)]
destroying_entity_list[i] = destroying_entity
collect_destroyed_entities_with(destroying_entity)
end
do
local r = math.random(1, 4)
if r == 1 then
evo.destroy(__table_unpack(destroying_entity_list))
elseif r == 2 then
assert(evo.defer())
evo.destroy(__table_unpack(destroying_entity_list))
assert(evo.commit())
elseif r == 3 then
for _, destroying_entity in ipairs(destroying_entity_list) do
evo.destroy(destroying_entity)
end
elseif r == 4 then
assert(evo.defer())
for _, destroying_entity in ipairs(destroying_entity_list) do
evo.destroy(destroying_entity)
end
assert(evo.commit())
end
end
---
---
---
---
---
local all_chunk_query = evo.query():build()
for chunk in evo.execute(all_chunk_query) do
assert(not evo.has_any(chunk, __table_unpack(destroying_entity_list)))
assert(not evo.has_any(chunk, __table_unpack(should_be_destroyed_entity_list)))
end
for _, destroying_entity in ipairs(destroying_entity_list) do
assert(not evo.is_alive(destroying_entity))
end
for _, destroyed_entity in ipairs(should_be_destroyed_entity_list) do
assert(not evo.is_alive(destroyed_entity))
end
---
---
---
---
---
evo.destroy(__table_unpack(all_entity_list))
evo.collect_garbage()

View File

@@ -1,5 +1,5 @@
require 'develop.unload' 'evolved'
local basics = require 'develop.basics'
basics.unload 'evolved'
local evo = require 'evolved'

View File

@@ -1,8 +0,0 @@
---@param pattern string
return function(pattern)
for name, _ in pairs(package.loaded) do
if name:match(pattern) then
package.loaded[name] = nil
end
end
end

View File

@@ -1,4 +1,5 @@
require 'develop.unload' 'evolved'
local basics = require 'develop.basics'
basics.unload 'evolved'
local evo = require 'evolved'
@@ -6246,7 +6247,7 @@ do
do
local c4_es, c4_ec = evo.entities(c4)
assert(c4_es and #c4_es == 3 and c4_ec == 3)
assert(c4_es[1] == e14 and c4_es[2] == e24 and c4_es[3] == e124)
assert(c4_es[1] == e24 and c4_es[2] == e14 and c4_es[3] == e124)
end
assert(#evo.entities(c14) == 0)

View File

@@ -1,5 +1,5 @@
require 'develop.unload' 'evolved'
local basics = require 'develop.basics'
basics.unload 'evolved'
local evo = require 'evolved'
local tiny = require 'develop.3rdparty.tiny'