Skip to content

Commit 1a3f87f

Browse files
signals: benchmarks measure the built package; the fold queue's pre-batch record (CodSpeed follow-up to #3774) (#3776)
* bench(signals): benchmarks measure the built package In benchmark mode the benches' src/index.js import is aliased to the dist entry of the selected tier (SIGNALS_TIER, default prod for benchmarks) and the dist is externalized so Node links it natively — the prod build preserves modules, and vite-node's SSR transform would turn its cross-module imports back into the namespace-object property loads the alias exists to take out of the measurement. SIGNALS_BENCH=source keeps a from-source loop for local iteration. A missing or stale dist fails the run with the rebuild instruction instead of timing code not under test. CodSpeed already builds before benching; its first run after this re-baselines every signals bench. Plan sec. 43.1. Co-authored-by: Cursor <[email protected]> * store: the fold queue records the pre-batch backing beside the target The weak map keyed by target kept every container's last adopted-away backing alive until its next adoption: for a keyed reconcile the previous tick's whole tree promoted out of the nursery every tick (saturated listened-paths +15%, reconcile tree +10-13% over next on the dist, all of it GC); for a store adopted once, the old tree for the store's lifetime. The record is now foldOlds, parallel to foldList: t.v at queueFold for every queued target, released with the drain. That one record is the fold's base in every case (draft, eager adoption, mid-batch privatization, draft over an adopted raw), so privatizedOlds, draftedAdoptions and the adoption flag go. The target has no slot for it (ARRAY SHAPE RULE), so the by-target lookups (preBatch: a node born onto an open staging, a committed-frame read of a node-less staging, a park) index the list through a map built on first use per batch; the drain never builds it. After, on the dist vs next: tree reverse +3%, shuffle +4%, saturated +1.5%, sparse parity; fresh stores / owned backings / enumerate / storebench unchanged; GC totals identical to the head. Signals 4894/0, web 1132/0, solid 819/0; every size cap holds. Plan sec. 43.2. Co-authored-by: Cursor <[email protected]> * bench(signals): projection-root-write loads under the dev tier The warm-up store write sat inside createRoot, where the dev tier's owned-scope write check throws — the file failed to load under SIGNALS_TIER=dev, so CodSpeed compared its prod-dist values against a stale base (two of the four 'regressions' on #3776). Moved outside the root. Plan sec. 43.1 records the first dist-mode CodSpeed run. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]>
1 parent 1b9ceb6 commit 1a3f87f

5 files changed

Lines changed: 264 additions & 61 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Store fold queue: the pre-batch committed backing is recorded beside the queued target and released with the drain. The weak map it replaces kept every container's last adopted-away backing alive until its next adoption — for a keyed reconcile, the previous tick's whole tree promoted out of the nursery every tick (the saturated listened-paths and reconcile-tree shapes ran 10–15% over `next` on that alone); for a store adopted once, the old tree for the store's lifetime. The per-batch side maps for mid-batch privatizations and drafts over adoptions fold into the same record.

‎documentation/plans/size-reduction-carve-step1.md‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,6 +5461,116 @@ A-rules carry `Mechanism (L2, 2026-10-04)` against the running code; the
54615461
pre-L2 paragraphs are under History verbatim. The branch's fate: **push
54625462
and open one PR against `next`**, this document the design record.
54635463

