@@ -4,14 +4,15 @@ import type { ModuleBuildMetrics, PluginBuildMetrics } from '../../shared/types'
44import type { RolldownEventsManager } from './events-manager'
55import fs from 'node:fs/promises'
66import { basename , dirname , join } from 'pathe'
7+ import { decodeReaderIndex , encodeReaderIndex } from './reader-index-codec'
78
89export const ROLLDOWN_LOG_CACHE_DIR = '.cache'
910export const ROLLDOWN_LOG_SESSION_SUMMARY_CACHE_FILENAME = 'session-summary.json'
10- export const ROLLDOWN_LOG_READER_INDEX_CACHE_FILENAME = 'reader-index.json '
11+ export const ROLLDOWN_LOG_READER_INDEX_CACHE_FILENAME = 'reader-index.bin '
1112export const ROLLDOWN_LOG_PACKAGE_SUMMARY_CACHE_FILENAME = 'package-summary.json'
1213export const ROLLDOWN_LOG_PLUGIN_SUMMARY_CACHE_FILENAME = 'plugin-summary.json'
1314export const ROLLDOWN_LOG_MODULE_METRICS_SUMMARY_CACHE_FILENAME = 'module-metrics-summary.json'
14- const ROLLDOWN_LOG_CACHE_VERSION = 2
15+ const ROLLDOWN_LOG_CACHE_VERSION = 3
1516
1617type ManagerSnapshot = ReturnType < RolldownEventsManager [ 'snapshot' ] >
1718
@@ -91,21 +92,6 @@ interface RolldownPluginSummaryCacheSnapshot {
9192 plugins : Array < [ number , PluginBuildMetricsSummary ] >
9293}
9394
94- interface RolldownReaderIndexCacheSnapshot {
95- reader : {
96- lastBytes : number
97- lastTimestamp : number
98- moduleEventIndex : Array < [ string , {
99- resolveIds : Array < [ string , IndexedHookCall ] >
100- loads : Array < [ string , IndexedHookCall ] >
101- transforms : Array < [ string , IndexedHookCall ] >
102- } ] >
103- stringRefIndex : Array < [ string , LineLocation ] >
104- pendingHookCallStarts : Array < [ string , PendingHookCallStart ] >
105- assetsReadyLocations : LineLocation [ ]
106- }
107- }
108-
10995interface RolldownPackageSummaryCacheSnapshot {
11096 packageSummaryTimestamp : number
11197 manager : ManagerSnapshot
@@ -167,6 +153,46 @@ class RolldownLogCacheFile<T> {
167153 }
168154}
169155
156+ class RolldownReaderIndexCacheFile {
157+ private constructor (
158+ readonly filepath : string ,
159+ ) {
160+ }
161+
162+ static forLogFile ( logFilepath : string ) {
163+ if ( basename ( logFilepath ) !== 'logs.json' )
164+ return undefined
165+
166+ return new RolldownReaderIndexCacheFile ( join ( dirname ( logFilepath ) , ROLLDOWN_LOG_CACHE_DIR , ROLLDOWN_LOG_READER_INDEX_CACHE_FILENAME ) )
167+ }
168+
169+ async read ( stat : Stats ) {
170+ try {
171+ return await decodeReaderIndex ( await fs . readFile ( this . filepath ) , toLogStat ( stat ) )
172+ }
173+ catch {
174+ return undefined
175+ }
176+ }
177+
178+ async write ( stat : Stats , state : RolldownLogCacheState ) {
179+ try {
180+ const chunks = await encodeReaderIndex ( state , toLogStat ( stat ) )
181+ const temporaryFilepath = `${ this . filepath } .tmp`
182+ await fs . mkdir ( dirname ( this . filepath ) , { recursive : true } )
183+ await fs . writeFile ( temporaryFilepath , chunks )
184+ await fs . rename ( temporaryFilepath , this . filepath )
185+ }
186+ catch {
187+ // The cache is best-effort. The reader can always rebuild from logs.json.
188+ }
189+ }
190+ }
191+
192+ function toLogStat ( stat : Stats ) {
193+ return { size : stat . size , mtime : stat . mtime . getTime ( ) }
194+ }
195+
170196function createMetricSummary ( ) : PluginMetricSummary {
171197 return {
172198 count : 0 ,
@@ -297,7 +323,7 @@ export class RolldownLogCache {
297323 private sessionSummaryCache : RolldownLogCacheFile < RolldownSessionSummaryCacheSnapshot > | undefined
298324 private moduleMetricsSummaryCache : RolldownLogCacheFile < RolldownModuleMetricsSummaryCacheSnapshot > | undefined
299325 private pluginSummaryCache : RolldownLogCacheFile < RolldownPluginSummaryCacheSnapshot > | undefined
300- private readerIndexCache : RolldownLogCacheFile < RolldownReaderIndexCacheSnapshot > | undefined
326+ private readerIndexCache : RolldownReaderIndexCacheFile | undefined
301327 private packageSummaryCache : RolldownLogCacheFile < RolldownPackageSummaryCacheSnapshot > | undefined
302328 private completeSessionWriteAttempted = false
303329 private summaryWriteAttempted = false
@@ -308,7 +334,7 @@ export class RolldownLogCache {
308334 this . sessionSummaryCache = RolldownLogCacheFile . forLogFile ( logFilepath , ROLLDOWN_LOG_SESSION_SUMMARY_CACHE_FILENAME )
309335 this . moduleMetricsSummaryCache = RolldownLogCacheFile . forLogFile ( logFilepath , ROLLDOWN_LOG_MODULE_METRICS_SUMMARY_CACHE_FILENAME )
310336 this . pluginSummaryCache = RolldownLogCacheFile . forLogFile ( logFilepath , ROLLDOWN_LOG_PLUGIN_SUMMARY_CACHE_FILENAME )
311- this . readerIndexCache = RolldownLogCacheFile . forLogFile ( logFilepath , ROLLDOWN_LOG_READER_INDEX_CACHE_FILENAME )
337+ this . readerIndexCache = RolldownReaderIndexCacheFile . forLogFile ( logFilepath )
312338 }
313339 this . packageSummaryCache = RolldownLogCacheFile . forLogFile ( logFilepath , ROLLDOWN_LOG_PACKAGE_SUMMARY_CACHE_FILENAME )
314340 }
@@ -351,11 +377,14 @@ export class RolldownLogCache {
351377 if ( ! sessionSummaryCache || ! moduleMetricsCache || ! pluginSummaryCacheExists || ! indexCache )
352378 return false
353379
354- if ( sessionSummaryCache . reader . lastBytes !== stat . size || indexCache . reader . lastBytes !== stat . size )
380+ if ( sessionSummaryCache . reader . lastBytes !== stat . size || indexCache . lastBytes !== stat . size )
355381 return false
356382
357383 this . restoreSessionSnapshot ( sessionSummaryCache , state , moduleMetricsCache )
358- this . restoreReaderIndexSnapshot ( indexCache , state )
384+ state . moduleEventIndex = indexCache . moduleEventIndex
385+ state . stringRefIndex = indexCache . stringRefIndex
386+ state . pendingHookCallStarts = indexCache . pendingHookCallStarts
387+ state . assetsReadyLocations = indexCache . assetsReadyLocations
359388 this . completeSessionWriteAttempted = true
360389 this . summaryWriteAttempted = true
361390 return true
@@ -365,27 +394,18 @@ export class RolldownLogCache {
365394 if ( state . lastBytes || state . manager . eventCount )
366395 return false
367396
368- const [ sessionSummaryCache , moduleMetricsCache , _pluginSummaryCache , indexCache ] = await Promise . all ( [
397+ // The module list needs the metrics; the index is loaded on a full read.
398+ const [ sessionSummaryCache , moduleMetricsCache ] = await Promise . all ( [
369399 this . sessionSummaryCache ?. read ( stat ) ,
370400 this . moduleMetricsSummaryCache ?. read ( stat ) ,
371- this . pluginSummaryCache ?. read ( stat ) ,
372- this . readerIndexCache ?. read ( stat ) ,
373401 ] )
374- if ( ! sessionSummaryCache )
375- return false
376-
377- if ( sessionSummaryCache . reader . lastBytes !== stat . size )
402+ if ( ! sessionSummaryCache || sessionSummaryCache . reader . lastBytes !== stat . size )
378403 return false
379404
380405 this . restoreSessionSnapshot ( sessionSummaryCache , state , moduleMetricsCache )
381- const hasCompleteCache = ! ! moduleMetricsCache && ! ! _pluginSummaryCache && ! ! indexCache
382- if ( indexCache ?. reader . lastBytes === stat . size )
383- this . restoreReaderIndexSnapshot ( indexCache , state )
384- else
385- this . clearReaderIndexState ( state )
406+ this . clearReaderIndexState ( state )
386407 this . summaryWriteAttempted = true
387- this . completeSessionWriteAttempted = hasCompleteCache
388- return hasCompleteCache ? 'complete' : 'summary'
408+ return 'summary' as const
389409 }
390410
391411 async restorePackageSummary ( stat : Stats , state : RolldownLogCacheState ) {
@@ -416,17 +436,19 @@ export class RolldownLogCache {
416436 this . sessionSummaryCache ?. write ( stat , this . createSessionSummarySnapshot ( state ) ) ,
417437 this . moduleMetricsSummaryCache ?. write ( stat , this . createModuleMetricsSummarySnapshot ( state ) ) ,
418438 this . pluginSummaryCache ?. write ( stat , this . createPluginSummarySnapshot ( state ) ) ,
419- this . readerIndexCache ?. write ( stat , this . createReaderIndexSnapshot ( state ) ) ,
439+ this . readerIndexCache ?. write ( stat , state ) ,
420440 ] )
421441 }
422442
423443 async writeSummary ( stat : Stats , state : RolldownLogCacheState ) {
424444 this . summaryWriteAttempted = true
445+ // Writes the same sidecars as a complete session, so don't write them again for the full read.
446+ this . completeSessionWriteAttempted = true
425447 await Promise . all ( [
426448 this . sessionSummaryCache ?. write ( stat , this . createSessionSummarySnapshot ( state ) ) ,
427449 this . moduleMetricsSummaryCache ?. write ( stat , this . createModuleMetricsSummarySnapshot ( state ) ) ,
428450 this . pluginSummaryCache ?. write ( stat , this . createPluginSummarySnapshot ( state ) ) ,
429- this . readerIndexCache ?. write ( stat , this . createReaderIndexSnapshot ( state ) ) ,
451+ this . readerIndexCache ?. write ( stat , state ) ,
430452 ] )
431453 }
432454
@@ -471,23 +493,6 @@ export class RolldownLogCache {
471493 }
472494 }
473495
474- private createReaderIndexSnapshot ( state : RolldownLogCacheState ) : RolldownReaderIndexCacheSnapshot {
475- return {
476- reader : {
477- lastBytes : state . lastBytes ,
478- lastTimestamp : state . lastTimestamp ,
479- moduleEventIndex : Array . from ( state . moduleEventIndex . entries ( ) ) . map ( ( [ module , index ] ) => [ module , {
480- resolveIds : Array . from ( index . resolveIds . entries ( ) ) ,
481- loads : Array . from ( index . loads . entries ( ) ) ,
482- transforms : Array . from ( index . transforms . entries ( ) ) ,
483- } ] ) ,
484- stringRefIndex : Array . from ( state . stringRefIndex . entries ( ) ) ,
485- pendingHookCallStarts : Array . from ( state . pendingHookCallStarts . entries ( ) ) ,
486- assetsReadyLocations : state . assetsReadyLocations ,
487- } ,
488- }
489- }
490-
491496 private createPackageSummarySnapshot ( state : RolldownLogCacheState ) : RolldownPackageSummaryCacheSnapshot {
492497 return {
493498 packageSummaryTimestamp : state . packageSummaryTimestamp ,
@@ -521,17 +526,6 @@ export class RolldownLogCache {
521526 } )
522527 }
523528
524- private restoreReaderIndexSnapshot ( snapshot : RolldownReaderIndexCacheSnapshot , state : RolldownLogCacheState ) {
525- state . moduleEventIndex = new Map ( snapshot . reader . moduleEventIndex . map ( ( [ module , item ] ) => [ module , {
526- resolveIds : new Map ( item . resolveIds ) ,
527- loads : new Map ( item . loads ) ,
528- transforms : new Map ( item . transforms ) ,
529- } ] ) )
530- state . stringRefIndex = new Map ( snapshot . reader . stringRefIndex )
531- state . pendingHookCallStarts = new Map ( snapshot . reader . pendingHookCallStarts )
532- state . assetsReadyLocations = snapshot . reader . assetsReadyLocations
533- }
534-
535529 private restorePackageSummarySnapshot ( snapshot : RolldownPackageSummaryCacheSnapshot , state : RolldownLogCacheState ) {
536530 state . manager . restore ( snapshot . manager )
537531 state . packageSummaryTimestamp = snapshot . packageSummaryTimestamp
0 commit comments