@devries-cameron20 / t3code-1 / commits / 30084f0018
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): the pull request code tab derives its file model once per diff load (#27833)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 2, 2026 at 14:02 UTC 30084f0018c5751a07291222e8d04e16d513700a
1 file changed +79 -37
apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+79 -37
@@ -176,6 +176,76 @@ function parseSlice(slice: DiffSlice, cacheKey: string, ignoreWhitespace: boolea
176 return parsed;
177 }
178
179 +type DiffModel = {
180 + readonly parsedSlices: ReadonlyArray<RenderablePatch | null>;
181 + readonly loadedFiles: ReadonlyArray<Extract<RenderablePatch, { kind: "files" }>["files"][number]>;
182 + readonly sourceFileStats: ReadonlyMap<string, ReturnType<typeof getDiffLineStat>>;
183 +};
184 +const diffModels = new WeakMap<ReadonlyArray<DiffSlice>, Map<string, DiffModel>>();
185 +const patchHashes = new WeakMap<DiffSlice, number>();
186 +
187 +// Hashing, ordering and counting every file of a huge pull request costs a second, and the tab
188 +// renders far more often than its slices change, so the model is kept per slice list.
189 +function diffModel(
190 + slices: ReadonlyArray<DiffSlice>,
191 + keyPrefix: string,
192 + ignoreWhitespace: boolean,
193 +): DiffModel {
194 + let models = diffModels.get(slices);
195 + if (models === undefined) diffModels.set(slices, (models = new Map()));
196 + const cached = models.get(keyPrefix);
197 + if (cached !== undefined) return cached;
198 + const parsedSlices = slices.map((slice) => {
199 + let hash = patchHashes.get(slice);
200 + if (hash === undefined) patchHashes.set(slice, (hash = fnv1a32(slice.patch)));
201 + // The cache key carries the theme, which the tokenizer caches against, and the patch's
202 + // hash: a refreshed page reuses its cursor, so position alone would return a stale parse.
203 + return parseSlice(slice, `${keyPrefix}:${slice.cursor ?? "first"}:${hash}`, ignoreWhitespace);
204 + });
205 + const model: DiffModel = {
206 + parsedSlices,
207 + loadedFiles: orderDiffFilesLikeTree(
208 + parsedSlices.flatMap((parsed) =>
209 + parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [],
210 + ),
211 + ),
212 + sourceFileStats: new Map(
213 + parsedSlices.flatMap((parsed) =>
214 + parsed?.kind === "files"
215 + ? parsed.files.map(
216 + (file, index) =>
217 + [
218 + buildFileDiffRenderKey(file),
219 + getDiffLineStat([parsed.sourceFiles[index] ?? file]),
220 + ] as const,
221 + )
222 + : [],
223 + ),
224 + ),
225 + };
226 + models.set(keyPrefix, model);
227 + return model;
228 +}
229 +
230 +const threadsByPathCache = new WeakMap<
231 + ReadonlyArray<PullRequestReviewThread>,
232 + ReadonlyMap<string, ReadonlyArray<PullRequestReviewThread>>
233 +>();
234 +
235 +function reviewThreadsByPath(threads: ReadonlyArray<PullRequestReviewThread>) {
236 + let byPath = threadsByPathCache.get(threads);
237 + if (byPath === undefined) {
238 + const grouped = new Map<string, PullRequestReviewThread[]>();
239 + for (const thread of threads) {
240 + const list = grouped.get(thread.path);
241 + if (list) list.push(thread);
242 + else grouped.set(thread.path, [thread]);
243 + }
244 + threadsByPathCache.set(threads, (byPath = grouped));
245 + }
246 + return byPath;
247 +}
248 +
249 interface FileViewedToggleState {
250 readonly enabled: boolean;
251 readonly isViewed: (path: string) => boolean;
@@ -491,20 +561,10 @@ function PullRequestCodeTab({
561 // Agent context is local, so it does not require host comment permission.
562 const canSelectLines =
563 commit === null && (review.inlineComment || onAddToAgentSelection !== undefined);
494 - // The cache key carries the theme, which is what the tokenizer caches against, so a theme
495 - // change is still a fresh parse. The patch's own hash is part of it too: a refreshed page
496 - // reuses its cursor, and a key of position alone would hand back the parse it replaced.
497 - const parsedSlices = loadedSlices.map((slice) =>
498 - parseSlice(
499 - slice,
500 - `pull-request:${scopeKey}:${resolvedTheme}:${ignoreWhitespace}:${slice.cursor ?? "first"}:${fnv1a32(slice.patch)}`,
501 - ignoreWhitespace,
502 - ),
503 - );
504 - const loadedFiles = orderDiffFilesLikeTree(
505 - parsedSlices.flatMap((parsed) =>
506 - parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [],
507 - ),
564 + const { parsedSlices, loadedFiles, sourceFileStats } = diffModel(
565 + loadedSlices,
566 + `pull-request:${scopeKey}:${resolvedTheme}:${ignoreWhitespace}`,
567 + ignoreWhitespace,
568 );
569 const testFileCount = loadedFiles.filter((file) =>
570 isTestChangedFile(resolveFileDiffPath(file)),
@@ -523,33 +583,16 @@ function PullRequestCodeTab({
583 slice.omittedFileStats.map((file) => [file.path, file] as const),
584 ),
585 );
526 - const sourceFileStats = new Map(
527 - parsedSlices.flatMap((parsed) =>
528 - parsed?.kind === "files"
529 - ? parsed.files.map(
530 - (file, index) =>
531 - [
532 - buildFileDiffRenderKey(file),
533 - getDiffLineStat([parsed.sourceFiles[index] ?? file]),
534 - ] as const,
535 - )
536 - : [],
537 - ),
538 - );
586 const fileTreeEntries = diffFileTreeEntries(files);
587 + const threadsByPath = reviewThreadsByPath(detail.reviewThreads);
588 // File membership does not guarantee a rendered hunk for the thread.
589 const placedThreadIds = (() => {
590 const placed = new Set<string>();
591 // Commit diffs use different line coordinates; list conversations separately.
592 if (commit !== null) return placed;
593 for (const file of files) {
546 - const path = resolveFileDiffPath(file);
547 - for (const thread of detail.reviewThreads) {
548 - if (
549 - thread.path === path &&
550 - thread.line !== null &&
551 - isLineInFileDiff(file, thread.side, thread.line)
552 - ) {
594 + for (const thread of threadsByPath.get(resolveFileDiffPath(file)) ?? []) {
595 + if (thread.line !== null && isLineInFileDiff(file, thread.side, thread.line)) {
596 placed.add(thread.id);
597 }
598 }
@@ -576,9 +619,8 @@ function PullRequestCodeTab({
619 return created;
620 };
621
579 - for (const thread of detail.reviewThreads) {
580 - if (thread.path !== path || thread.line === null) continue;
581 - if (!placedThreadIds.has(thread.id)) continue;
622 + for (const thread of threadsByPath.get(path) ?? []) {
623 + if (thread.line === null || !placedThreadIds.has(thread.id)) continue;
624 groupAt(thread.side, thread.line).threads.push(thread);
625 }
626 if (commit === null) {