@@ -175,6 +175,76 @@ function parseSlice(slice: DiffSlice, cacheKey: string, ignoreWhitespace: boolea
175175 return parsed ;
176176}
177177
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+
178248interface FileViewedToggleState {
179249 readonly enabled : boolean ;
180250 readonly isViewed : ( path : string ) => boolean ;
@@ -490,20 +560,10 @@ function PullRequestCodeTab({
490560 // Agent context is local, so it does not require host comment permission.
491561 const canSelectLines =
492562 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 ,
507567 ) ;
508568 const testFileCount = loadedFiles . filter ( ( file ) =>
509569 isTestChangedFile ( resolveFileDiffPath ( file ) ) ,
@@ -522,33 +582,16 @@ function PullRequestCodeTab({
522582 slice . omittedFileStats . map ( ( file ) => [ file . path , file ] as const ) ,
523583 ) ,
524584 ) ;
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- ) ;
538585 const fileTreeEntries = diffFileTreeEntries ( files ) ;
586+ const threadsByPath = reviewThreadsByPath ( detail . reviewThreads ) ;
539587 // File membership does not guarantee a rendered hunk for the thread.
540588 const placedThreadIds = ( ( ) => {
541589 const placed = new Set < string > ( ) ;
542590 // Commit diffs use different line coordinates; list conversations separately.
543591 if ( commit !== null ) return placed ;
544592 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 ) ) {
552595 placed . add ( thread . id ) ;
553596 }
554597 }
@@ -575,9 +618,8 @@ function PullRequestCodeTab({
575618 return created ;
576619 } ;
577620
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 ;
581623 groupAt ( thread . side , thread . line ) . threads . push ( thread ) ;
582624 }
583625 if ( commit === null ) {
0 commit comments