Skip to content

Commit 4902bc3

Browse files
perf(mobile): run several highlight batches per frame
Yielding after every 2,000-character batch kept each JS task short but waited for a frame after each one, which about doubled the time until a large file got its colors. Batches now run back to back until about 16ms of work has passed, then yield once. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d10e1c7 commit 4902bc3

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎apps/mobile/src/features/review/shikiReviewHighlighter.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ const REVIEW_HIGHLIGHTER_ENGINE_PREFERENCE = resolveReviewHighlighterEnginePrefe
5050
REVIEW_HIGHLIGHTER_ENGINE_ENV_VALUE,
5151
);
5252
const REVIEW_HIGHLIGHT_CHUNK_LINE_THRESHOLD = 8;
53-
// Bounds each tokenizing task by characters, not lines, so long-line files
53+
// Bounds each tokenizing call by characters, not lines, so long-line files
5454
// still yield to touches and renders between short batches.
5555
const REVIEW_HIGHLIGHT_CHUNK_CHARACTERS = 2_000;
56+
// A yield waits for the next frame, so batches run back to back until about one
57+
// frame of work has passed instead of yielding after every batch.
58+
const REVIEW_HIGHLIGHT_YIELD_AFTER_MS = 16;
5659
const REVIEW_TOKENIZE_MAX_LINE_LENGTH = 1_000;
5760
const REVIEW_INITIAL_LANGUAGE_MODULES = [
5861
bashLanguage,
@@ -519,6 +522,7 @@ async function highlightLines(
519522
// template string that spans a batch boundary keeps its colors.
520523
let grammarState: GrammarState | undefined;
521524
let start = 0;
525+
let sliceStartedAt = performance.now();
522526

523527
while (start < sourceLines.length) {
524528
// A skipped line leaves its ending state unknown; resume from a fresh state.
@@ -551,8 +555,13 @@ async function highlightLines(
551555
start = end;
552556
}
553557

554-
if (sourceLines.length > REVIEW_HIGHLIGHT_CHUNK_LINE_THRESHOLD && start < sourceLines.length) {
558+
if (
559+
sourceLines.length > REVIEW_HIGHLIGHT_CHUNK_LINE_THRESHOLD &&
560+
start < sourceLines.length &&
561+
performance.now() - sliceStartedAt >= REVIEW_HIGHLIGHT_YIELD_AFTER_MS
562+
) {
555563
await waitForNextFrame();
564+
sliceStartedAt = performance.now();
556565
}
557566
}
558567

0 commit comments

Comments
 (0)