From a04abbec532d50dde810cf436838ee23b02e9637 Mon Sep 17 00:00:00 2001 From: scamiv <6170744+scamiv@users.noreply.github.com> Date: Mon, 24 Nov 2025 19:59:10 +0100 Subject: [PATCH] Refactor slice budget calculation in ClientGameRunner to improve backlog handling. Introduced dynamic slice budget scaling based on backlog size, allowing for longer processing times when necessary while maintaining UI responsiveness. --- src/client/ClientGameRunner.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/client/ClientGameRunner.ts b/src/client/ClientGameRunner.ts index 97a85fd2b..c7ad0628b 100644 --- a/src/client/ClientGameRunner.ts +++ b/src/client/ClientGameRunner.ts @@ -499,8 +499,24 @@ export class ClientGameRunner { this.isProcessingUpdates = true; const processSlice = () => { - const SLICE_BUDGET_MS = 8; // keep UI responsive while catching up + const BASE_SLICE_BUDGET_MS = 8; // keep UI responsive while catching up + const MAX_SLICE_BUDGET_MS = 1000; // allow longer slices when backlog is large + const BACKLOG_FREE_TURNS = 10; // scaling starts at this many turns + const BACKLOG_MAX_TURNS = 1000; // MAX_SLICE_BUDGET_MS is reached at this many turns const MAX_PER_SLICE = 100; + + const backlogOverhead = Math.max( + 0, + this.backlogTurns - BACKLOG_FREE_TURNS, + ); + const backlogScale = Math.min( + 1, + backlogOverhead / (BACKLOG_MAX_TURNS - BACKLOG_FREE_TURNS), + ); + const sliceBudgetMs = + BASE_SLICE_BUDGET_MS + + backlogScale * (MAX_SLICE_BUDGET_MS - BASE_SLICE_BUDGET_MS); + const sliceStart = performance.now(); const batch: GameUpdateViewData[] = []; @@ -529,7 +545,7 @@ export class ClientGameRunner { lastTick = gu.tick; const elapsed = performance.now() - sliceStart; - if (processedCount >= MAX_PER_SLICE || elapsed >= SLICE_BUDGET_MS) { + if (processedCount >= MAX_PER_SLICE || elapsed >= sliceBudgetMs) { break; } }