5464+
## 43. After the merge — CodSpeed against the dist; the fold queue's record (2026-10-04, 1:30–2:10 AM)
5465+
5466+
#3774 merged as `1b9ceb679`. CodSpeed (not a required check) listed 14
5467+
regressions on it; §41.1 had already shown its method — vite-node, one ESM
5468+
module per source file, every cross-module call a namespace-object
5469+
property load — penalizes the carve's layout and contradicts the dist on
5470+
the core suite. Four store shapes had not been dist-measured. This section
5471+
is both halves: the audit, and making the next report mean something.
5472+
5473+
### 43.1 Benchmarks measure the built package
5474+
5475+
`packages/signals/vite.config.ts`: in benchmark mode the benches'
5476+
`src/index.js` import is aliased to the dist entry of the tier
5477+
(`SIGNALS_TIER`, default **prod** for benchmarks — the artifact users ship;
5478+
the test suite keeps dev), and the dist is **externalized** so Node links it
5479+
natively — the prod build preserves modules, and vite-node's SSR transform
5480+
would have turned its cross-module imports straight back into the namespace
5481+
loads the alias exists to remove. `SIGNALS_BENCH=source` keeps a from-source
5482+
loop for local iteration. A stale or missing dist fails the run with the
5483+
rebuild instruction instead of silently timing code not under test (a fresh
5484+
checkout bumps `src` mtimes — rebuild). The CodSpeed workflow already
5485+
builds before benching; no workflow change. Web's benches already measured
5486+
signals through the package `exports` (its `development` condition →
5487+
`dist/dev.js`); signals' own were the only ones on source.
5488+
5489+
Consequence for CodSpeed: the first run after this lands re-baselines every
5490+
signals bench (a different series — bundled prod instead of per-module dev
5491+
source). The 14 "regressions" on `1b9ceb679` are superseded by that
5492+
re-baseline; the four shapes below are the only ones that were real, and
5493+
43.2 is their fix.
5494+
5495+
**The PR's first CodSpeed run** (#3776, `3ab88fd5e` vs `next`): 84
5496+
improved (×2–5 — the layout cost, now gone from the measurement: dbmon
5497+
full tick 750 → 337 ms of simulated time, createOwners ×5), 100 untouched,
5498+
4 "regressed", flagged by CodSpeed itself as compared across different
5499+
runtime environments. All four are the instrument: `merge-merge-mixed(deep
5500+
0, 100, 3, 2)` 46 → 819 µs is a GC landing inside a ~75 ns operation's
5501+
measured window (`--predictable-gc-schedule`; the operation itself is 72–98
5502+
ns on prod and dev dist alike, JIT or `--no-opt`, every variant);
5503+
`merge-static(deep)` −7% is the same window's small cousin; and the two
5504+
`projection-root-write` rows had no live base at all — that file **fails to
5505+
load under the dev tier** (a store write inside `createRoot` trips the
5506+
owned-scope check), so its base was stale while the prod run has values
5507+
(20 µs each, consistent with the file's own reference line). The warm-up
5508+
write moved outside the root so the file runs in every tier.
5509+
5510+
### 43.2 The four unmeasured shapes, on the dist
5511+
5512+
Harness `/tmp/carve/cs3.mjs`: the CodSpeed benches reproduced exactly (one
5513+
`createEffect` walking all 1111 tree nodes; one effect reading all ~12k
5514+
leaves), prod dist, Node 26, fork (`next` @ `8950bb7b7`) vs L2, interleaved.
5515+
5516+
| shape (CodSpeed name) | fork | L2 @ `1b9ceb679` | |
5517+
|---|---|---|---|
5518+
| store reconcile tree reverse: 1111 keyed nodes | 1.21–1.25 ms | 1.36–1.38 | +11% |
5519+
| store reconcile tree shuffle: 1111 keyed nodes | 1.02–1.04 | 1.12–1.18 | +10–13% |
5520+
| reconcile: deep tree, all ~12k paths subscribed | 3.79 | 4.37–4.41 | +15% |
5521+
| reconcile: deep tree, 10 of ~12k paths subscribed | 0.37 | 0.22–0.38 | parity/ahead |
5522+
| dbmon shallow full tick (1000 rows; `shapes4.mjs`) | 0.89–0.93 | 0.67–0.69 | L2 −25% |
5523+
5524+
So two of CodSpeed's four were real (the tree pair is one shape), one was
5525+
the harness (dbmon shallow — L2 is a quarter faster on the dist), one was
5526+
noise.
5527+
5528+
**Cause.** The profile's self-time differences were small (`queueFold`,
5529+
`drainFolds`, `preBatch`, `foldTarget`, `parentSlotKey` ≈ 7–9%) but
5530+
`(garbage collector)` was 16% against 6%, and removing the two weak-map
5531+
operations alone took the saturated shape *under* the fork. §41.5's fold
5532+
queue kept each adopted container's pre-batch backing in a `WeakMap`
5533+
keyed by target, "written in place batch after batch — no churn". No
5534+
churn, but **retention**: an entry lives as long as its target, so every
5535+
container held its last adopted-away backing until its next adoption — for
5536+
a keyed reconcile, the previous tick's entire tree (5,400 containers,
5537+
12k strings) stayed reachable through the scavenges, was promoted, and
5538+
became old-generation garbage every tick; for a store adopted once, the
5539+
old tree is retained for the store's lifetime. A memory bug as much as a
5540+
speed one.
5541+
5542+
**Fix.** The record moves beside the target: `foldOlds`, parallel to
5543+
`foldList`, set to `t.v` at `queueFold` for **every** queued target
5544+
(adoption or draft) and cleared with the drain. That one record is the
5545+
fold's base in every case — a draft's backing does not move before the
5546+
fold (the record is `t.v` still), an adoption's swaps eagerly (the record
5547+
is the frame committed readers keep), a mid-batch privatization or a draft
5548+
over an adopted raw moves it again (the record is what the batch started
5549+
from) — so `privatizedOlds`, `draftedAdoptions` and the adoption flag on
5550+
`queueFold` go too. The target has no slot for the record (ARRAY SHAPE RULE:
5551+
20 named fields, 21 ≡ 0 mod 3 normalizes array targets to dictionary
5552+
mode), so the by-target lookups (`preBatch`: a container node born onto an
5553+
open staging, a committed-frame read of a node-less staging, `_storePark`)
5554+
go through an index map built on first use per batch and kept in step by
5555+
`queueFold`; the drain walks the two lists together and never builds it.
5556+
The arrays' `length = 0` releases the entries — nothing outlives the batch.
5557+
5558+
**After** (same harness, interleaved, 3 rounds, medians): tree reverse
5559+
1.232 → **1.267** (+3%), shuffle 1.196 → **1.245** (+4%, 1.137 vs 1.153 in
5560+
the profiled run), saturated 3.166 → **3.212** (+1.5%), sparse 0.333 →
5561+
0.330. The residual is the queue itself — `drainFolds` + `foldTarget` +
5562+
`parentSlotKey` ≈ 3.5% of a tick — the price of a staging per adopted
5563+
container under the hold model. `adoptPB` at 21% self is the per-adopted-raw
5564+
`storeLookup.set` registration, the same in both arms (the lever listed with the deep-tick ruling, §42).
5565+
No regression elsewhere: fresh stores 1.00–1.27 ms (fork 1.16), 2000 owned
5566+
backings 0.30–0.32 (fork 0.35), enumerate 7.3/7.9 (fork 8.5/8.1),
5567+
storebench parity; GC totals over the store battery identical to the head
5568+
(40 scavenges 87 ms, 4 major 94 ms; fork 38/108 + 5/98). Size: within every
5569+
cap (`+ createStore` 14519, every-store 28755, page base 44029 — 1 B under
5570+
its cap — live 47654); a few bytes over the head per scenario, the arrays
5571+
and the lazy index against the three collections. Signals **4894 / 0**,
5572+
web 1132 / 0, solid 819 / 0.
5573+
54645574
---
54655575

