@@ -84,8 +84,13 @@ export type Connection = {
8484 | undefined ;
8585 readonly debug ?: DebugDelegate | undefined ;
8686 lastPushedEpoch : number ;
87+ // Cached result of primaryKeyConstraintFromFilters(), computed once at
88+ // connect() time. Avoids recomputing from filters on every #fetch() call.
8789 pkConstraint : Constraint | undefined ;
90+ // Per-connection cache of #getOrCreateIndex results, keyed by constraint
91+ // shape + sort. Avoids repeated JSON.stringify + Map lookups on every fetch.
8892 indexCache : Map < string , Index > ;
93+ // Pre-computed sort key for index cache lookups, avoids recomputing per fetch.
8994 requestedSortKey : string ;
9095} ;
9196
@@ -101,6 +106,8 @@ export class MemorySource implements Source {
101106 readonly #columns: Record < string , SchemaValue > ;
102107 readonly #primaryKey: PrimaryKey ;
103108 readonly #primaryIndexSort: Ordering ;
109+ // Cached JSON.stringify of #primaryIndexSort, used in #getPrimaryIndex()
110+ // and constructor to avoid repeated serialization.
104111 readonly #primaryIndexKey: string ;
105112 readonly #indexes: Map < string , Index > = new Map ( ) ;
106113 readonly #connections: Connection [ ] = [ ] ;
@@ -270,7 +277,6 @@ export class MemorySource implements Source {
270277 const connectionComparator = ( r1 : Row , r2 : Row ) =>
271278 compareRows ( r1 , r2 ) * ( req . reverse ? - 1 : 1 ) ;
272279
273- // Patch 7: Use cached pkConstraint from connection instead of recomputing
274280 const pkConstraint = conn . pkConstraint ;
275281 // The primary key constraint will be more limiting than the constraint
276282 // so swap out to that if it exists.
@@ -295,7 +301,6 @@ export class MemorySource implements Source {
295301 indexSort . push ( ...requestedSort ) ;
296302 }
297303
298- // Patch 4: Cache index lookup per connection
299304 let constraintShapeKey = '' ;
300305 if ( fetchOrPkConstraint ) {
301306 for ( const key of Object . keys ( fetchOrPkConstraint ) ) {
0 commit comments