Skip to main content

pico-js design

Each derived node keeps edges to the sources and derived nodes its computation read on its last run. Writing to a source marks its dependents. A later read refreshes only the nodes it needs. For a node with a change callback, pico-js also checks the value at the end of the update so it can notify the caller.

const a = createSourceNode(1);
const b = createSourceNode(2);
const useA = createSourceNode(true);

const sum = createDerivedNode(
(callingNode) => readSource(callingNode, a) + readSource(callingNode, b),
);
const doubled = createDerivedNode(
(callingNode) => readDerived(callingNode, sum) * 2,
);
const chosen = createDerivedNode((callingNode) =>
readSource(callingNode, useA)
? readSource(callingNode, a)
: readSource(callingNode, b),
);

const doubledRetain = retainDerivedNode(doubled);
retainDerivedNode(chosen);
// sum is 3, doubled is 6, and chosen is 1

Each example starts from this state unless it says otherwise.

Invalidate upward​

In this example both retains have no callback, so writeSource(a, 10) marks nodes without running a computation. It marks sum and chosen DIRTY because they read a directly. It marks doubled MAYBE_DIRTY because that node reads sum, whose new value is still unknown. A write to b would mark sum and doubled but leave chosen alone.

Once a write has marked a path, a second write can usually stop at the first already-marked reader. For example, if you write to a and then to b before reading anything, the second write finds sum already DIRTY. The exception involves readers first reached through a node with a callback, described below.

Lazily recompute​

A read of a CLEAN node returns its cached value. Other reads may need to recompute the node or verify its dependencies. The flush uses the same rules when it refreshes nodes with callbacks.

A read of a DIRTY node recomputes it. readDerivedOutsideComputation(chosen) after writeSource(a, 10) runs the computation of chosen, which returns 10 and makes chosen CLEAN.

A read of a MAYBE_DIRTY node first brings its derived dependencies up to date, one at a time in the order the node read them. This check is called verification. It stops at the first derived dependency that changed, and then recomputes the node. A derived dependency changed if its recompute returned a value not === to its previous value. If none changed, the node becomes CLEAN without running. readDerivedOutsideComputation(doubled) after writeSource(a, 10) brings sum up to date first. sum is DIRTY, so it recomputes to 12. Its value changed, so doubled recomputes to 24.

Suppose a is 10 and b is 2. Both sum (12) and doubled (24) are CLEAN. Writing 11 to a and 1 to b marks sum as DIRTY and doubled as MAYBE_DIRTY. Reading doubled recomputes sum, which still returns 12. Verification then marks doubled as CLEAN without running its computation. The unchanged result cuts off propagation.

Nodes with callbacks​

A node with a callback has at least one retain with an onChange function. When marking first reaches such a node and takes it out of CLEAN, pico-js queues it for the flush. A second walk marks its dependents MAYBE_DIRTY so later reads will verify them, but leaves callbacks reached only through that walk off the queue. At the end of the update, the flush refreshes each queued node. If one needs the value of another queued node, the read refreshes that dependency first.

Retain doubled with a callback, retainDerivedNode(doubled, (value) => console.log(value)). writeSource(a, 10) marks sum DIRTY, and when marking reaches doubled it makes doubled MAYBE_DIRTY and puts it on the flush queue. The flush recomputes sum to 12 and doubled to 24, and the callback logs 24.

Now give sum a callback and leave doubled lazy. Writing the original value, writeSource(a, 1), still queues sum because Pico treats every source write as an update. The flush recomputes sum to 3. Its result stayed equal, so reading doubled can reuse the cached 6.

Marking past a node on the flush queue​

Suppose both sum and doubled have callbacks. A write to a marks sum dirty and queues it. A second walk marks doubled MAYBE_DIRTY and sets its MARKED_BEYOND bit. This records that the write reached doubled through a queued callback node. A direct read of doubled will check its input, but the flush can wait for sum's result before deciding whether to queue doubled.

  • If sum changes, markDependentsOfChangedDerivedNode upgrades its direct reader doubled to DIRTY, clears the bit, and queues it for refresh.
  • If sum keeps its value and no other input of doubled changed, the flush can leave doubled alone. Its next read verifies sum and reuses its cache.
  • If another write reaches doubled through a path without a queued callback node, that write clears the bit and queues doubled itself. Its value can change independently of sum.

During refresh, a computation can read a callback node before the flush reaches its queue entry. That read refreshes the node immediately and records any change for notification. When the queue reaches the same node later, it is already CLEAN.

Skipping repeated walks past a node​

