from scratch

This commit is contained in:
BlackMATov
2024-11-19 21:19:15 +07:00
parent 6317665321
commit 71fd593711
9 changed files with 9 additions and 715 deletions
+1 -34
View File
@@ -1,34 +1 @@
local evolved = require 'evolved'
local registry = evolved.registry()
local fragments = {
position = registry:entity(),
velocity = registry:entity(),
}
do
registry:entity(
fragments.position,
fragments.velocity)
end
do
local entity = registry:entity()
entity:insert(fragments.position)
entity:insert(fragments.velocity)
end
do
local query = registry:query(
fragments.position,
fragments.velocity)
for chunk in query:chunks() do
local ps = chunk.components[fragments.position]
local vs = chunk.components[fragments.position]
for i = 1, #chunk.entities do
end
end
end
local evolved = require 'evolved.evolved'
-78
View File
@@ -1,78 +0,0 @@
local iterators = {}
---@generic K
---@param t table<K, any>
---@return fun(): K
function iterators.keys(t)
if #t > 0 then
local i = 0
return function()
i = i + 1
if i <= #t then
return i
end
end
else
local k = nil
return function()
local v
k, v = next(t, k)
if k ~= nil then
return k
end
end
end
end
---@generic V
---@param t table<any, V>
---@return fun(): V?
function iterators.values(t)
if #t > 0 then
local i = 0
return function()
i = i + 1
if i <= #t then
return t[i]
end
end
else
local k = nil
return function()
local v
k, v = next(t, k)
if k ~= nil then
return v
end
end
end
end
---@generic V
---@param iter fun(): V?
---@return integer
function iterators.count(iter)
local count = 0
for _ in iter do count = count + 1 end
return count
end
---@generic V
---@param iter fun(): V?
---@param func fun(v: V): boolean
---@return fun(): V?
function iterators.filter(iter, func)
return function()
while true do
local v = iter()
if v == nil then
return
end
if func(v) then
return v
end
end
end
end
return iterators
+1 -41
View File
@@ -1,41 +1 @@
local evolved = require 'evolved'
local utilities = require 'develop.utilities'
---@param name string
---@param func fun(...):...
---@param ... any
local function describe(name, func, ...)
collectgarbage('stop')
print(string.format('| unbench | %s ...', name))
local start_s = os.clock()
local start_kb = collectgarbage('count')
local success, result = pcall(func, ...)
local finish_s = os.clock() - start_s
local finish_kb = collectgarbage('count') - start_kb
print(string.format(' %s | ms: %.2f | mb: %.2f',
success and 'OK' or 'FAILED', finish_s * 1000, finish_kb / 1024))
if not success then print(' ' .. result) end
collectgarbage('restart')
end
describe('memory footprint of 1k entities', function()
local registry = evolved.registry()
for _ = 1, 1000 do registry:entity() end
end)
describe('memory footprint of 10k entities', function()
local registry = evolved.registry()
for _ = 1, 10000 do registry:entity() end
end)
describe('memory footprint of 100k entities', function()
local registry = evolved.registry()
for _ = 1, 100000 do registry:entity() end
end)
local evolved = require 'evolved.evolved'
+1 -152
View File
@@ -1,152 +1 @@
local evolved = require 'evolved'
local iterators = require 'develop.iterators'
local utilities = require 'develop.utilities'
---@param name string
---@param func fun(...):...
---@param ... any
local function describe(name, func, ...)
collectgarbage('stop')
print(string.format('| untests | %s ...', name))
local start_s = os.clock()
local start_kb = collectgarbage('count')
local success, result = pcall(func, ...)
local finish_s = os.clock() - start_s
local finish_kb = collectgarbage('count') - start_kb
print(string.format(' %s | ms: %.2f | mb: %.2f',
success and 'OK' or 'FAILED', finish_s * 1000, finish_kb / 1024))
if not success then print(' ' .. result) end
collectgarbage('restart')
end
describe('entity:entity', function()
for _ = 1, 500 do
local registry = evolved.registry()
---@type evolved.entity[]
local all_fragments = {}
local all_fragment_count = math.random(1, 10)
for i = 1, all_fragment_count do all_fragments[i] = registry:entity() end
for _ = 1, 100 do
---@type evolved.entity[]
local insert_fragments = {}
local insert_fragment_count = math.random(1, 10)
for i = 1, insert_fragment_count do insert_fragments[i] = all_fragments[math.random(1, all_fragment_count)] end
local e1 = registry:entity()
for _, fragment in ipairs(insert_fragments) do e1:insert(fragment) end
local e2 = registry:entity(unpack(insert_fragments))
assert(e1.chunk == e2.chunk)
end
end
end)
describe('entity:chunk', function()
for _ = 1, 500 do
local registry = evolved.registry()
---@type evolved.entity[]
local all_fragments = {}
local all_fragment_count = math.random(1, 10)
for i = 1, all_fragment_count do all_fragments[i] = registry:entity() end
for _ = 1, 100 do
local e1, e2 = registry:entity(), registry:entity()
---@type evolved.entity[]
local insert_fragments = {}
local insert_fragment_count = math.random(1, 10)
for i = 1, insert_fragment_count do insert_fragments[i] = all_fragments[math.random(1, all_fragment_count)] end
---@type evolved.entity[]
local remove_fragments = {}
local remove_fragment_count = math.random(1, 10)
for i = 1, remove_fragment_count do remove_fragments[i] = all_fragments[math.random(1, all_fragment_count)] end
utilities.shuffle_array(insert_fragments)
for _, fragment in ipairs(insert_fragments) do e1:insert(fragment) end
utilities.shuffle_array(insert_fragments)
for _, fragment in ipairs(insert_fragments) do e2:insert(fragment) end
assert(e1.chunk == e2.chunk)
utilities.shuffle_array(remove_fragments)
for _, fragment in ipairs(remove_fragments) do e1:remove(fragment) end
utilities.shuffle_array(remove_fragments)
for _, fragment in ipairs(remove_fragments) do e2:remove(fragment) end
assert(e1.chunk == e2.chunk)
end
end
end)
describe('query:chunks', function()
for _ = 1, 500 do
local registry = evolved.registry()
---@type evolved.entity[]
local all_fragments = {}
local all_fragment_count = math.random(1, 10)
for i = 1, all_fragment_count do all_fragments[i] = registry:entity() end
local function check_all_queries()
for _, q in ipairs(registry.queries) do
local query_chunk_count = iterators.count(q:chunks())
local registry_chunk_count = iterators.count(iterators.filter(
iterators.values(registry.chunks),
function(chunk) return chunk:has_all_fragments(unpack(q.fragments)) end))
assert(query_chunk_count == registry_chunk_count)
end
end
---@param n integer
local function create_queries(n)
for _ = 1, n do
---@type evolved.entity[]
local query_fragments = {}
local query_fragment_count = math.random(1, 5)
for i = 1, query_fragment_count do
query_fragments[i] = all_fragments[math.random(1, all_fragment_count)]
end
registry:query(unpack(query_fragments))
end
end
---@param n integer
local function create_entities(n)
for _ = 1, n do
---@type evolved.entity[]
local insert_fragments = {}
local insert_fragment_count = math.random(1, 10)
for i = 1, insert_fragment_count do
insert_fragments[i] = all_fragments[math.random(1, all_fragment_count)]
end
local e = registry:entity()
for _, fragment in ipairs(insert_fragments) do e:insert(fragment) end
end
end
create_entities(50)
create_queries(10)
check_all_queries()
create_entities(50)
create_queries(10)
check_all_queries()
end
end)
local evolved = require 'evolved.evolved'
-11
View File
@@ -1,11 +0,0 @@
local utilities = {}
---@param vs any[]
function utilities.shuffle_array(vs)
for i = 1, #vs do
local j = math.random(i, #vs)
vs[i], vs[j] = vs[j], vs[i]
end
end
return utilities