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.

This commit is contained in:
scamiv
2025-11-24 19:59:10 +01:00
parent bd300708c7
commit a04abbec53
+18 -2
View File
@@ -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;
}
}