@zakkster/lite-signal 1.6.0-preview.1 → 1.7.0-preview.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -64,6 +64,47 @@ pure-owner node transparently.
64
64
  Exported as a registry method and a top-level helper bound to the default registry,
65
65
  mirroring `createRoot`.
66
66
 
67
+ ### Added -- characterization harnesses (`burst-dag.mjs`, `pull-stress.mjs`)
68
+
69
+ Two root-level standalone harnesses that exercise the new 1.6.0 instrumentation
70
+ surface against the engine's two main hot paths. Both are engine-agnostic
71
+ (`./Signal.js` + public `onGraphMutation` only), both expose a CLI + module
72
+ exports, both stay out of `npm test` and the published tarball.
73
+
74
+ **`burst-dag.mjs`** (`npm run profile:burst`, also `node --expose-gc burst-dag.mjs
75
+ [--width=512 --layers=32 ...]`) reconstructs the published
76
+ `js-reactivity-benchmark` burst shape (sources -> width x layers, fanIn 8, burst
77
+ 16) and characterizes lite-signal's own flush behavior. The standalone path uses
78
+ the new `stats().flushPasses` counter as a cross-check against the opcode-6 pass
79
+ count -- by construction they must match when the hook is attached for exactly
80
+ one burst, which the harness asserts inline. Also exports `burstDagScenario`
81
+ (for plugging into a steady-state allocation gate) and `multiPassProbe` (a
82
+ controlled two-effect write-back that forces a `passes > 1` case so the opcode
83
+ dispatch can be smoke-tested on an engine where multi-pass actually occurs).
84
+
85
+ **`pull-stress.mjs`** (`npm run profile:pull`, also `node --expose-gc
86
+ pull-stress.mjs [--maxDepth=3584 --step=512 ...]`) is the pull-path companion:
87
+ characterizes how the recursive pull scales with chain depth and binary-searches
88
+ the exact overflow point. Three answers per run: (1) the exact deepest chain
89
+ that pulls successfully (the roadmap's "~5,000 chained computeds" pinned to a
90
+ number for the current engine), (2) per-level cold cost across a depth sweep
91
+ (should be roughly linear; superlinear flags a per-node bookkeeping cost
92
+ growing with depth), and (3) cached-read cost (should be O(1) regardless of
93
+ depth via the 1.1.4 `markEpoch` short-circuit). Structure pass at a safe fixed
94
+ depth confirms the opcode triple (0 flush passes, 0 effect runs, recomputes ==
95
+ depth) -- the pull path is doing nothing it shouldn't. Also exports
96
+ `pullStressScenario` for steady-state allocation gates and `probeOverflow`
97
+ for the binary search alone. Both `--kind=callable` and `--kind=box` are
98
+ supported and surface a real engine difference (box pulls overflow earlier
99
+ because `box.get()` adds a prototype-method frame per level, and cached box
100
+ reads are several times more expensive than cached callable reads).
101
+
102
+ Both files import only `./Signal.js`, so the same harness runs against any
103
+ engine build: point `Signal.js` at 1.5.x to compare without the counters
104
+ (`burst-dag` adapts -- only opcode 5 fires on that engine -- and `pull-stress`
105
+ works identically), or at a rejected candidate to re-verify on. The numbers
106
+ they produce are reproducible, comparable, and engine-version-aware.
107
+
67
108
  ### Verified
68
109
 
69
110
  - **Full suite green** against the 1.6.0 engine: 443 tests, 442 pass, 0 fail,
package/README.md CHANGED
@@ -485,6 +485,8 @@ A registry-level (and top-level) debug hook for push-based tooling -- the connec
485
485
 
486
486
  **1.6.0 -- flush-profiling opcodes.** Opcodes `6` (flush pass) and `7` (effect run in pass) extend the surface to cover the flush dimension lite-devtools 1.2 / lite-studio 1.2 read through `watchAllocations`. The matching `stats().flushPasses` counter (the 12th `stats()` key) advances once per drain pass. Both the counter bump and the opcode dispatch sit behind the same `mutationHook !== null` gate, so when no listener is attached they are inert and `flushPasses` is frozen -- the flush loop is byte-identical to 1.5.0.
487
487
 
