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

Add the ability to buffer commands #88

Merged
merged 39 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e85f5d8
add spawn, despawn, and insert commands
memorycode Jul 16, 2024
a3dfe88
start remove command
memorycode Jul 16, 2024
7ecc772
add remove command
memorycode Jul 16, 2024
1945388
get all world tests passing
memorycode Jul 16, 2024
7986fd8
remove idea of multiple storages
memorycode Jul 16, 2024
db171d5
remove the idea of oldStorage from transitionArchetype
memorycode Jul 16, 2024
eb72ef6
Merge branch 'main' into buffer-commands
memorycode Jul 16, 2024
6206863
rename defer methods
memorycode Jul 16, 2024
18c9473
fix world tests to account for command queue
memorycode Jul 16, 2024
76a2a7c
make loop tests pass
memorycode Jul 16, 2024
0b5bec4
remove outdated loop test
memorycode Jul 16, 2024
67f62c9
fix ci
memorycode Jul 16, 2024
1e1d74e
add docs
memorycode Jul 16, 2024
5d4f9ae
handle error in commit
memorycode Jul 17, 2024
9cc4523
add replace command
memorycode Jul 18, 2024
134aa83
pass arguments to systems
memorycode Jul 18, 2024
8dc8803
remove old comments
memorycode Jul 18, 2024
8bb9749
polish
memorycode Jul 18, 2024
0bdf946
remove comments
memorycode Jul 18, 2024
6edeee2
clear commands when we clear the world
memorycode Jul 18, 2024
9c2d830
allow multiple worlds
memorycode Jul 21, 2024
020cd76
address review comments
memorycode Jul 22, 2024
24b4a61
validate arguments closer to source
memorycode Jul 22, 2024
1fdcb82
add more tests
memorycode Jul 23, 2024
500d3cd
track entities that are marked for deletion
memorycode Jul 23, 2024
00faa15
update changelog
memorycode Jul 23, 2024
f39172a
remove prints
memorycode Jul 23, 2024
a267037
remove commented out code
memorycode Jul 23, 2024
6b1d1b7
apply reviewed changes
memorycode Jul 23, 2024
2fceafe
apply feedback from review
memorycode Jul 27, 2024
e822b4b
add test for correct size while deferring
memorycode Jul 27, 2024
a5a942f
remove entity from old archetype
memorycode Jul 27, 2024
e47dc79
update debug labels
memorycode Jul 30, 2024
848883c
Merge branch 'main' into buffer-commands
Ukendio Aug 3, 2024
4d4c40c
fix error formatting
memorycode Aug 7, 2024
d175779
Merge branch 'main' into buffer-commands
Ukendio Aug 9, 2024
8f4a979
Merge branch 'main' into buffer-commands
memorycode Aug 9, 2024
08e631f
Merge branch 'main' into buffer-commands
memorycode Aug 16, 2024
cb68c93
Merge branch 'main' into buffer-commands
memorycode Aug 24, 2024
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ The format is based on [Keep a Changelog][kac], and this project adheres to

## [Unreleased]

### Added

- Implemented a deferred command mode for the registry.
- The Loop turns deferring on for all worlds given to it.
- The command buffer is flushed between systems.
- Iterator invalidation is now only prevented in deferred mode.

### Changed
memorycode marked this conversation as resolved.
Show resolved Hide resolved

- Deprecated the return type of `World:remove()`.
- Deprecated `World:optimizeQueries()` because it no longer does anything.

## [0.8.3] - 2024-07-02

### Fixed
Expand Down
48 changes: 36 additions & 12 deletions lib/Loop.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local RunService = game:GetService("RunService")

local World = require(script.Parent.World)
local rollingAverage = require(script.Parent.rollingAverage)
local topoRuntime = require(script.Parent.topoRuntime)

Expand Down Expand Up @@ -364,16 +366,25 @@ function Loop:begin(events)

generation = not generation

local dirtyWorlds: { [any]: true } = {}
local profiling = self.profiling

local worlds: { World.World } = {}
for _, stateArgument in self._state do
if typeof(stateArgument) == "table" and getmetatable(stateArgument) == World then
table.insert(worlds, stateArgument)
end
end

for _, world in worlds do
world:startDeferring()
end

for _, system in ipairs(self._orderedSystemsByEvent[eventName]) do
topoRuntime.start({
system = self._systemState[system],
frame = {
generation = generation,
deltaTime = deltaTime,
dirtyWorlds = dirtyWorlds,
logs = self._systemLogs[system],
},
currentSystem = system,
Expand All @@ -382,13 +393,26 @@ function Loop:begin(events)
if profiling then
profiling[system] = nil
end

return
end

local fn = systemFn(system)
debug.profilebegin("system: " .. systemName(system))

local thread = coroutine.create(fn)
local name = systemName(system)

debug.profilebegin("system: " .. name)
local commitFailed = false
local thread = coroutine.create(function(...)
memorycode marked this conversation as resolved.
Show resolved Hide resolved
fn(...)

for _, world in worlds do
local ok, err = pcall(world.commitCommands, world)
if not ok then
commitFailed = true
error(err)
end
memorycode marked this conversation as resolved.
Show resolved Hide resolved
end
end)

local startTime = os.clock()
local success, errorValue = coroutine.resume(thread, unpack(self._state, 1, self._stateLength))
Expand All @@ -415,20 +439,20 @@ function Loop:begin(events)
)
end

for world in dirtyWorlds do
world:optimizeQueries()
end
table.clear(dirtyWorlds)

if not success then
if os.clock() - recentErrorLastTime > 10 then
recentErrorLastTime = os.clock()
recentErrors = {}
end

local errorString = systemName(system)
local errorString = name
.. ": "
.. tostring(errorValue)
.. (
if commitFailed
-- Strip irrelevant line numbers that point to Loop / World
then string.gsub(errorValue, "%[.+%]:%d+: ", "Failed to apply commands: ")
else errorValue
)
.. "\n"
.. debug.traceback(thread)

Expand Down
24 changes: 0 additions & 24 deletions lib/Loop.spec.luau
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
local Loop = require(script.Parent.Loop)
local useHookState = require(script.Parent.topoRuntime).useHookState
local World = require(script.Parent.World)
local component = require(script.Parent).component
local BindableEvent = require(script.Parent.mock.BindableEvent)

local bindable = BindableEvent.new()
Expand Down Expand Up @@ -609,27 +607,5 @@ return function()
expect(called[2]).to.equal(2)
expect(called[3]).to.equal(3)
end)

it("should optimize queries of worlds used inside it", function()
local world = World.new()
local loop = Loop.new(world)

local A = component()

world:spawn(A())

loop:scheduleSystem(function(world)
world:query(A)
end)

local bindable = BindableEvent.new()
loop:begin({
default = bindable.Event,
})

bindable:Fire()

expect(#world._storages).to.equal(1)
end)
end)
end
Loading
Loading