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
75
main.lua
75
main.lua
|
@ -9,77 +9,48 @@ require(file)
|
||||||
|
|
||||||
local Concord = require("src")
|
local Concord = require("src")
|
||||||
|
|
||||||
local Component = Concord.component
|
local Component = Concord.component
|
||||||
local Components = Concord.components
|
local System = Concord.system
|
||||||
|
local Entity = Concord.entity
|
||||||
local System = Concord.system
|
local World = Concord.world
|
||||||
local Systems = Concord.systems
|
|
||||||
|
|
||||||
local Entity = Concord.entity
|
|
||||||
|
|
||||||
local World = Concord.world
|
|
||||||
local Worlds = Concord.worlds
|
|
||||||
|
|
||||||
|
|
||||||
local test_comp_1 = Component(function(e, a)
|
local test_comp_1 = Component(function(e, a)
|
||||||
e.a = a
|
e.a = a
|
||||||
end)
|
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})
|
local test_system_1 = System({test_comp_1})
|
||||||
|
local test_system_2 = System({test_comp_1})
|
||||||
function test_system_1:init()
|
local test_system_3 = System({test_comp_1})
|
||||||
self.pool.onEntityAdded = function()
|
|
||||||
print("Added to test_system 1")
|
|
||||||
end
|
|
||||||
self.pool.onEntityRemoved = function() print("Removed from test_system 1") end
|
|
||||||
end
|
|
||||||
|
|
||||||
function test_system_1:test()
|
function test_system_1:test()
|
||||||
print("Running test_system_1 with: " ..#self.pool)
|
for _, _ in ipairs(self.pool) do
|
||||||
|
|
||||||
for _, e in ipairs(self.pool) do
|
|
||||||
local newE = Entity()
|
|
||||||
newE:give(test_comp_1)
|
|
||||||
self:getWorld():addEntity(newE)
|
|
||||||
|
|
||||||
e:give(test_comp_2)
|
|
||||||
end
|
end
|
||||||
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()
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local world = World()
|
local world = World()
|
||||||
|
|
||||||
local entity = Entity(world)
|
world:addSystems(test_system_1, test_system_2, test_system_3)
|
||||||
entity:give(test_comp_1, 100, 100)
|
|
||||||
|
|
||||||
world:addSystems(test_system_1, test_system_2)
|
for _ = 1, 100 do
|
||||||
|
local entity = Entity(world)
|
||||||
|
entity:give(test_comp_1, 100, 100)
|
||||||
|
end
|
||||||
|
|
||||||
print("Iteration: 1")
|
|
||||||
world:emit("test")
|
|
||||||
|
|
||||||
print("Iteration: 2")
|
local start = love.timer.getTime()
|
||||||
world:emit("test")
|
for _ = 1, 1000000 do
|
||||||
|
world:emit("test")
|
||||||
|
end
|
||||||
|
local stop = love.timer.getTime()
|
||||||
|
|
||||||
print("Iteration: 3")
|
print("Time taken: " .. stop - start)
|
||||||
world:emit("test")
|
|
|
@ -2,9 +2,13 @@
|
||||||
|
|
||||||
local PATH = (...):gsub('%.[^%.]+$', '')
|
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||||
|
|
||||||
local Pool = require(PATH..".pool")
|
local Pool = require(PATH..".pool")
|
||||||
|
local Utils = require(PATH..".utils")
|
||||||
|
|
||||||
|
local System = {
|
||||||
|
ENABLE_OPTIMIZATION = true,
|
||||||
|
}
|
||||||
|
|
||||||
local System = {}
|
|
||||||
System.mt = {
|
System.mt = {
|
||||||
__index = System,
|
__index = System,
|
||||||
__call = function(baseSystem, world)
|
__call = function(baseSystem, world)
|
||||||
|
@ -18,6 +22,13 @@ System.mt = {
|
||||||
__isBaseSystem = false, -- Overwrite value from baseSystem
|
__isBaseSystem = false, -- Overwrite value from baseSystem
|
||||||
}, 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
|
for _, filter in pairs(baseSystem.__filter) do
|
||||||
local pool = system:__buildPool(filter)
|
local pool = system:__buildPool(filter)
|
||||||
if not system[pool.name] then
|
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
|
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
local PATH = (...):gsub('%.[^%.]+$', '')
|
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||||
|
|
||||||
local Type = require(PATH..".type")
|
local Type = require(PATH..".type")
|
||||||
local List = require(PATH..".list")
|
local List = require(PATH..".list")
|
||||||
|
local Utils = require(PATH..".utils")
|
||||||
|
|
||||||
local World = {}
|
local World = {
|
||||||
|
ENABLE_OPTIMIZATION = true,
|
||||||
|
}
|
||||||
World.__index = World
|
World.__index = World
|
||||||
|
|
||||||
--- Creates a new World.
|
--- Creates a new World.
|
||||||
|
@ -26,6 +29,13 @@ function World.new()
|
||||||
__isWorld = true,
|
__isWorld = true,
|
||||||
}, World)
|
}, 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
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue