Skip to content

Experimental next/ implementation: re-run only when a read value changed - #130

Closed
johnsoncodehk wants to merge 2 commits into
masterfrom
cursor/near-optimal-signal-redesign-6a0e
Closed

johnsoncodehk wants to merge 2 commits into
masterfrom
cursor/near-optimal-signal-redesign-6a0e

Conversation

@johnsoncodehk

Copy link
Copy Markdown
Collaborator

Adds next/index.ts, a from-scratch implementation with the same public API as src/index.ts, plus tests. Nothing in src/ changes and nothing in next/ is built or published.

Design

One rule: a getter or effect is called again only when a value it read during its last call is different now.

  • Each link stores the value read through it. A node that may be stale compares those values in read order and stops at the first difference.
  • A computed with no subscribers keeps its value and its recorded reads, but its dependencies hold no reference to it. It can be garbage collected, and a later read validates by comparison instead of calling the getter.
  • Effects never run inside a write made by another effect or getter. An effect that changes something it read is re-run until it settles.
  • A getter that throws is not called again until a dependency changes; readers get the same error. An effect that throws does not stop the other effects in the flush.
  • Cycles throw Cycle detected (computeds that read themselves or each other, effects that never settle).

Results against master

Correctness

  • The existing effect, effectScope, trigger and conformance suites pass unchanged against next (200 tests).
  • Differential fuzz against evaluation from scratch: 0 failures in 30,000 graphs, with and without effects that write signals.
  • Chaos fuzz (throwing getters, effects and cleanups, disposal during runs, nesting, trigger): 0 failures in 300,000 cases.

Getter calls (20,000 random programs)

getter calls
master 245,664
next 231,818
last-run reference model 231,818
unbounded memoisation (floor) 180,459

next matches the last-run model on every single case, not only in total. Effect runs are identical everywhere (292,687).

Steady-state instructions per iteration (node --jitless under cachegrind, slope between 300 and 600 iterations)

workload master next change
grid (10 chains of 10 computeds, 10 effects) 616,134 547,040 -11.2%
wide (1 signal, 100 effects) 580,584 532,330 -8.3%
read (cached reads) 126,102 111,481 -11.6%
unwatched chain 67,874 61,414 -9.5%
create and dispose 37,204 36,848 -1.0%
diamond 100,252 91,546 -8.7%

Wall-clock with the JIT agrees in direction: -1.5% to -12% on these workloads, -28% on a batch that leaves values unchanged.

Where next is worse

  • Reading an unwatched computed chain after a write to an unrelated signal: about 9 ns on master, about 81 ns on next for a 10-deep chain. next walks the chain comparing values (no getter calls); master answers in O(1) because the chain stays subscribed. This is the cost of letting unwatched computeds be collected.
  • Size: 7,125 B minified / 2,617 B gzip, against 5,353 / 1,887.
  • Memory per 10,000 nodes: computed +3%, effect +5%, tree +4% (signal -7%).

Garbage collection: of 1,000 unwatched computeds that were read once and dropped, master collects 0 while the source signal lives; next collects all 1,000.

Behaviour differences from src

next is not a drop-in replacement. next/tests/behavior.spec.ts pins each of these:

  • An effect that writes one of its own dependencies re-runs until it settles.
  • Effects triggered by a write inside an effect run after that effect finishes, not nested inside it.
  • A computed keeps its value after losing its last subscriber.
  • Cycles throw instead of hanging or silently going stale.
  • Errors from getters are cached until a dependency changes.
  • A flush continues past an effect that throws and rethrows the first error at the end.
  • An effect disposed during its own run gets the cleanup it returned called.

Testing

pnpm run test:next runs the existing suites against next/index.ts together with next/tests/ (229 tests). pnpm run build, pnpm run check and pnpm run test pass.

pnpm run check only covers src; next/index.ts was linted separately with the same tsslint rules and is clean.

The instruction-count, wall-clock, memory and GC measurements were taken with scripts that are not part of this branch.

Open in Web Open in Cursor 

cursoragent and others added 2 commits November 12, 2026 08:29
next/index.ts exposes the same API as src/index.ts. Links remember the
value read through them, and a node that may be out of date compares
those values in read order instead of relying on dirty marks alone.
Computeds without subscribers keep their value without being referenced
by their dependencies.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
test:next runs the suites written for src against next/index.ts, plus:
- behavior: where next intentionally differs from src
- bound: getter and effect call counts equal a last-run reference model
- fuzz: random graphs against evaluation from scratch
- chaos: throwing getters, effects and cleanups, disposal during runs

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants