Skip to content

Commit 713a910

Browse files
refactor(signals): copyOwn — one descriptor-preserving copy for the store fold's three identical sites
The fourth site (WK_ALL value-diff against the pre-batch old) stays inline on purpose: its plain-value write is gated on "the draft changed this slot", since an untouched slot in pb holds the pre-batch reference and writing it back would clobber a descendant fold stitched into v. Noted at the site. Size (brotli, macOS): + createStore -5 B, hydrating+stores -35 B; the harness's standing observation holds — repeats compress nearly free, so this is dedup for the next edit more than bytes. Co-authored-by: Cursor <[email protected]>
1 parent ef2b02c commit 713a910

2 files changed

Lines changed: 23 additions & 18 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+
Internal: the store fold's descriptor-preserving property copy is one `copyOwn` helper instead of three inline repeats. No behavior change.

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ function cloneRaw(source: Record<PropertyKey, any>, t?: StoreNextTarget): Record
424424
: Object.create(Object.getPrototypeOf(source), descs);
425425
}
426426

427+
/** Copy own `key` from `from` onto `to`. A plain data slot (enumerable,
428+
* writable, configurable, no accessor) is a bare assignment — the common case
429+
* and the cheap one; anything else goes through defineProperty so accessors
430+
* and attribute flags survive the copy. */
431+
function copyOwn(to: object, from: object, key: PropertyKey): void {
432+
const d = Object.getOwnPropertyDescriptor(from, key)!;
433+
if (d.get || d.set || !d.enumerable || !d.writable || !d.configurable)
434+
Object.defineProperty(to, key, d);
435+
else (to as any)[key] = d.value;
436+
}
437+
427438
/** One-time own-accessor scan (Annex-B probes, no descriptor allocation);
428439
* returns true when the container is plain data (overlay-safe). */
429440
function scanAccessorsOnce(target: StoreNextTarget): boolean {
@@ -448,12 +459,7 @@ export function materializePB(target: StoreNextTarget): void {
448459
if (!target.ovl) return;
449460
const proto = target.pb!;
450461
const clone = cloneRaw(target.v, target);
451-
for (const key of Reflect.ownKeys(proto)) {
452-
const d = Object.getOwnPropertyDescriptor(proto, key)!;
453-
if (d.get || d.set || !d.enumerable || !d.writable || !d.configurable)
454-
Object.defineProperty(clone, key, d);
455-
else (clone as any)[key] = d.value;
456-
}
462+
for (const key of Reflect.ownKeys(proto)) copyOwn(clone, proto, key);
457463
if (target.del !== null) {
458464
for (const key of target.del) delete (clone as any)[key];
459465
target.del = null;
@@ -785,12 +791,7 @@ function drainFolds(): void {
785791
// — the never-mutate-user-data contract holds.
786792
privatizeCommitted(t);
787793
const v = t.v;
788-
for (const key of Reflect.ownKeys(pb)) {
789-
const d = Object.getOwnPropertyDescriptor(pb, key)!;
790-
if (d.get || d.set || !d.enumerable || !d.writable || !d.configurable)
791-
Object.defineProperty(v, key, d);
792-
else (v as any)[key] = d.value;
793-
}
794+
for (const key of Reflect.ownKeys(pb)) copyOwn(v, pb, key);
794795
if (t.del !== null) {
795796
for (const key of t.del) delete (v as any)[key];
796797
t.del = null;
@@ -817,17 +818,16 @@ function drainFolds(): void {
817818
if (wk !== null && wk !== WK_ALL) {
818819
// The trap records every write/delete key — apply exactly those.
819820
for (const key of wk) {
820-
if (hasOwn.call(pb, key)) {
821-
const d = Object.getOwnPropertyDescriptor(pb, key)!;
822-
if (d.get || d.set || !d.enumerable || !d.writable || !d.configurable)
823-
Object.defineProperty(v, key, d);
824-
else (v as any)[key] = d.value;
825-
} else delete (v as any)[key];
821+
if (hasOwn.call(pb, key)) copyOwn(v, pb, key);
822+
else delete (v as any)[key];
826823
}
827824
} else {
828825
// Array length write poisoned the bound (WK_ALL) — value-diff
829826
// against the pre-batch old. Slots the draft never touched hold
830827
// the same raw reference in both, so descendant folds stay put.
828+
// Not copyOwn: the plain-value write is GATED on "the draft changed
829+
// this slot" — an untouched slot in pb holds the pre-batch reference,
830+
// and writing it back would clobber a descendant fold stitched into v.
831831
for (const key of Reflect.ownKeys(pb)) {
832832
const d = Object.getOwnPropertyDescriptor(pb, key)!;
833833
if (d.get || d.set || !d.enumerable || !d.writable || !d.configurable)

0 commit comments

Comments
 (0)