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