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:
@@ -40,6 +40,8 @@ func _init(match_seed: int, human_players: int, bot_count: int, rules: MatchRule
|
||||
if rules == null:
|
||||
rules = immediate_rules(human_players, bot_count)
|
||||
match_flow = Match.new(match_seed, rules)
|
||||
# Each later island's bot navigation is made on the worker that makes the island.
|
||||
match_flow.prepare_next = _prepare_bots
|
||||
nav = NavGrid.build(match_flow.sim)
|
||||
world = BotWorld.build(match_flow.sim, nav)
|
||||
for handle: int in range(1, human_players + 1):
|
||||
@@ -115,8 +117,19 @@ func _advance() -> void:
|
||||
bots.clear()
|
||||
scripted.clear()
|
||||
if event["from"] == "results":
|
||||
nav = NavGrid.build(match_flow.sim)
|
||||
world = BotWorld.build(match_flow.sim, nav)
|
||||
var ready: Dictionary = match_flow.prepared
|
||||
if ready.has("nav"):
|
||||
nav = ready["nav"]
|
||||
world = ready["world"]
|
||||
else:
|
||||
nav = NavGrid.build(match_flow.sim)
|
||||
world = BotWorld.build(match_flow.sim, nav)
|
||||
|
||||
|
||||
## Runs on Match's worker thread with the next match's fresh simulation.
|
||||
static func _prepare_bots(next_sim: Simulation) -> Dictionary:
|
||||
var grid: NavGrid = NavGrid.build(next_sim)
|
||||
return {"nav": grid, "world": BotWorld.build(next_sim, grid)}
|
||||
|
||||
|
||||
func _track_players() -> void:
|
||||
|
||||
+60
-1
@@ -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:
|
||||
|
||||
+12
-1
@@ -56,6 +56,8 @@ var hit_history: HitHistory = HitHistory.new()
|
||||
|
||||
## Player entity ID -> { command tick -> PlayerCommand }
|
||||
var _pending: Dictionary = {}
|
||||
## The tide levels plan_tide() worked out, or empty.
|
||||
var _planned_levels: PackedFloat32Array = PackedFloat32Array()
|
||||
## How many entries of props.destroyed have been checked for crate loot.
|
||||
var _destroyed_checked: int = 0
|
||||
## Player entity IDs, ascending.
|
||||
@@ -154,11 +156,20 @@ static func crate_spill_points(prop_set: PropSet) -> Dictionary:
|
||||
## Starts the tide's schedule from this tick.
|
||||
func start_tide() -> void:
|
||||
if island != null:
|
||||
tide = Tide.new(tick, SafeGround.new(ground).plan_levels())
|
||||
plan_tide()
|
||||
tide = Tide.new(tick, _planned_levels)
|
||||
else:
|
||||
tide = Tide.new(tick)
|
||||
|
||||
|
||||
## Works out the island's tide levels now, if it hasn't been done, so start_tide() is
|
||||
## quick. They depend only on the island, so the server does this on the worker that
|
||||
## builds the next match's island; anything that changes the ground first must not.
|
||||
func plan_tide() -> void:
|
||||
if island != null and _planned_levels.is_empty():
|
||||
_planned_levels = SafeGround.new(ground).plan_levels()
|
||||
|
||||
|
||||
## Height of the water surface over a point: the sea level where the flood reaches,
|
||||
## Flood.NO_WATER where it doesn't. On a test ground, the sea level everywhere.
|
||||
func water_level_at(point: Vector3) -> float:
|
||||
|
||||
Reference in New Issue
Block a user