mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-05 11:52:05 +00:00
bca980f572
Stacked on #4243 (the `perf:client` harness) — first step of fixing the every-100ms main-thread stutter: make the per-tick burst small before spreading what remains across frames. ## Problem The harness showed the main-thread burst was dominated by `structuredClone` of the `updates` object, and the clone was dominated by two kinds of per-tick churn that re-sent object payloads every tick: - `gold` / `troops` / `tilesOwned` change for nearly every alive player every tick → ~278 partial `PlayerUpdate` objects per tick (world/400 bots), ~508 on giantworldmap. - Attack troop counts tick down every tick → whole `outgoingAttacks`/`incomingAttacks` arrays re-cloned for every fighting player every tick. - `playerNameViewData` (an all-players record) was cloned every tick but only recomputed every 30 ticks. ## Change Three additions to the worker → main protocol (all transferable, zero-clone): 1. **`packedPlayerUpdates`** — `[smallID, tilesOwned, gold, troops]` float64 quads for players whose stats changed. These fields no longer appear in `PlayerUpdate` diffs (first emissions still carry the full snapshot). Gold is exact in a float64 (game values ≪ 2^53). 2. **`packedAttackUpdates`** — `[ownerSmallID, direction, index, troops]` quads. Attack arrays are only resent when membership/order/retreating changes — which is exactly the condition that keeps the patch indexes valid (a tick either resends an array or patches it, never both). 3. **`playerNameViewData` is now optional** — attached only on placement-rebuild ticks (spawn ticks, first ticks, every 30th, spawn end). The client keeps the last applied values; dead players' name placements freeze at death (matching the previous effective behavior). On the client, `GameView.populateFrame` now also rebuilds `names` / `relationMatrix` / `allianceClusters` only when their inputs changed that tick — field presence on a partial `PlayerUpdate` marks them dirty. (`playerStatus`, nuke telegraphs, and attack rings still recompute every tick; they're tick- or unit-dependent.) ## Results (perf:client, this machine; low-end devices ~5–20× slower) Default run (world, 400 bots, 1800 ticks): | stage | before | after | |---|---|---| | clone (serialize+deserialize) | 1.02ms | **0.09ms** | | GameView.update | 0.62ms | **0.29ms** | | WebGLFrameBuilder.update | 0.04ms | 0.04ms | | **TOTAL burst mean** | **1.67ms** | **0.42ms** | | TOTAL p99 / max | 3.47 / 10.3ms | **1.21 / 3.92ms** | giantworldmap/600t: 2.54 → 0.68ms mean. Player update objects: 278 → 6.5 per tick (world), 508 → 12 (giant). The remaining burst is mostly tile apply + per-tick derivations — the part that frame-spreading (next step) addresses. ## Verification - **Sim final hash unchanged** on all three reference configs (`5607618202213430`, `29309648281599524`, `39945089450032050`) — no simulation behavior change. - **View hash unchanged** on all three configs (`942106e9`, `a3aae227`, `cbaaf265`) — the rendered view state is provably identical tick-for-tick, including the name-freeze semantics. - New tests: `tests/PackedPlayerUpdates.test.ts` (drain + GameRunner cadence), packed-channel and freeze-at-death cases in `tests/client/view/GameView.test.ts`, `packAttackTroopDeltas` unit tests and updated diff contract in `tests/GameUpdateUtils.test.ts` / `tests/PlayerUpdateDiff.test.ts`. - `npm test` (1490 tests), `eslint`, `prettier`, `tsc --noEmit` all pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
308 lines
9.3 KiB
TypeScript
308 lines
9.3 KiB
TypeScript
import { placeName, placeSpawnName } from "../client/hud/NameBoxCalculator";
|
|
import { Config } from "./configuration/Config";
|
|
import { Executor } from "./execution/ExecutionManager";
|
|
import { RecomputeRailClusterExecution } from "./execution/RecomputeRailClusterExecution";
|
|
import { SpawnTimerExecution } from "./execution/SpawnTimerExecution";
|
|
import { WinCheckExecution } from "./execution/WinCheckExecution";
|
|
import {
|
|
AllPlayers,
|
|
BuildableUnit,
|
|
Game,
|
|
GameType,
|
|
GameUpdates,
|
|
NameViewData,
|
|
Player,
|
|
PlayerActions,
|
|
PlayerBorderTiles,
|
|
PlayerBuildableUnitType,
|
|
PlayerID,
|
|
PlayerInfo,
|
|
PlayerProfile,
|
|
PlayerType,
|
|
UnitType,
|
|
} from "./game/Game";
|
|
import { createGame } from "./game/GameImpl";
|
|
import { TileRef } from "./game/GameMap";
|
|
import { GameMapLoader } from "./game/GameMapLoader";
|
|
import { ErrorUpdate, GameUpdateViewData } from "./game/GameUpdates";
|
|
import { createNationsForGame } from "./game/NationCreation";
|
|
import { loadTerrainMap as loadGameMap } from "./game/TerrainMapLoader";
|
|
import { PseudoRandom } from "./PseudoRandom";
|
|
import { ClientID, GameStartInfo, Turn } from "./Schemas";
|
|
import { simpleHash } from "./Util";
|
|
|
|
export async function createGameRunner(
|
|
gameStart: GameStartInfo,
|
|
clientID: ClientID | undefined,
|
|
mapLoader: GameMapLoader,
|
|
callBack: (gu: GameUpdateViewData | ErrorUpdate) => void,
|
|
): Promise<GameRunner> {
|
|
const config = new Config(gameStart.config, null, false);
|
|
const gameMap = await loadGameMap(
|
|
gameStart.config.gameMap,
|
|
gameStart.config.gameMapSize,
|
|
mapLoader,
|
|
);
|
|
const random = new PseudoRandom(simpleHash(gameStart.gameID));
|
|
|
|
const humans = gameStart.players.map((p) => {
|
|
return new PlayerInfo(
|
|
p.username,
|
|
PlayerType.Human,
|
|
p.clientID,
|
|
random.nextID(),
|
|
p.isLobbyCreator ?? false,
|
|
p.clanTag,
|
|
p.friends ?? [],
|
|
);
|
|
});
|
|
|
|
const nations = createNationsForGame(
|
|
gameStart,
|
|
gameMap.nations,
|
|
gameMap.additionalNations,
|
|
humans.length,
|
|
random,
|
|
);
|
|
|
|
const game: Game = createGame(
|
|
humans,
|
|
nations,
|
|
gameMap.gameMap,
|
|
gameMap.miniGameMap,
|
|
config,
|
|
gameMap.teamGameSpawnAreas,
|
|
);
|
|
|
|
const gr = new GameRunner(
|
|
game,
|
|
new Executor(game, gameStart.gameID, clientID),
|
|
callBack,
|
|
);
|
|
gr.init();
|
|
return gr;
|
|
}
|
|
|
|
export class GameRunner {
|
|
private turns: Turn[] = [];
|
|
private currTurn = 0;
|
|
private isExecuting = false;
|
|
|
|
private playerViewData: Record<PlayerID, NameViewData> = {};
|
|
|
|
constructor(
|
|
public game: Game,
|
|
private execManager: Executor,
|
|
private callBack: (gu: GameUpdateViewData | ErrorUpdate) => void,
|
|
) {}
|
|
|
|
init() {
|
|
if (this.game.config().gameConfig().gameType !== GameType.Singleplayer) {
|
|
this.game.addExecution(new SpawnTimerExecution());
|
|
}
|
|
if (this.game.config().spawnNations()) {
|
|
this.game.addExecution(...this.execManager.nationExecutions());
|
|
}
|
|
if (this.game.config().isRandomSpawn()) {
|
|
this.game.addExecution(...this.execManager.spawnPlayers());
|
|
}
|
|
if (this.game.config().bots() > 0) {
|
|
this.game.addExecution(
|
|
...this.execManager.spawnTribes(this.game.config().bots()),
|
|
);
|
|
}
|
|
this.game.addExecution(new WinCheckExecution());
|
|
if (!this.game.config().isUnitDisabled(UnitType.Factory)) {
|
|
this.game.addExecution(
|
|
new RecomputeRailClusterExecution(this.game.railNetwork()),
|
|
);
|
|
}
|
|
}
|
|
|
|
public addTurn(turn: Turn): void {
|
|
this.turns.push(turn);
|
|
}
|
|
|
|
public executeNextTick(pendingTurns?: number): boolean {
|
|
if (this.isExecuting) {
|
|
return false;
|
|
}
|
|
if (this.currTurn >= this.turns.length) {
|
|
return false;
|
|
}
|
|
this.isExecuting = true;
|
|
|
|
this.game.addExecution(
|
|
...this.execManager.createExecs(this.turns[this.currTurn]),
|
|
);
|
|
this.currTurn++;
|
|
|
|
const wasInSpawnPhase = this.game.inSpawnPhase();
|
|
let updates: GameUpdates;
|
|
let tickExecutionDuration: number;
|
|
|
|
try {
|
|
const startTime = performance.now();
|
|
updates = this.game.executeNextTick();
|
|
const endTime = performance.now();
|
|
tickExecutionDuration = endTime - startTime;
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
console.error("Game tick error:", error.message);
|
|
this.callBack({
|
|
errMsg: error.message,
|
|
stack: error.stack,
|
|
} as ErrorUpdate);
|
|
} else {
|
|
console.error("Game tick error:", error);
|
|
}
|
|
this.isExecuting = false;
|
|
return false;
|
|
}
|
|
|
|
// Track whether placements were recomputed this tick — the record is
|
|
// only attached to the update when it could have changed, so the main
|
|
// thread doesn't structured-clone an identical ~all-players record on
|
|
// every other tick.
|
|
let viewDataChanged = false;
|
|
if (this.game.inSpawnPhase()) {
|
|
for (const p of this.game.players()) {
|
|
if (p.type() !== PlayerType.Human && p.type() !== PlayerType.Nation) {
|
|
continue;
|
|
}
|
|
if (p.spawnTile() === undefined) continue;
|
|
this.playerViewData[p.id()] = placeSpawnName(this.game, p);
|
|
viewDataChanged = true;
|
|
}
|
|
}
|
|
|
|
const spawnJustEnded = wasInSpawnPhase && !this.game.inSpawnPhase();
|
|
if (
|
|
spawnJustEnded ||
|
|
this.game.ticks() < 3 ||
|
|
this.game.ticks() % 30 === 0
|
|
) {
|
|
for (const p of this.game.players()) {
|
|
this.playerViewData[p.id()] = placeName(this.game, p);
|
|
}
|
|
viewDataChanged = true;
|
|
}
|
|
|
|
const packedTileUpdates = this.game.drainPackedTileUpdates();
|
|
const packedMotionPlans = this.game.drainPackedMotionPlans();
|
|
const packedPlayerUpdates = this.game.drainPackedPlayerUpdates();
|
|
const packedAttackUpdates = this.game.drainPackedAttackUpdates();
|
|
|
|
this.callBack({
|
|
tick: this.game.ticks(),
|
|
packedTileUpdates,
|
|
...(packedMotionPlans ? { packedMotionPlans } : {}),
|
|
...(packedPlayerUpdates ? { packedPlayerUpdates } : {}),
|
|
...(packedAttackUpdates ? { packedAttackUpdates } : {}),
|
|
updates: updates,
|
|
...(viewDataChanged ? { playerNameViewData: this.playerViewData } : {}),
|
|
tickExecutionDuration: tickExecutionDuration,
|
|
pendingTurns: pendingTurns ?? 0,
|
|
});
|
|
this.isExecuting = false;
|
|
return true;
|
|
}
|
|
|
|
public pendingTurns(): number {
|
|
return Math.max(0, this.turns.length - this.currTurn);
|
|
}
|
|
|
|
public playerBuildables(
|
|
playerID: PlayerID,
|
|
x?: number,
|
|
y?: number,
|
|
units?: readonly PlayerBuildableUnitType[],
|
|
): BuildableUnit[] {
|
|
const player = this.game.player(playerID);
|
|
const tile =
|
|
x !== undefined && y !== undefined ? this.game.ref(x, y) : null;
|
|
return player.buildableUnits(tile, units);
|
|
}
|
|
|
|
public playerActions(
|
|
playerID: PlayerID,
|
|
x?: number,
|
|
y?: number,
|
|
units?: readonly PlayerBuildableUnitType[] | null,
|
|
): PlayerActions {
|
|
const player = this.game.player(playerID);
|
|
const tile =
|
|
x !== undefined && y !== undefined ? this.game.ref(x, y) : null;
|
|
const actions = {
|
|
canAttack: tile !== null && player.canAttack(tile),
|
|
buildableUnits: units === null ? [] : player.buildableUnits(tile, units),
|
|
canSendEmojiAllPlayers: player.canSendEmoji(AllPlayers),
|
|
canEmbargoAll: player.canEmbargoAll(),
|
|
} as PlayerActions;
|
|
|
|
if (tile !== null && this.game.hasOwner(tile)) {
|
|
const other = this.game.owner(tile) as Player;
|
|
actions.interaction = {
|
|
sharedBorder: player.sharesBorderWith(other),
|
|
canSendEmoji: player.canSendEmoji(other),
|
|
canTarget: player.canTarget(other),
|
|
canSendAllianceRequest: player.canSendAllianceRequest(other),
|
|
canBreakAlliance: player.isAlliedWith(other),
|
|
canDonateGold: player.canDonateGold(other),
|
|
canDonateTroops: player.canDonateTroops(other),
|
|
canEmbargo: !player.hasEmbargoAgainst(other),
|
|
allianceInfo: player.allianceInfo(other) ?? undefined,
|
|
};
|
|
}
|
|
|
|
return actions;
|
|
}
|
|
|
|
public playerProfile(playerID: number): PlayerProfile {
|
|
const player = this.game.playerBySmallID(playerID);
|
|
if (!player.isPlayer()) {
|
|
throw new Error(`player with id ${playerID} not found`);
|
|
}
|
|
return player.playerProfile();
|
|
}
|
|
public playerBorderTiles(playerID: PlayerID): PlayerBorderTiles {
|
|
const player = this.game.player(playerID);
|
|
if (!player.isPlayer()) {
|
|
throw new Error(`player with id ${playerID} not found`);
|
|
}
|
|
return {
|
|
borderTiles: player.borderTiles(),
|
|
} as PlayerBorderTiles;
|
|
}
|
|
|
|
public attackClusteredPositions(
|
|
playerID: number,
|
|
attackID?: string,
|
|
): { id: string; positions: { x: number; y: number }[] }[] {
|
|
const player = this.game.playerBySmallID(playerID);
|
|
if (!player.isPlayer())
|
|
throw new Error(`player with id ${playerID} not found`);
|
|
const all = [...player.outgoingAttacks(), ...player.incomingAttacks()];
|
|
const attacks = attackID ? all.filter((a) => a.id() === attackID) : all;
|
|
|
|
return attacks.map((a) => ({
|
|
id: a.id(),
|
|
positions: a.clusteredPositions().map((tile) => ({
|
|
x: this.game.map().x(tile),
|
|
y: this.game.map().y(tile),
|
|
})),
|
|
}));
|
|
}
|
|
|
|
public bestTransportShipSpawn(
|
|
playerID: PlayerID,
|
|
targetTile: TileRef,
|
|
): TileRef | false {
|
|
const player = this.game.player(playerID);
|
|
if (!player.isPlayer()) {
|
|
throw new Error(`player with id ${playerID} not found`);
|
|
}
|
|
return player.bestTransportShipSpawn(targetTile);
|
|
}
|
|
}
|