H12: Build the next match's island on a worker thread

When a match's results begin, Match starts the next match's Simulation on a
WorkerThreadPool task (its seed is fixed by then) and collects it when the
lobby opens. The same task plans the island's tide levels
(Simulation.plan_tide) and runs a prepare_next hook, which ServerLoop uses
to build the bots' NavGrid and BotWorld for the island. All of these read
only the new simulation, so the results are the ones the main thread would
have made: tests compare the state hash, terrain, solids, props, tide
levels and navigation with fresh ones, and the ten-matches test still
matches its references. A Match freed while its task runs waits for it.

Measured: a 24-bot server paced in real time through the results, lobby,
countdown and 3 s of the next drop has a worst tick of 5.4 ms; it was 1.8 s.
In the 24-client soak at 2% loss, moving the island alone left a 0.82 s tick
(the bots' navigation) and moving that too left 74 ms at the next match's
start (planning the tide), which this also moves. The soak hasn't been run
again since that last step.

Closes #79

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 01:27:39 -05:00
co-authored by Claude Opus 5.5
parent 3a76371760
commit ad0fd2695d
5 changed files with 158 additions and 4 deletions
+60 -1
View File
@@ -74,6 +74,18 @@ var _match_size: int = 0
var _roster: Array[int] = []
## Handle -> item code of the weapon that last hit them, this match.
var _last_weapon: Dictionary = {}
## The next match's simulation, built on a WorkerThreadPool task while the results
## show, so the server's tick never stalls for the island (H12). The task writes
## its result into _next_box[0]; nothing reads it until the task is waited on.
var _next_task: int = -1
var _next_box: Array = []
## Called on the worker with the next match's new Simulation, after it is built:
## func(sim: Simulation) -> Dictionary. The server uses it to prepare what its bots
## need for the island at the same time. Must only read the simulation. Its result is
## `prepared` once the lobby opens: empty if there is no hook, or if the simulation was
## built on the main thread instead.
var prepare_next: Callable = Callable()
var prepared: Dictionary = {}
func _init(seed_value: int, match_rules: MatchRules = null) -> void:
@@ -82,6 +94,13 @@ func _init(seed_value: int, match_rules: MatchRules = null) -> void:
sim = Simulation.new(match_seed(0))
func _notification(what: int) -> void:
# A task still running must be waited on before its pool slot is forgotten.
if what == NOTIFICATION_PREDELETE and _next_task >= 0:
WorkerThreadPool.wait_for_task_completion(_next_task)
_next_task = -1
## The seed of match `index`: the session seed for the first, then a hash of it and the
## index, kept non-negative.
func match_seed(index: int) -> int:
@@ -354,6 +373,7 @@ func _check_finished() -> void:
})
spectating.erase(handle)
_enter(RESULTS)
_prepare_next()
events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate(), "timed_out": timed_out})
@@ -398,7 +418,7 @@ func _reset() -> void:
winners.clear()
_next_bot_handle = FIRST_BOT_HANDLE
_match_size = 0
sim = Simulation.new(match_seed(matches_started))
sim = _take_next()
_enter(LOBBY)
# People who watched the last match take places first; bots only ever fill
# what is left when the next one starts.
@@ -409,6 +429,45 @@ func _reset() -> void:
watchers.append(handle)
## Starts building the next match's simulation on a worker thread. Its seed is
## fixed by now, and generation reads nothing but the seed, so the result is the
## same as building it on the main thread.
func _prepare_next() -> void:
if _next_task >= 0:
return
# Build the shared item table here, so the worker never races to fill it.
ItemCatalog.all()
var box: Array = [null, {}]
_next_box = box
_next_task = WorkerThreadPool.add_task(_build_into.bind(box, match_seed(matches_started), prepare_next), false, "Tidewall next island")
static func _build_into(box: Array, seed_value: int, hook: Callable) -> void:
var built: Simulation = Simulation.new(seed_value)
built.plan_tide()
box[0] = built
if hook.is_valid():
box[1] = hook.call(built)
## The next match's simulation: the worker's, once it has finished (normally long
## before the results end), or one built here if none was started.
func _take_next() -> Simulation:
var seed_value: int = match_seed(matches_started)
prepared = {}
if _next_task < 0:
return Simulation.new(seed_value)
WorkerThreadPool.wait_for_task_completion(_next_task)
_next_task = -1
var built: Simulation = _next_box[0] as Simulation
var extras: Dictionary = _next_box[1]
_next_box = []
if built == null or built.rng.match_seed != seed_value:
return Simulation.new(seed_value)
prepared = extras
return built
## The living handle `handle` is watching, or 0 if they aren't spectating or nobody is
## left to watch.
func spectate_target(handle: int) -> int: