DEV Community

gxqwb
gxqwb

Posted on

Incremental Computation vs Batch Recompute: What Changing One Input Really Costs

Most programs are written as if inputs never change. A function takes its arguments, computes a result, and forgets everything that happened during the call. That model is clean, testable, and deeply at odds with interactive software, where a user edits one cell or one line and expects the rest of the world to catch up immediately.

Batch recompute pays for the whole problem after every change, while incremental computation pays for the change itself plus the bookkeeping needed to know what the change touched.

The simplicity of starting over

A batch computation has a strong virtue: its correctness does not depend on history. Given the same inputs, it produces the same outputs, regardless of what happened before. There is no cache to invalidate, no dirty flag to forget, and no dependency edge that might have gone stale. Parallelism is straightforward because each output can be computed independently, and a failure can be recovered by simply running again.

The cost is that the work does not shrink when the change does. A compiler that rebuilds every module after a one-character edit repeats analysis of thousands of files that could not possibly have changed. A dashboard that recomputes every chart when a single filter moves repeats queries whose results are identical. For small problems the expense is invisible; for large ones it becomes the thing users notice, the pause between an action and a response.

Optimizers often reach for caching first. Memoizing a pure function avoids recomputation when the exact same arguments reappear, which helps when requests repeat but does nothing when one input changes slightly. The cache hits are gone precisely in the situations that matter most in interactive work.

How change propagates

An incremental system keeps a record of the computation as a graph. During evaluation, each node notes which other nodes it read; when an input changes, those notes become the edges along which invalidation travels. A node whose dependencies are unchanged stays clean and returns its stored value. A node whose dependency changed is marked dirty and re-evaluated, and if its new value equals the old one, propagation can stop there.

Tracking reads is the subtle part. Dependencies that exist on one execution but not another must be recorded dynamically, or the system will keep recomputing a result after its true dependency has vanished. Systems such as Salsa in Rust and self-adjusting computation research solve this by capturing read sets per run and diffing them against the previous run. Spreadsheets do a simplified version with a static formula graph, and build systems do it with declared inputs and outputs rather than observed reads, which is more predictable but requires developers to list dependencies honestly. The same split appears in editor tooling: a language server that records which source ranges a query inspected can recheck one function after a keystroke, while one that only watches whole files rebuilds far more of the analysis than the edit justified.

Granularity decides whether the bookkeeping pays for itself. Track too coarsely and a small edit dirties half the graph, so you have paid for an incremental system and still done batch work. Track too finely and the cost of maintaining edges and version stamps exceeds the arithmetic you saved. There is a middle ground where a node represents a meaningful unit of work, and finding it is more of an engineering judgment than a theorem.

A spreadsheet with a million formulas recalculated on every keystroke spends about one second per edit at one microsecond per cell, while touching fifty dependent cells reduces that to fifty microseconds; ten thousand users making twenty edits a minute then saves more than three thousand hours of CPU each day.

An everyday analogy: the kitchen and the onion

A restaurant kitchen that practiced batch recompute would handle a diner's request for no onions by throwing away the finished dish, washing the pan, and cooking the entire meal again. The result is correct, the process is easy to explain to a new cook, and it is absurdly wasteful when there were twelve other orders waiting.

A good cook does the incremental thing: remove the onions from the component that has not yet been plated, adjust the sauce, taste once, and serve. The cook can do this because they know exactly which part of the dish contains the onion, which is the same as knowing which node in a graph depends on a changed input. If the kitchen had no record of which sauce went with which plate, the careful adjustment would be impossible, and starting over would be the only safe choice.

When batch still wins

Incremental systems carry costs that are easy to hide in a diagram. They hold onto graphs, stored values, and version stamps, so memory grows with the number of tracked nodes. Debugging changes character: a wrong answer might be a stale edge rather than an error in the computation itself. A dependency that was missed because a read went through an untracked path produces a result that looks plausible and is wrong, which is the most expensive kind of bug.

Batch recompute is the right choice when changes are large relative to the problem, when the computation is fast, or when correctness must be obvious to a reader who will never inspect the dependency graph. One-off data pipelines, nightly reports, and scientific runs whose inputs are replaced wholesale all fit this description. Rewriting them as incremental systems adds machinery without removing meaningful work.

The two approaches are also easy to combine. A system can recompute a whole subsystem from scratch while propagating changes incrementally between subsystems, which is how many build tools handle generated code. The boundary is placed where changes are rare rather than where the graph is largest. A useful diagnostic is to measure the fan-out of a typical edit: if changing one input dirties less than a tenth of the graph, incremental machinery will usually pay for itself; if it dirties half, the team should first look at the shape of the computation rather than at a faster cache.

The decision comes down to the ratio between a typical change and the whole problem, tempered by how much you trust the system to notice every dependency it should have noticed.

Incremental computation is not a faster version of batch computation; it is a different bet, trading a larger memory footprint and a harder debugging story for the ability to answer a small question without redoing a large one. When edits are rare and consequences are large, starting over remains the wisest course.

SOL: HCqPmPJM3NVtS6YrmVaTZYnuP8XZ2PFgQZKT94VK1LkS
Creating this takes real effort — thank you for the support.

Top comments (0)