mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 04:13:58 -04:00
Add optional optimization for worlds and systems
This commit is contained in:
parent
56b5244541
commit
6aeb91d984
4 changed files with 58 additions and 57 deletions
63
main.lua
63
main.lua
|
@ -10,76 +10,47 @@ require(file)
|
|||
local Concord = require("src")
|
||||
|
||||
local Component = Concord.component
|
||||
local Components = Concord.components
|
||||
|
||||
local System = Concord.system
|
||||
local Systems = Concord.systems
|
||||
|
||||
local Entity = Concord.entity
|
||||
|
||||
local World = Concord.world
|
||||
local Worlds = Concord.worlds
|
||||
|
||||
|
||||
local test_comp_1 = Component(function(e, a)
|
||||
e.a = a
|
||||
end)
|
||||
|
||||
local test_comp_2 = Component(function(e, a)
|
||||
e.a = a
|
||||
end)
|
||||
|
||||
local test_comp_3 = Component()
|
||||
|
||||
local test_system_1 = System({test_comp_1})
|
||||
|
||||
function test_system_1:init()
|
||||
self.pool.onEntityAdded = function()
|
||||
print("Added to test_system 1")
|
||||
end
|
||||
self.pool.onEntityRemoved = function() print("Removed from test_system 1") end
|
||||
end
|
||||
local test_system_2 = System({test_comp_1})
|
||||
local test_system_3 = System({test_comp_1})
|
||||
|
||||
function test_system_1:test()
|
||||
print("Running test_system_1 with: " ..#self.pool)
|
||||
|
||||
for _, e in ipairs(self.pool) do
|
||||
local newE = Entity()
|
||||
newE:give(test_comp_1)
|
||||
self:getWorld():addEntity(newE)
|
||||
|
||||
e:give(test_comp_2)
|
||||
for _, _ in ipairs(self.pool) do
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local test_system_2 = System({test_comp_2})
|
||||
|
||||
function test_system_2:init()
|
||||
self.pool.onEntityAdded = function(pool, e) print("Added to test_system 2") e:remove(test_comp_1) end
|
||||
self.pool.onEntityRemoved = function() print("Removed from test_system 2") end
|
||||
end
|
||||
|
||||
function test_system_2:test()
|
||||
print("Running test_system_2 with: " ..#self.pool)
|
||||
for _, _ in ipairs(self.pool) do
|
||||
end
|
||||
end
|
||||
|
||||
for _, e in ipairs(self.pool) do
|
||||
function test_system_3:test()
|
||||
for _, _ in ipairs(self.pool) do
|
||||
end
|
||||
end
|
||||
|
||||
local world = World()
|
||||
|
||||
world:addSystems(test_system_1, test_system_2, test_system_3)
|
||||
|
||||
for _ = 1, 100 do
|
||||
local entity = Entity(world)
|
||||
entity:give(test_comp_1, 100, 100)
|
||||
end
|
||||
|
||||
world:addSystems(test_system_1, test_system_2)
|
||||
|
||||
print("Iteration: 1")
|
||||
local start = love.timer.getTime()
|
||||
for _ = 1, 1000000 do
|
||||
world:emit("test")
|
||||
end
|
||||
local stop = love.timer.getTime()
|
||||
|
||||
print("Iteration: 2")
|
||||
world:emit("test")
|
||||
|
||||
print("Iteration: 3")
|
||||
world:emit("test")
|
||||
print("Time taken: " .. stop - start)
|
|
@ -3,8 +3,12 @@
|
|||
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||
|
||||
local Pool = require(PATH..".pool")
|
||||
local Utils = require(PATH..".utils")
|
||||
|
||||
local System = {
|
||||
ENABLE_OPTIMIZATION = true,
|
||||
}
|
||||
|
||||
local System = {}
|
||||
System.mt = {
|
||||
__index = System,
|
||||
__call = function(baseSystem, world)
|
||||
|
@ -18,6 +22,13 @@ System.mt = {
|
|||
__isBaseSystem = false, -- Overwrite value from baseSystem
|
||||
}, baseSystem)
|
||||
|
||||
-- Optimization: We deep copy the World class into our instance of a world.
|
||||
-- This grants slightly faster access times at the cost of memory.
|
||||
-- Since there (generally) won't be many instances of worlds this is a worthwhile tradeoff
|
||||
if (System.ENABLE_OPTIMIZATION) then
|
||||
Utils.shallowCopy(System, system)
|
||||
end
|
||||
|
||||
for _, filter in pairs(baseSystem.__filter) do
|
||||
local pool = system:__buildPool(filter)
|
||||
if not system[pool.name] then
|
||||
|
|
9
src/utils.lua
Normal file
9
src/utils.lua
Normal file
|
@ -0,0 +1,9 @@
|
|||
local Utils = {}
|
||||
|
||||
function Utils.shallowCopy(orig, target)
|
||||
for key, value in pairs(orig) do
|
||||
target[key] = value
|
||||
end
|
||||
end
|
||||
|
||||
return Utils
|
|
@ -4,8 +4,11 @@ local PATH = (...):gsub('%.[^%.]+$', '')
|
|||
|
||||
local Type = require(PATH..".type")
|
||||
local List = require(PATH..".list")
|
||||
local Utils = require(PATH..".utils")
|
||||
|
||||
local World = {}
|
||||
local World = {
|
||||
ENABLE_OPTIMIZATION = true,
|
||||
}
|
||||
World.__index = World
|
||||
|
||||
--- Creates a new World.
|
||||
|
@ -26,6 +29,13 @@ function World.new()
|
|||
__isWorld = true,
|
||||
}, World)
|
||||
|
||||
-- Optimization: We deep copy the World class into our instance of a world.
|
||||
-- This grants slightly faster access times at the cost of memory.
|
||||
-- Since there (generally) won't be many instances of worlds this is a worthwhile tradeoff
|
||||
if (World.ENABLE_OPTIMIZATION) then
|
||||
Utils.shallowCopy(World, world)
|
||||
end
|
||||
|
||||
return world
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue