Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scheduler not respecting priority #51

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 38 additions & 46 deletions lib/Loop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ local function systemName(system: System)
return debug.info(fn, "s") .. "->" .. debug.info(fn, "n")
end

local function systemPriority(system: System)
if type(system) == "table" then
return system.priority or 0
end

return 0
end

--[=[
@class Loop

Expand Down Expand Up @@ -222,9 +214,44 @@ function Loop:replaceSystem(old: System, new: System)
end

local function orderSystemsByDependencies(unscheduledSystems: { System })
local sortedUnscheduledSystems = table.clone(unscheduledSystems)
local systemPriorityMap = {}
local visiting = "v"

local function systemPriority(system: System)
local priority = systemPriorityMap[system]

if not priority then
priority = 0

systemPriorityMap[system] = visiting

if type(system) == "table" then
if system.after then
for _, dependency in system.after do
if systemPriorityMap[dependency] ~= visiting then
priority = math.max(priority, systemPriority(dependency) + 1)
else
error(
`Unable to get system priority due to cyclic dependency between: \n{systemName(system)} \nAND \n{systemName(
dependency
)}`
)
Ukendio marked this conversation as resolved.
Show resolved Hide resolved
end
end
elseif system.priority then
priority = system.priority
end
end

systemPriorityMap[system] = priority
end

table.sort(sortedUnscheduledSystems, function(a, b)
return priority
end

local scheduledSystems = table.clone(unscheduledSystems)

table.sort(scheduledSystems, function(a, b)
local priorityA = systemPriority(a)
local priorityB = systemPriority(b)

Expand All @@ -242,39 +269,6 @@ local function orderSystemsByDependencies(unscheduledSystems: { System })
return priorityA < priorityB
end)

local scheduledSystemsSet = {}
local scheduledSystems = {}

local visited, explored = 1, 2

local function scheduleSystem(system)
scheduledSystemsSet[system] = visited

if type(system) == "table" and system.after then
for _, dependency in system.after do
if scheduledSystemsSet[dependency] == nil then
scheduleSystem(dependency)
elseif scheduledSystemsSet[dependency] == visited then
error(
`Unable to schedule systems due to cyclic dependency between: \n{systemName(system)} \nAND \n{systemName(
dependency
)}`
)
end
end
end

scheduledSystemsSet[system] = explored

table.insert(scheduledSystems, system)
end

for _, system in sortedUnscheduledSystems do
if scheduledSystemsSet[system] == nil then
scheduleSystem(system)
end
end

return scheduledSystems
end

Expand Down Expand Up @@ -304,9 +298,7 @@ function Loop:_sortSystems()
error(
`Unable to schedule "{systemName(system)}" because the system "{systemName(dependency)}" is not scheduled.\n\nEither schedule "{systemName(
dependency
)}" before "{systemName(
system
)}" or consider scheduling these systems together with Loop:scheduleSystems`
)}" before "{systemName(system)}" or consider scheduling these systems together with Loop:scheduleSystems`
)
end
end
Expand Down
42 changes: 42 additions & 0 deletions lib/Loop.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ return function()
connection.default:Disconnect()
end)

it("should call systems in order with dependent system after priority system", function()
local loop = Loop.new()

local order = {}
local systemB = {
system = function()
table.insert(order, "b")
end,
}
local systemC = {
system = function()
table.insert(order, "c")
end,
priority = 1000,
}
local systemA = {
system = function()
table.insert(order, "a")
end,
after = { systemC },
}

loop:scheduleSystems({
systemB,
systemC,
systemA,
})

local connection = loop:begin({ default = bindable.Event })

expect(#order).to.equal(0)

bindable:Fire()

expect(#order).to.equal(3)
expect(order[1]).to.equal("b")
expect(order[2]).to.equal("c")
expect(order[3]).to.equal("a")

connection.default:Disconnect()
end)

it("should not schedule systems more than once", function()
local loop = Loop.new()

Expand Down
Loading