1515//
1616// Full ticks change every non-name cell (nothing skippable — the worst case
1717// for fine-grained diffing); partial ticks reuse 90% of row objects by
18- // reference and exercise the identity skip on both paths .
18+ // reference and exercise the identity skip on the deep path .
1919import { afterAll , bench } from "vitest" ;
2020import { createRenderEffect , createRoot , createStore , flush , reconcile } from "../../src/index.js" ;
2121
@@ -62,9 +62,10 @@ function subscribeRow(read: (q: number) => any, name: () => any, count: () => an
6262
6363function setup ( shallow : boolean ) {
6464 let applyTick ! : ( fresh : any [ ] ) => void ;
65+ const initial = makeData ( ROWS , 0 ) ;
6566 const dispose = createRoot ( d => {
6667 if ( shallow ) {
67- const [ state , setState ] = createStore ( makeData ( ROWS , 0 ) , { shallow : true } ) ;
68+ const [ state , setState ] = createStore ( initial , { shallow : true } ) ;
6869 for ( let i = 0 ; i < ROWS ; i ++ ) {
6970 subscribeRow (
7071 q => ( ) => state [ i ] . queries [ q ] ,
@@ -75,7 +76,7 @@ function setup(shallow: boolean) {
7576 }
7677 applyTick = fresh => setState ( reconcile ( fresh , null ) ) ;
7778 } else {
78- const [ state , setState ] = createStore ( { rows : makeData ( ROWS , 0 ) } ) ;
79+ const [ state , setState ] = createStore ( { rows : initial } ) ;
7980 for ( let i = 0 ; i < ROWS ; i ++ ) {
8081 const db = state . rows [ i ] ;
8182 subscribeRow (
@@ -93,17 +94,21 @@ function setup(shallow: boolean) {
9394 return d ;
9495 } ) ;
9596 flush ( ) ;
96- return { applyTick, dispose } ;
97+ return { applyTick, dispose, last : initial } ;
9798}
9899
99- function runTicks ( applyTick : ( fresh : any [ ] ) => void , partial : boolean ) {
100+ type Lane = { applyTick : ( fresh : any [ ] ) => void ; last : any [ ] } ;
101+
102+ function runTicks ( lane : Lane , partial : boolean ) {
100103 for ( let frame = 1 ; frame <= 5 ; frame ++ ) {
101- let fresh = makeData ( ROWS , frame ) ;
104+ let fresh : any [ ] ;
102105 if ( partial ) {
103- const prev = makeData ( ROWS , frame - 1 ) ;
104- fresh = prev . map ( ( row , i ) => ( i < ROWS / 10 ? fresh [ i ] : row ) ) ;
105- }
106- applyTick ( fresh ) ;
106+ // Reuse the row objects the store holds now, so the other 90% hit the identity skip.
107+ const changed = makeData ( ROWS / 10 , frame ) ;
108+ fresh = lane . last . map ( ( row , i ) => ( i < changed . length ? changed [ i ] : row ) ) ;
109+ } else fresh = makeData ( ROWS , frame ) ;
110+ lane . applyTick ( fresh ) ;
111+ lane . last = fresh ;
107112 flush ( ) ;
108113 }
109114}
@@ -112,15 +117,15 @@ const deep = setup(false);
112117const shallowStore = setup ( true ) ;
113118
114119bench ( "dbmon full tick — deep reconcile" , ( ) => {
115- runTicks ( deep . applyTick , false ) ;
120+ runTicks ( deep , false ) ;
116121} ) ;
117122
118123bench ( "dbmon full tick — shallow reconcile" , ( ) => {
119- runTicks ( shallowStore . applyTick , false ) ;
124+ runTicks ( shallowStore , false ) ;
120125} ) ;
121126
122127bench ( "dbmon partial tick — deep reconcile" , ( ) => {
123- runTicks ( deep . applyTick , true ) ;
128+ runTicks ( deep , true ) ;
124129} ) ;
125130
126131afterAll ( ( ) => {
0 commit comments