Skip to content

Commit 22c3d3e

Browse files
ryansolidclaudecursoragent
committed
perf(signals): record a pre-batch backing only for adoptions — fresh one-key stores 2.1 → 1.27 ms per 2000 (next 1.13)
CodSpeed flagged 'fresh stores: create + first write + first commit' −13%; on the built dist it was 2×: every fresh draft recorded a weak-map entry for a backing that never moves until the fold. Adoptions keep the weak map (written in place for reused containers — dbmon's steady state is unchanged); drafts record nothing; a mid-batch privatization or a draft over an adopted raw is listed in a small per-batch map (preBatch). Signals suite green, web green. Co-authored-by: Claude <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 5001e7b commit 22c3d3e

2 files changed

Lines changed: 47 additions & 14 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+
Size audit (measurement branch): the fold queue records a pre-batch backing only for adoptions (whose backing swaps eagerly), in a weak map written in place batch after batch; a draft's pre-batch backing is its own committed backing, so nothing is recorded for it — 2000 fresh one-key stores per flush had cost 2000 weak-map insertions and ran at 2× `next` (CodSpeed's `fresh stores` bench). The two rare moves of a draft's backing mid-batch (a privatization, a draft over an adopted raw) are kept in small per-batch maps.

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ export function getContainerNode(target: StoreTarget): Signal<any> {
479479
// pending backing staged — so whoever asked for it reads the frame it
480480
// would have read had the node been there from the write.
481481
if (target.pb !== null && !directCommit) {
482-
k._value = foldOlds.get(target) ?? target.v;
482+
k._value = preBatch(target);
483483
k._pendingValue = target.pb;
484484
queuePendingNode(k);
485485
}
@@ -720,11 +720,12 @@ 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);
723724
const k = target.k;
724725
if (k !== null && !(k._config & CONFIG_HELD)) k._pendingValue = pb;
725726
} else if (pb === null) {
726727
const v = target.v;
727-
if (!directCommit) queueFold(target);
728+
if (!directCommit) queueFold(target, false);
728729
if (
729730
!target.ch &&
730731
!Array.isArray(v) &&
@@ -746,6 +747,10 @@ function privatizeCommitted(target: StoreTarget): void {
746747
if (isOwned(target.v)) return;
747748
const before = target.v;
748749
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);
749754
target.v = clone;
750755
target.ch = false;
751756
if (target.u) {
@@ -804,7 +809,7 @@ export function adoptPB(
804809
incoming: Record<PropertyKey, any>,
805810
notify = true
806811
): void {
807-
if (!directCommit) queueFold(target);
812+
if (!directCommit) queueFold(target, true);
808813
let base: Record<PropertyKey, any>;
809814
if (target.pb !== null) {
810815
if (target.ovl) materializePB(target);
@@ -841,24 +846,43 @@ export function adoptPB(
841846

842847
/** The fold queue: the targets with a batch open (`pb !== null` IS the
843848
* membership — a target is queued exactly while it carries a staging; the
844-
* drain skips one already folded), in two reusable arrays, and each
845-
* target's pre-batch committed backing in a weak map whose entry is
846-
* written in place batch after batch. Nothing here allocates per batch
847-
* once warm: a dbmon tick queues ~7000 containers, and a map built and
848-
* drained per tick cost ~540 KB of table allocation — most of the tick's
849-
* young-generation GC, and the whole of its gap to `next` (plan sec. 41.5). */
849+
* drain skips one already folded), in two reusable arrays. Nothing here
850+
* allocates per batch once warm: a dbmon tick queues ~7000 containers, and
851+
* a map built and drained per tick cost ~540 KB of table allocation — most
852+
* of the tick's young-generation GC, and the whole of its gap to `next`
853+
* (plan sec. 41.5).
854+
*
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. */
850864
let foldList: StoreTarget[] = [];
851865
let foldSpare: StoreTarget[] = [];
852-
const foldOlds = new WeakMap<StoreTarget, Record<PropertyKey, any>>();
866+
const adoptOlds = new WeakMap<StoreTarget, Record<PropertyKey, any>>();
867+
const privatizedOlds = new Map<StoreTarget, Record<PropertyKey, any>>();
868+
const draftedAdoptions = new Set<StoreTarget>();
853869

854870
/** Before the staging is set and before an adoption swaps the backing. */
855-
function queueFold(target: StoreTarget): void {
871+
function queueFold(target: StoreTarget, adoption: boolean): void {
856872
if (target.pb !== null) return;
857873
schedule();
858-
foldOlds.set(target, target.v);
874+
if (adoption) adoptOlds.set(target, target.v);
859875
foldList.push(target);
860876
}
861877

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`. */
881+
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;
884+
}
885+
862886
/** The flush committed its pending nodes: fold every pending backing whose
863887
* container node committed (its staging is gone — `_value` is the backing
864888
* now) into the committed home; one a later step holds (its node still
@@ -875,9 +899,13 @@ function drainFolds(): void {
875899
const k = t.k;
876900
if (k !== null && k._pendingValue !== NOT_PENDING)
877901
foldList.push(t); // held: commits with its hold
878-
else foldTarget(t, foldOlds.get(t)!);
902+
else foldTarget(t, preBatch(t));
879903
}
880904
list.length = 0;
905+
if (foldList.length === 0) {
906+
if (privatizedOlds.size !== 0) privatizedOlds.clear();
907+
if (draftedAdoptions.size !== 0) draftedAdoptions.clear();
908+
}
881909
}
882910
if (deferredReleases.size !== 0) {
883911
for (const node of deferredReleases) {
@@ -1418,7 +1446,7 @@ export function committed(t: StoreTarget): Record<PropertyKey, any> {
14181446
const k = t.k;
14191447
if (k !== null) return k._pendingValue !== NOT_PENDING ? asBacking(k._value) : t.v;
14201448
// A node-less staging (`stageOn`): the fold queue remembers the frame.
1421-
return t.pb !== null ? (foldOlds.get(t) ?? t.v) : t.v;
1449+
return t.pb !== null ? preBatch(t) : t.v;
14221450
}
14231451

14241452
/** The pass that sees this flush's staging (signal parity — `read()`'s

0 commit comments

Comments
 (0)