488
+ **Characterization harnesses (`burst-dag.mjs`, `pull-stress.mjs`).** Two root-level standalone harnesses that exercise the 1.6.0 instrumentation against the engine's two main hot paths. `burst-dag.mjs` (`npm run profile:burst`) reconstructs the layered DAG burst shape (sources -> width x layers, fanIn 8, burst 16) and uses opcodes 5/6/7 to characterize a single steady-state burst: flush passes, recomputes per node, effects enqueued vs short-circuited. Exports `burstDagScenario` for a steady-state allocation gate, and `multiPassProbe` for a controlled two-effect write-back that forces a `passes > 1` case so the opcode dispatch can be smoke-tested where multi-pass actually occurs. `pull-stress.mjs` (`npm run profile:pull`) is the pull-path companion: binary-searches the exact recursive-pull depth limit (the ~5,000-computed bound named in the roadmap, pinned to a number for the current engine), sweeps cold cost per level across depths to flag any superlinear bookkeeping, and confirms cached reads are O(1) regardless of depth (the 1.1.4 `markEpoch` short-circuit holds). Exports `pullStressScenario` for steady-state gates and `probeOverflow` for the bisection alone; supports both `--kind=callable` and `--kind=box`, which surface a real engine difference (box pulls overflow earlier because `box.get()` adds a prototype-method frame per level). Both import only `./Signal.js` + the public `onGraphMutation` hook, so the same files run against any engine build (point `Signal.js` at 1.5.x to compare without the counters). Neither is part of `npm test` or the published tarball -- they ship in source for reproducibility.
489
+
488
490
  ### onCleanup
489
491
 
