@@ -366,32 +366,33 @@ export type OpenCodeUsageReaderEnv = FileSystem.FileSystem | Path.Path | Provide
366366
367367// Rename when parseOpenCodeMessage output or the fingerprint changes; a bad file reads as empty.
368368const CACHE_FILE_NAME = "usage-opencode-cache-v1.json" ;
369+ // Tuples in plain records; ReadonlyMap codecs made encode and decode several times slower.
369370const CacheFile = Schema . fromJsonString (
370- Schema . toCodecJson (
371- Schema . ReadonlyMap (
372- Schema . String ,
373- Schema . ReadonlyMap (
371+ Schema . Record (
372+ Schema . String ,
373+ Schema . Array (
374+ Schema . Tuple ( [
374375 Schema . Number ,
375- Schema . Struct ( {
376- fingerprint : Schema . NullOr ( Schema . String ) ,
377- created : Schema . NullOr ( Schema . Number ) ,
378- record : Schema . NullOr (
379- Schema . Struct ( {
380- provider : UsageProviderKind ,
381- timestampMs : Schema . Number ,
382- model : Schema . String ,
383- sessionId : Schema . String ,
384- totals : UsageTokenTotals ,
385- reportedCostUsd : Schema . NullOr ( Schema . Number ) ,
386- speed : Schema . Literals ( [ "standard" , "fast" , "ultrafast" ] ) ,
387- dedupeKey : Schema . NullOr ( Schema . String ) ,
388- } ) ,
389- ) ,
390- } ) ,
391- ) ,
376+ Schema . NullOr ( Schema . String ) ,
377+ Schema . NullOr ( Schema . Number ) ,
378+ Schema . NullOr (
379+ Schema . Struct ( {
380+ provider : UsageProviderKind ,
381+ timestampMs : Schema . Number ,
382+ model : Schema . String ,
383+ sessionId : Schema . String ,
384+ totals : UsageTokenTotals ,
385+ reportedCostUsd : Schema . NullOr ( Schema . Number ) ,
386+ speed : Schema . Literals ( [ "standard" , "fast" , "ultrafast" ] ) ,
387+ dedupeKey : Schema . NullOr ( Schema . String ) ,
388+ } ) ,
389+ ) ,
390+ ] ) ,
392391 ) ,
393392 ) ,
394393) ;
394+ const decodeCacheFile = Schema . decodeUnknownEffect ( CacheFile ) ;
395+ const encodeCacheFile = Schema . encodeEffect ( CacheFile ) ;
395396// Parsed rows by cache file, so refreshes and restarts re-read only rows that changed.
396397const rowCaches = new Map < string , OpenCodeUsageCache > ( ) ;
397398
@@ -408,10 +409,16 @@ export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCode
408409 const cachePath = path . join ( ( yield * ProviderHost . ProviderHost ) . paths . stateDir , CACHE_FILE_NAME ) ;
409410 if ( ! rowCaches . has ( cachePath ) ) {
410411 const loaded = yield * fileSystem . readFileString ( cachePath ) . pipe (
411- Effect . flatMap ( Schema . decodeUnknownEffect ( CacheFile ) ) ,
412- Effect . orElseSucceed ( ( ) => [ ] ) ,
412+ Effect . flatMap ( decodeCacheFile ) ,
413+ Effect . orElseSucceed ( ( ) => ( { } ) ) ,
413414 ) ;
414- if ( ! rowCaches . has ( cachePath ) ) rowCaches . set ( cachePath , new Map ( loaded ) ) ;
415+ const tables = Object . entries ( loaded ) . map ( ( [ key , rows ] ) => {
416+ const entries = rows . map ( ( [ id , fingerprint , created , record ] ) => {
417+ return [ id , { fingerprint, created, record } ] as const ;
418+ } ) ;
419+ return [ key , new Map ( entries ) ] as const ;
420+ } ) ;
421+ if ( ! rowCaches . has ( cachePath ) ) rowCaches . set ( cachePath , new Map ( tables ) ) ;
415422 }
416423 const rowCache = rowCaches . get ( cachePath ) ! ;
417424 const before = new Map ( rowCache ) ;
@@ -436,7 +443,13 @@ export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCode
436443 rowCache . size !== before . size ||
437444 rowCache . entries ( ) . some ( ( [ key , rows ] ) => before . get ( key ) !== rows )
438445 ) {
439- yield * Schema . encodeEffect ( CacheFile ) ( rowCache ) . pipe (
446+ const tables = rowCache . entries ( ) . map ( ( [ key , rows ] ) => {
447+ const entries = rows
448+ . entries ( )
449+ . map ( ( [ id , row ] ) => [ id , row . fingerprint , row . created , row . record ] as const ) ;
450+ return [ key , entries . toArray ( ) ] as const ;
451+ } ) ;
452+ yield * encodeCacheFile ( Object . fromEntries ( tables ) ) . pipe (
440453 Effect . flatMap ( ( contents ) => writeFileStringAtomically ( { filePath : cachePath , contents } ) ) ,
441454 Effect . ignore ,
442455 ) ;
0 commit comments