Suppose three components independently read the same price band, a derived node with a callback. Their edges appear in the band’s reader list in order R1, R2, R3. A price change within the same band leaves all three readers marked MAYBE_DIRTY, with the boundary at R1. Reading R1 verifies its cached value and makes it CLEAN. Its edge is already at the head, so the boundary advances to R2. The next marking pass checks R1, stops before R2, and moves the boundary back to R1. A further write with no intervening reads can skip all three edges.

A node on the flush queue that keeps its value still costs the walk past it. A projection that keeps its value on most writes and has N dependents with callbacks would have all N dependents visited on every such write, although after the first walk none of them is CLEAN. Each derived node therefore keeps a firstKnownMarkedDependentEdge boundary pointer into its list of dependents, which prepends new and moved edges at firstDependent. Every edge from firstKnownMarkedDependentEdge to the end of the list has a dependent that is not CLEAN. Every derived dependency of a CLEAN node is CLEAN, so no node above those dependents is CLEAN either. When the walk that sets MARKED_BEYOND visits a node, it walks only the prefix of edges before firstKnownMarkedDependentEdge and advances firstKnownMarkedDependentEdge to firstDependent, skipping the node when firstDependent === firstKnownMarkedDependentEdge.

A dependent becomes CLEAN only when its computation returns, after it read the node through readDerived, or when verification makes it CLEAN, after verification visited the dependent's edge to the node. A dependent joins the node's dependents only through readDerived. Every edge whose dependent may become CLEAN is placed strictly before firstKnownMarkedDependentEdge:

  • addEdgeToDerivedDependents prepends every new or moved edge at firstDependent, strictly before firstKnownMarkedDependentEdge.
  • readDerived calls moveEdgeBeforeKnownMarkedDependents on a reused edge when firstKnownMarkedDependentEdge is not undefined, moving the edge to firstDependent and advancing firstKnownMarkedDependentEdge if the edge was the boundary.
  • verifyDerivedDependencies calls moveEdgeBeforeKnownMarkedDependents on each edge it visits when dependency.firstKnownMarkedDependentEdge is not undefined.

removeEdgeFromDerivedDependents advances firstKnownMarkedDependentEdge to edge.nextDependentEdge when unlinking the boundary edge itself, markDependentsOfChangedDerivedNode resets firstKnownMarkedDependentEdge to undefined so dependents recomputing after a value change skip moveEdgeBeforeKnownMarkedDependents, and disposal resets firstKnownMarkedDependentEdge to undefined. A derived node keeps its 12 fields, with firstKnownMarkedDependentEdge alongside firstDependent and no tail pointer.

Because only dependents that became CLEAN since the last beyond walk sit before firstKnownMarkedDependentEdge, if k of N dependents rerun or verify between two writes that leave the node unchanged, the second write walks only those k edges instead of all N. A move before firstKnownMarkedDependentEdge made inside a computation stays in effect until the computation returns, because the walk that advances firstKnownMarkedDependentEdge to firstDependent starts only from a write or a release, and neither runs inside a computation. Correctness proves that the engine gives no stale read with firstKnownMarkedDependentEdge.

When callbacks run​

Callbacks run last, for every node on the flush queue whose value changed, and only after the flush has refreshed every node on the flush queue. They never run at the moment the value of a node on the flush queue changes. Retain doubled with a callback, as in "Nodes with callbacks", and retain sum with the callback () => console.log(readDerivedOutsideComputation(doubled)). writeSource(a, 10) puts sum on the flush queue and marks doubled MAYBE_DIRTY with MARKED_BEYOND, without putting doubled on the flush queue. The flush recomputes sum to 12, and only then marks doubled DIRTY and appends it to the flush queue behind sum. The flush refreshes doubled to 24 before it calls any callback, and the callback of sum then logs 24. By the time the first callback runs, both nodes hold their final values for this update.

Dynamic dependencies​

A computation can take a different branch on each run. After writeSource(useA, false), the next read of chosen replaces its dependency on a with a dependency on b. Writes to b then invalidate chosen. Writes to a leave it alone.

Batches​

A batch stages one value per source. At commit Pico stores each value and marks its readers, then refreshes the affected nodes with callbacks. If sum has a callback, a batch that sets a to 10 and b to 20 recomputes sum once and delivers 30 to its callback. With only a plain retain, sum stays dirty until a read needs its value.

Other documents​

Disposing nodes describes when pico-js disposes a cached node, performance describes the data structures and techniques that make the algorithm fast, and correctness proves that no computation, top-level read, or change callback receives a stale value.