490
492
  ```ts
package/Signal.js CHANGED
@@ -1,5 +1,33 @@
1
1
  /**
2
- * @zakkster/lite-signal v1.6.0
2
+ * @zakkster/lite-signal v1.7.0
3
+ * --------------------
4
+ * 1.7.0 -- flushStrategy: registry-level effect-delivery policy.
5
+ *
6
+ * NEW: createRegistry({flushStrategy: "eager" | "sab" | "manual"})
7
+ * - "eager" (DEFAULT, byte-identical to 1.6.0): .set outside batch auto-flushes;
8
+ * batch exit auto-flushes. No behavioral change vs 1.6.0 for any existing user.
9
+ * - "sab" ("stable after batch", matches Andrii Volynets's @volynets/reflex sab
10
+ * semantics): .set outside batch enqueues effects but does NOT auto-flush;
11
+ * batch exit DOES flush. Effects deduplicate via FLAG_SCHEDULED across writes.
12
+ * This is the apples-to-apples comparison mode for the js-reactivity-benchmark
13
+ * update group, where the eager flushEffects empty-path try/finally was the
14
+ * dominant per-write overhead on tests that have no effects to deliver
15
+ * (updateComputations1to1 etc).
16
+ * - "manual": neither .set nor batch exit auto-flush; only explicit r.flush()
17
+ * drains the queue. For hard-real-time loops that need frame-aligned settle.
18
+ *
19
+ * NEW: r.flush() -- explicit flush API (no-op if already flushing or queue empty).
20
+ *
21
+ * Hot-path safety: the strategy is resolved ONCE at registry init by selecting
22
+ * one of two pre-built flush hooks (eagerFlushHook | noopFlush). Each .set/.boxSet
23
+ * closure captures a constant function reference for its lifetime; V8 inlines the
24
+ * monomorphic target. In "eager" mode the inlined body is byte-identical to the
25
+ * 1.6.0 inline check (`if (batchDepth === 0) flushEffects()`), preserving the
26
+ * §0b ledger #6 verdict (per-call closure-var loads are forbidden -- a hoisted
27
+ * function reference + monomorphic inlining is not the same mechanism).
28
+ *
29
+ * Original 1.6.0 header follows.
30
+ *
3
31
  * --------------------
4
32
  * Hybrid Doubly-Linked-List Reactive Graph Engine -- decoupled (Signal1_3) base
5
33
  * with the two 1.1.3 performance fixes ported in:
@@ -219,6 +247,17 @@ export function createRegistry(config) {
219
247
  const maxFlushPasses = (config !== undefined && config.maxFlushPasses !== undefined) ? config.maxFlushPasses : 100;
220
248
  const maxLinkLimit = currentLinkCapacity * 16;
221
249
 
250
+ // 1.7: flushStrategy -- selects when effects auto-deliver.
251
+ // "eager" (default, byte-identical to 1.6.0): .set + batch-exit both auto-flush.
252
+ // "sab" (Reflex-equivalent semantics): only batch-exit auto-flushes.
253
+ // "manual": neither -- only explicit r.flush().
254
+ // Validated here; the hot-path hooks bind to one of two pre-built closures
255
+ // below, near flushEffects.
256
+ const flushStrategy = (config !== undefined && config.flushStrategy !== undefined) ? config.flushStrategy : "eager";
257
+ if (flushStrategy !== "eager" && flushStrategy !== "sab" && flushStrategy !== "manual") {
258
+ throw new Error("flushStrategy must be one of: 'eager', 'sab', 'manual'");
259
+ }
260
+
222
261
  // Lazy pool population (P1): maxNodes / maxLinks are capacity ledgers,
223
262
  // not eager construction counts. Nodes/links are constructed on first
224
263
  // demand and recycled through the free lists thereafter -- the zero-GC
@@ -825,6 +864,40 @@ export function createRegistry(config) {
825
864
  }
826
865
  }
827
866
 
867
+ // 1.7: flushStrategy hot-path lever (REVISION).
868
+ //
869
+ // The first 1.7 implementation captured a function reference (flushAfterWrite)
870
+ // and called it from .set / boxSet. V8 did NOT inline that reference on this
871
+ // workload -- eager-mode .set measured 16-65% slower than 1.6.0 on Andrii's
872
+ // updateComputations* tests because every set paid a function-call indirection.
873
+ //
874
+ // This revision encodes the strategy as a closure-captured BOOLEAN const
875
+ // (FLUSH_ON_IDLE_WRITE / FLUSH_ON_BATCH_EXIT) and inlines the conditional
876
+ // directly into the .set / boxSet / batch bodies. V8 captures the booleans as
877
+ // hidden-class constants and can fold them at JIT time: for "eager", the
878
+ // emitted code is byte-identical to 1.6.0's `if (batchDepth === 0) flushEffects()`.
879
+ // For "sab"/"manual", the `FLUSH_ON_IDLE_WRITE && ...` short-circuits on the
880
+ // const-false; V8 elides the whole branch after one tier-up.
881
+ //
882
+ // This is NOT ledger #6 (per-call closure-var load of a PRIMITIVE that V8
883
+ // can't fold). The literals here are immutable for the registry's lifetime
884
+ // (declared `const`, never reassigned), which gives V8's JIT the constant-
885
+ // folding hook it needs.
886
+ const FLUSH_ON_IDLE_WRITE = (flushStrategy === "eager");
887
+ const FLUSH_ON_BATCH_EXIT = (flushStrategy !== "manual");
888
+
889
+ /**
890
+ * Explicit flush API (1.7). Drains the effect queue if not already flushing.
891
+ * Available in every mode. In "manual" mode this is the ONLY way effects run
892
+ * outside of explicit batch boundaries with non-manual strategy.
893
+ *
894
+ * Safe to call from any state: re-entrant calls are no-ops (`isFlushing`
895
+ * guard inside flushEffects), and an empty queue exits immediately.
896
+ */
897
+ function flush() {
898
+ flushEffects();
899
+ }
900
+
828
901
  /**
829
902
  * Run an effect's compute body, re-tracking dependencies.
830
903
  * Short-circuits if no dependency has bumped its version since last eval.
@@ -1075,25 +1148,55 @@ export function createRegistry(config) {
1075
1148
  // set stays a CLOSURE (byte-identical to 1.2.0): its call path is the hot
1076
1149
  // path, and a closure over `node` beats a shared method's this[NODE_PTR]
1077
1150
  // load. Keeping it a closure also restores detached `const {set}=signal()`.
1078
- read.set = (value) => {
1079
- if (node.gen !== birthGen) return;
1080
- const eq = node.equals;
1081
- if (eq && eq(node.value, value)) return;
1082
- if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1083
- node.preBatchValue = node.value;
1084
- node.preBatchVersion = node.version;
1085
- node.revertEpoch = batchEpoch;
1086
- }
1087
- node.value = value;
1088
- if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1089
- node.version = node.preBatchVersion;
1090
- return;
1151
+ //
1152
+ // 1.7: TWO closures, selected once at signal creation by FLUSH_ON_IDLE_WRITE.
1153
+ // The closure-captured boolean approach (a single .set with
1154
+ // `if (FLUSH_ON_IDLE_WRITE && batchDepth === 0)`) measured +16-30% slower
1155
+ // than 1.6.0 on Andrii's updateComputations* tests -- V8 doesn't
1156
+ // constant-fold closure-captured `const` booleans on this hot path
1157
+ // (they live in a context slot, not a literal in bytecode). Splitting at
1158
+ // build time keeps the eager body byte-identical to 1.6.0 -- V8 JITs each
1159
+ // closure independently, no extra load/branch per .set.
1160
+ read.set = FLUSH_ON_IDLE_WRITE
1161
+ ? (value) => {
1162
+ if (node.gen !== birthGen) return;
1163
+ const eq = node.equals;
1164
+ if (eq && eq(node.value, value)) return;
1165
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1166
+ node.preBatchValue = node.value;
1167
+ node.preBatchVersion = node.version;
1168
+ node.revertEpoch = batchEpoch;
1169
+ }
1170
+ node.value = value;
1171
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1172
+ node.version = node.preBatchVersion;
1173
+ return;
1174
+ }
1175
+ globalVersion = (globalVersion + 1) | 0;
1176
+ node.version = globalVersion;
1177
+ markDownstream(node);
1178
+ if (batchDepth === 0) flushEffects();
1091
1179
  }
1092
- globalVersion = (globalVersion + 1) | 0;
1093
- node.version = globalVersion;
1094
- markDownstream(node);
1095
- if (batchDepth === 0) flushEffects();
1096
- };
1180
+ : (value) => {
1181
+ // sab / manual mode: write + mark only. Effects stay queued (dedup via
1182
+ // FLAG_SCHEDULED) until batch exit (sab) or explicit flush() (manual).
1183
+ if (node.gen !== birthGen) return;
1184
+ const eq = node.equals;
1185
+ if (eq && eq(node.value, value)) return;
1186
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1187
+ node.preBatchValue = node.value;
1188
+ node.preBatchVersion = node.version;
1189
+ node.revertEpoch = batchEpoch;
1190
+ }
1191
+ node.value = value;
1192
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1193
+ node.version = node.preBatchVersion;
1194
+ return;
1195
+ }
1196
+ globalVersion = (globalVersion + 1) | 0;
1197
+ node.version = globalVersion;
1198
+ markDownstream(node);
1199
+ };
1097
1200
  read.update = sharedUpdate; // shared: cold path, calls this.set (the closure above)
1098
1201
  read.subscribe = sharedSubscribe; // shared: cold path, recovers via `this`
1099
1202
 
@@ -1179,26 +1282,51 @@ export function createRegistry(config) {
1179
1282
  }
1180
1283
  return node.value;
1181
1284
  }
1182
- function boxSet(value) {
1183
- const node = this[NODE_PTR];
1184
- if (node.gen !== this[NODE_GEN]) return;
1185
- const eq = node.equals;
1186
- if (eq && eq(node.value, value)) return;
1187
- if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1188
- node.preBatchValue = node.value;
1189
- node.preBatchVersion = node.version;
1190
- node.revertEpoch = batchEpoch;
1191
- }
1192
- node.value = value;
1193
- if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1194
- node.version = node.preBatchVersion;
1195
- return;
1285
+ // 1.7: boxSet also splits into two builds, selected once at registry init.
1286
+ // Same rationale as the .set closure split (V8 doesn't fold closure-captured
1287
+ // booleans here either). `boxSet` is the method assigned to every signalBox,
1288
+ // so binding it once at registry creation gives every box a monomorphic
1289
+ // method dispatch and a body byte-identical to 1.6.0 in eager mode.
1290
+ const boxSet = FLUSH_ON_IDLE_WRITE
1291
+ ? function boxSet(value) {
1292
+ const node = this[NODE_PTR];
1293
+ if (node.gen !== this[NODE_GEN]) return;
1294
+ const eq = node.equals;
1295
+ if (eq && eq(node.value, value)) return;
1296
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1297
+ node.preBatchValue = node.value;
1298
+ node.preBatchVersion = node.version;
1299
+ node.revertEpoch = batchEpoch;
1300
+ }
1301
+ node.value = value;
1302
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1303
+ node.version = node.preBatchVersion;
1304
+ return;
1305
+ }
1306
+ globalVersion = (globalVersion + 1) | 0;
1307
+ node.version = globalVersion;
1308
+ markDownstream(node);
1309
+ if (batchDepth === 0) flushEffects();
1196
1310
  }
1197
- globalVersion = (globalVersion + 1) | 0;
1198
- node.version = globalVersion;
1199
- markDownstream(node);
1200
- if (batchDepth === 0) flushEffects();
1201
- }
1311
+ : function boxSet(value) {
1312
+ const node = this[NODE_PTR];
1313
+ if (node.gen !== this[NODE_GEN]) return;
1314
+ const eq = node.equals;
1315
+ if (eq && eq(node.value, value)) return;
1316
+ if (batchDepth > 0 && node.revertEpoch !== batchEpoch) {
1317
+ node.preBatchValue = node.value;
1318
+ node.preBatchVersion = node.version;
1319
+ node.revertEpoch = batchEpoch;
1320
+ }
1321
+ node.value = value;
1322
+ if (batchDepth > 0 && node.revertEpoch === batchEpoch && eq && eq(node.preBatchValue, value)) {
1323
+ node.version = node.preBatchVersion;
1324
+ return;
1325
+ }
1326
+ globalVersion = (globalVersion + 1) | 0;
1327
+ node.version = globalVersion;
1328
+ markDownstream(node);
1329
+ };
1202
1330
  function boxPeek() {
1203
1331
  const node = this[NODE_PTR];
1204
1332
  if (node.gen !== this[NODE_GEN]) return undefined;
@@ -1412,7 +1540,7 @@ export function createRegistry(config) {
1412
1540
  return fn();
1413
1541
  } finally {
1414
1542
  batchDepth = (batchDepth - 1) | 0;
1415
- if (batchDepth === 0) flushEffects();
1543
+ if (FLUSH_ON_BATCH_EXIT && batchDepth === 0) flushEffects();
1416
1544
  }
1417
1545
  }
1418
1546
 
@@ -1770,7 +1898,7 @@ export function createRegistry(config) {
1770
1898
  while (l !== null) { const nx = l.nextDep; fn(describeNode(l.source)); l = nx; }
1771
1899
  }
1772
1900
 
1773
- return {signal, computed, effect, signalBox, computedBox, dispose, batch, untrack, createRoot, createScope, onCleanup, stats, destroy, isTracking, hasObservers, observeObservers, forEachObserver, forEachSource, forEachOwned, ownerOf, nodeId, describe, onGraphMutation};
1901
+ return {signal, computed, effect, signalBox, computedBox, dispose, batch, flush, untrack, createRoot, createScope, onCleanup, stats, destroy, isTracking, hasObservers, observeObservers, forEachObserver, forEachSource, forEachOwned, ownerOf, nodeId, describe, onGraphMutation};
1774
1902
  }
1775
1903
 
1776
1904
  // -----------------------------------------------------------------
@@ -1811,6 +1939,10 @@ export function batch(fn) {
1811
1939
  return defaultRegistry.batch(fn);
1812
1940
  }
1813
1941
 
1942
+ export function flush() {
1943
+ return defaultRegistry.flush();
1944
+ }
1945
+
1814
1946
  export function untrack(fn) {
1815
1947
  return defaultRegistry.untrack(fn);
1816
1948
  }
package/llms.txt CHANGED
@@ -87,6 +87,16 @@ triggering write -- no `queueMicrotask`, no promises, no scheduler ticks.
87
87
 
88
88
  **The callable API and hot paths are unchanged**; this is purely additive
89
89
  instrumentation and a second ownership primitive. Drop-in over 1.5.0.
90
+ Two root-level characterization harnesses ship in source (not in the
91
+ tarball): **`burst-dag.mjs`** (`npm run profile:burst`) reconstructs the
92
+ layered DAG burst shape and uses opcodes 5/6/7 + `stats().flushPasses` to
93
+ report flush passes, max recomputes per node, and per-burst us; also exports
94
+ `burstDagScenario` + `multiPassProbe`. **`pull-stress.mjs`** (`npm run
95
+ profile:pull`) is the pull-path companion: binary-searches the recursive-pull
96
+ depth limit (~5,000 named in the roadmap, pinned to a number per engine),
97
+ sweeps cold cost per level, confirms cached reads are O(1) via markEpoch;
98
+ exports `pullStressScenario` + `probeOverflow`. Both import only
99
+ `./Signal.js` + public `onGraphMutation`, run against any engine build.
90
100
  **Tests:** 443 total, 442 pass, 0 fail, 1 skip (only the architecturally-N/A
91
101
  SSR case in `17-reactivity`); +4 `createScope` tests in `test/28-scope_test.mjs`
92
102
  and the 1.4.0 pool tests carry the 11-key shape pin forward to the new 12-key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zakkster/lite-signal",
3
- "version": "1.6.0-preview.1",
3
+ "version": "1.7.0-preview.0",
4
4
  "description": "Zero-GC reactive graph. Monomorphic object pool, versioned push-pull propagation, 32-bit modular versioning. Built for hot paths and long-running processes.",
5
5
  "author": "Zahary Shinikchiev <shinikchiev@yahoo.com>",
6
6
  "license": "MIT",
@@ -31,6 +31,8 @@
31
31
  "test:coverage": "c8 node --test --test-reporter=spec",
32
32
  "bench": "node --expose-gc bench/benchmark.mjs",
33
33
  "bench-reactive": "node --expose-gc bench/benchmarkReactive.mjs",
34
+ "profile:burst": "node --expose-gc burst-dag.mjs",
35
+ "profile:pull": "node --expose-gc pull-stress.mjs",
34
36
  "verify": "npm test && npm run bench"
35
37
  },
36
38
  "keywords": [