Skip to content

Commit 7603256

Browse files
committed
test(signals): make dbmon partial ticks reuse the store's rows
1 parent 9449845 commit 7603256

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

‎packages/signals/tests/store/reconcile-dbmon-ab.bench.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ function subscribeRows(state: any) {
4949

5050
function setupNext() {
5151
let applyTick!: (fresh: any[]) => void;
52+
const initial = makeData(ROWS, 0);
5253
const dispose = createRoot(d => {
53-
const [state, setState] = createStore({ rows: makeData(ROWS, 0) });
54+
const [state, setState] = createStore({ rows: initial });
5455
subscribeRows(state);
5556
applyTick = fresh =>
5657
setState((s: any) => {
@@ -59,17 +60,21 @@ function setupNext() {
5960
return d;
6061
});
6162
flush();
62-
return { applyTick, dispose };
63+
return { applyTick, dispose, last: initial };
6364
}
6465

65-
function runTicks(applyTick: (fresh: any[]) => void, partial: boolean) {
66+
type Lane = { applyTick: (fresh: any[]) => void; last: any[] };
67+
68+
function runTicks(lane: Lane, partial: boolean) {
6669
for (let frame = 1; frame <= 5; frame++) {
67-
let fresh = makeData(ROWS, frame);
70+
let fresh: any[];
6871
if (partial) {
69-
const prev = makeData(ROWS, frame - 1);
70-
fresh = prev.map((row, i) => (i < ROWS / 10 ? fresh[i] : row));
71-
}
72-
applyTick(fresh);
72+
// Reuse the row objects the store holds now, so the other 90% hit the identity skip.
73+
const changed = makeData(ROWS / 10, frame);
74+
fresh = lane.last.map((row, i) => (i < changed.length ? changed[i] : row));
75+
} else fresh = makeData(ROWS, frame);
76+
lane.applyTick(fresh);
77+
lane.last = fresh;
7378
flush();
7479
}
7580
}
@@ -82,14 +87,15 @@ function subscribeShallow(state: any) {
8287

8388
function setupShallowNext() {
8489
let applyTick!: (fresh: any[]) => void;
90+
const initial = makeData(ROWS, 0);
8591
const dispose = createRoot(d => {
86-
const [state, setState] = createStore(makeData(ROWS, 0), { shallow: true } as any);
92+
const [state, setState] = createStore(initial, { shallow: true } as any);
8793
subscribeShallow(state);
8894
applyTick = fresh => setState(reconcile(fresh, null) as any);
8995
return d;
9096
});
9197
flush();
92-
return { applyTick, dispose };
98+
return { applyTick, dispose, last: initial };
9399
}
94100

95101
const next = setupNext();
@@ -98,23 +104,23 @@ const shallowNext = setupShallowNext();
98104
bench(
99105
"dbmon full tick",
100106
() => {
101-
runTicks(next.applyTick, false);
107+
runTicks(next, false);
102108
},
103109
{ time: 4000, warmupIterations: 3 }
104110
);
105111

106112
bench(
107113
"dbmon partial tick",
108114
() => {
109-
runTicks(next.applyTick, true);
115+
runTicks(next, true);
110116
},
111117
{ time: 4000, warmupIterations: 3 }
112118
);
113119

114120
bench(
115121
"dbmon shallow full tick",
116122
() => {
117-
runTicks(shallowNext.applyTick, false);
123+
runTicks(shallowNext, false);
118124
},
119125
{ time: 3000, warmupIterations: 3 }
120126
);

‎packages/signals/tests/store/reconcile-dbmon.bench.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
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.
1919
import { afterAll, bench } from "vitest";
2020
import { 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

6363
function 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);
112117
const shallowStore = setup(true);
113118

114119
bench("dbmon full tick — deep reconcile", () => {
115-
runTicks(deep.applyTick, false);
120+
runTicks(deep, false);
116121
});
117122

118123
bench("dbmon full tick — shallow reconcile", () => {
119-
runTicks(shallowStore.applyTick, false);
124+
runTicks(shallowStore, false);
120125
});
121126

122127
bench("dbmon partial tick — deep reconcile", () => {
123-
runTicks(deep.applyTick, true);
128+
runTicks(deep, true);
124129
});
125130

126131
afterAll(() => {

0 commit comments

Comments
 (0)