diff --git a/main.lua b/main.lua index 1773142..ff2ec47 100644 --- a/main.lua +++ b/main.lua @@ -9,77 +9,48 @@ 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 Component = Concord.component +local System = Concord.system +local Entity = Concord.entity +local World = Concord.world 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() -local entity = Entity(world) -entity:give(test_comp_1, 100, 100) +world:addSystems(test_system_1, test_system_2, test_system_3) -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") -world:emit("test") +local start = love.timer.getTime() +for _ = 1, 1000000 do + world:emit("test") +end +local stop = love.timer.getTime() -print("Iteration: 3") -world:emit("test") \ No newline at end of file +print("Time taken: " .. stop - start) \ No newline at end of file diff --git a/src/system.lua b/src/system.lua index 28208d2..4839f70 100644 --- a/src/system.lua +++ b/src/system.lua @@ -2,9 +2,13 @@ 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 = { __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 diff --git a/src/utils.lua b/src/utils.lua new file mode 100644 index 0000000..43adf6c --- /dev/null +++ b/src/utils.lua @@ -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 \ No newline at end of file diff --git a/src/world.lua b/src/world.lua index 9ce0215..bd79ebd 100644 --- a/src/world.lua +++ b/src/world.lua @@ -2,10 +2,13 @@ local PATH = (...):gsub('%.[^%.]+$', '') -local Type = require(PATH..".type") -local List = require(PATH..".list") +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