54665576
## Appendix — ledger (verbatim)

‎packages/signals/src/store/store.ts‎

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,11 @@ function ensurePB(target: StoreTarget): Record<PropertyKey, any> {
720720
// adoption and the draft is a mainline layer above it (#3612/#3688: a
721721
// mainline setter mid-hold publishes mainline; the held keys stay held).
722722
pb = target.pb = cloneRaw(pb, target);
723-
draftedAdoptions.add(target);
724723
const k = target.k;
725724
if (k !== null && !(k._config & CONFIG_HELD)) k._pendingValue = pb;
726725
} else if (pb === null) {
727726
const v = target.v;
728-
if (!directCommit) queueFold(target, false);
727+
if (!directCommit) queueFold(target);
729728
if (
730729
!target.ch &&
731730
!Array.isArray(v) &&
@@ -747,10 +746,8 @@ function privatizeCommitted(target: StoreTarget): void {
747746
if (isOwned(target.v)) return;
748747
const before = target.v;
749748
const clone = cloneRaw(before, target);
750-
// Mid-batch (a staging is open on this target): the fold must still see
751-
// the backing the batch started from (`preBatch`).
752-
if (target.pb !== null && target.pb !== before && !draftedAdoptions.has(target))
753-
privatizedOlds.set(target, before);
749+
// Mid-batch (a staging is open on this target) the fold still sees the
750+
// backing the batch started from: the queue recorded it (`foldOlds`).
754751
target.v = clone;
755752
target.ch = false;
756753
if (target.u) {
@@ -809,7 +806,7 @@ export function adoptPB(
809806
incoming: Record<PropertyKey, any>,
810807
notify = true
811808
): void {
812-
if (!directCommit) queueFold(target, true);
809+
if (!directCommit) queueFold(target);
813810
let base: Record<PropertyKey, any>;
814811
if (target.pb !== null) {
815812
if (target.ovl) materializePB(target);
@@ -852,35 +849,48 @@ export function adoptPB(
852849
* of the tick's young-generation GC, and the whole of its gap to `next`
853850
* (plan sec. 41.5).
854851
*
855-
* The pre-batch committed backing (`preBatch`): a DRAFT's is `t.v` itself —
856-
* the backing does not move until the fold — so nothing is recorded for
857-
* it (2000 fresh one-key stores per flush recorded 2000 weak-map entries
858-
* for nothing, 2× the fork); an ADOPTION swaps the backing eagerly, so its
859-
* pre-batch backing is kept in a weak map written in place batch after
860-
* batch (the containers of a keyed reconcile are reused — no churn); the
861-
* two rare cases that move a draft's backing mid-batch — a privatization
862-
* (a child's path copy repointed the parent), a draft over an adopted raw
863-
* (cloned) — are kept in small per-batch maps. */
852+
* Beside each target, its PRE-BATCH committed backing — `t.v` as the batch
853+
* opened on it (`foldOlds`, parallel to `foldList`). That one record is the
854+
* fold's base in every case: a draft's backing does not move until the fold
855+
* (so the record is `t.v` still), an adoption swaps it eagerly (the record
856+
* is the frame committed readers keep), a mid-batch privatization or a
857+
* draft over an adopted raw moves it again (the record is what the batch
858+
* started from, which is all the fold wants to know). The record lives
859+
* exactly as long as the batch: a weak map keyed by target kept every
860+
* container's LAST adopted-away backing alive until its next adoption —
861+
* for a keyed reconcile, the whole previous tree promoted out of the
862+
* nursery every tick (the saturated listened-paths shape ran 15–25% over
863+
* the fork, all of it this; plan sec. 43.2) — and for a store adopted
864+
* once, for its lifetime.
865+
*
866+
* The target carries no slot for its record (ARRAY SHAPE RULE, target.ts),
867+
* so a lookup by target (`preBatch`: a container node born onto an open
868+
* staging, a committed-frame read of a node-less staging, a park) indexes
869+
* the list through a map built on first use per batch — the drain itself
870+
* walks the two lists in step and never builds it. */
864871
let foldList: StoreTarget[] = [];
872+
let foldOlds: Record<PropertyKey, any>[] = [];
865873
let foldSpare: StoreTarget[] = [];
866-
const adoptOlds = new WeakMap<StoreTarget, Record<PropertyKey, any>>();
867-
const privatizedOlds = new Map<StoreTarget, Record<PropertyKey, any>>();
868-
const draftedAdoptions = new Set<StoreTarget>();
874+
let foldOldsSpare: Record<PropertyKey, any>[] = [];
875+
let foldIndex: Map<StoreTarget, number> | null = null;
869876

870877
/** Before the staging is set and before an adoption swaps the backing. */
871-
function queueFold(target: StoreTarget, adoption: boolean): void {
878+
function queueFold(target: StoreTarget): void {
872879
if (target.pb !== null) return;
873880
schedule();
874-
if (adoption) adoptOlds.set(target, target.v);
881+
if (foldIndex !== null) foldIndex.set(target, foldList.length);
875882
foldList.push(target);
883+
foldOlds.push(target.v);
876884
}
877885

878-
/** The committed backing before this batch (valid while `pb !== null`). An
879-
* eager adoption nothing wrote after is `pb === v`; one a draft wrote over
880-
* is listed; a privatized draft's is listed; any other draft's is `v`. */
886+
/** The committed backing before this batch (valid while `pb !== null`). */
881887
function preBatch(t: StoreTarget): Record<PropertyKey, any> {
882-
if (t.pb === t.v || draftedAdoptions.has(t)) return adoptOlds.get(t) ?? t.v;
883-
return privatizedOlds.get(t) ?? t.v;
888+
if (foldIndex === null) {
889+
foldIndex = new Map();
890+
for (let i = 0; i < foldList.length; i++) foldIndex.set(foldList[i], i);
891+
}
892+
const at = foldIndex.get(t);
893+
return at === undefined ? t.v : foldOlds[at];
884894
}
885895

886896
/** The flush committed its pending nodes: fold every pending backing whose
@@ -891,21 +901,24 @@ function preBatch(t: StoreTarget): Record<PropertyKey, any> {
891901
function drainFolds(): void {
892902
if (foldList.length !== 0) {
893903
const list = foldList;
904+
const olds = foldOlds;
894905
foldList = foldSpare;
906+
foldOlds = foldOldsSpare;
895907
foldSpare = list;
908+
foldOldsSpare = olds;
909+
foldIndex = null;
896910
for (let i = 0; i < list.length; i++) {
897911
const t = list[i];
898912
if (t.pb === null) continue; // folded already (a direct commit)
899913
const k = t.k;
900-
if (k !== null && k._pendingValue !== NOT_PENDING)
901-
foldList.push(t); // held: commits with its hold
902-
else foldTarget(t, preBatch(t));
914+
if (k !== null && k._pendingValue !== NOT_PENDING) {
915+
// held: commits with its hold
916+
foldList.push(t);
917+
foldOlds.push(olds[i]);
918+
} else foldTarget(t, olds[i]);
903919
}
904920
list.length = 0;
905-
if (foldList.length === 0) {
906-
if (privatizedOlds.size !== 0) privatizedOlds.clear();
907-
if (draftedAdoptions.size !== 0) draftedAdoptions.clear();
908-
}
921+
olds.length = 0;
909922
}
910923
if (deferredReleases.size !== 0) {
911924
for (const node of deferredReleases) {

‎packages/signals/tests/store/projection-root-write.bench.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ describe(`one root key per commit, ${KEYS}-key record`, () => {
6363

6464
const [store, set] = createStore<Record<string, { n: number }>>(seed());
6565
void store.k0;
66-
set(d => {
67-
d.k0 = { n: -1 };
68-
});
69-
flush();
7066
setStore = set;
7167
});
68+
// The warm-up write happens outside the root: inside it, the dev tier's
69+
// owned-scope write check throws and the whole file fails to load under
70+
// `SIGNALS_TIER=dev` — leaving CodSpeed to compare a prod run against a
71+
// stale base.
72+
setStore(d => {
73+
d.k0 = { n: -1 };
74+
});
75+
flush();
7276

7377
bench("projection derive: delete + set one ROOT key (#3352)", () => {
7478
for (let c = 0; c < COMMITS; c++) {

0 commit comments

Comments
 (0)