diff --git a/src/core/GameRunner.ts b/src/core/GameRunner.ts index 2179d2df5..3e3affd39 100644 --- a/src/core/GameRunner.ts +++ b/src/core/GameRunner.ts @@ -173,12 +173,16 @@ export class GameRunner { } // Many tiles are updated to pack it into an array - const packedTileUpdates = updates[GameUpdateType.Tile].map((u) => u.update); + const tileUpdates = updates[GameUpdateType.Tile]; + const packedTileUpdates = new BigUint64Array(tileUpdates.length); + for (let i = 0; i < tileUpdates.length; i++) { + packedTileUpdates[i] = tileUpdates[i].update; + } updates[GameUpdateType.Tile] = []; this.callBack({ tick: this.game.ticks(), - packedTileUpdates: new BigUint64Array(packedTileUpdates), + packedTileUpdates, updates: updates, playerNameViewData: this.playerViewData, tickExecutionDuration: tickExecutionDuration, diff --git a/src/core/worker/Worker.worker.ts b/src/core/worker/Worker.worker.ts index aa61da3de..55c37dda1 100644 --- a/src/core/worker/Worker.worker.ts +++ b/src/core/worker/Worker.worker.ts @@ -30,6 +30,12 @@ function gameUpdate(gu: GameUpdateViewData | ErrorUpdate) { } function sendMessage(message: WorkerMessage) { + if (message.type === "game_update") { + // Transfer the packed tile updates buffer to avoid structured-clone copies and + // reduce worker-side memory churn during long runs / catch-up. + ctx.postMessage(message, [message.gameUpdate.packedTileUpdates.buffer]); + return; + } ctx.postMessage(message); }