Incremental calculation engine · Rust → WebAssembly

Change one input. Only what depends on it is recalculated.

This is watershed, running in your browser: the same Rust engine its tests check, compiled to WebAssembly. Move a slider and watch the change flow up the graph. Where a recalculated value comes out the same as before, the update stops.

Try a model

Inputs

Starting the engine…

input changed recalculated, new value recalculated, same value: the update stopped here not touched

Updates

  • Move a slider to start an update.

Run it as scenarios

The same formulas, compiled to a plan and run for many values of one input. The graph above keeps the values it has.

Nothing run yet.

A browser has no CUDA, so the plan runs here on one thread. Natively the same plan runs on every core or on a GPU: cargo run --release -p watershed-gpu --example loans.

Why does this node have its value?


          

Click any node in the graph to explain it.

The Rust code that builds this graph

        

How to read the live crypto book

The crypto book is a small trading portfolio: eight coins held across three desks. Its prices come from Coinbase as trades happen, many times a second. Each price is an input at the bottom of the graph, and everything above it is calculated from those prices.

  • pricesbtc eth sol avax link ada xrp doge, in US dollars, live from Coinbase. risk_limit is yours to set.
  • desksWhat each desk's holdings are worth. Majors holds BTC and ETH; alts holds SOL, AVAX, LINK and ADA; payments holds XRP and DOGE.
  • book_value, pnlThe three desks added up, and profit or loss against the $400,000 the book cost.
  • majors_shareHow much of the book sits in BTC and ETH.
  • within_limit, concentratedTwo yes-or-no checks: is the book under the risk limit, and is more than 60% of it in BTC and ETH?
  • statusok, too concentrated or over limit.

What happens on each price tick

  1. Coinbase reports a trade, say SOL at $88.31. The sol input turns green.
  2. The engine recalculates only what depends on SOL: the alts desk, then the book value, P&L and majors share. They turn orange. The majors and payments desks don't use SOL, so they are left alone.
  3. Next come the two checks. Usually a small price move doesn't change them: the book is still under the limit, and still more than 60% in BTC and ETH. They are recalculated, come out the same, and show as dashed.
  4. Because neither check changed, status is not recalculated at all. The update stops as soon as nothing above it can change. The Updates panel records which coin moved and where each update stopped.

The numbers in the live bar

Ticks are prices received from Coinbase. Updates are runs of the engine: prices that arrive within the same screen refresh are applied together in one update, so there are fewer updates than ticks. Engine time is how long the engine itself took per update, typically tens of microseconds in a browser.

Things to try

  • Drag risk_limit below the book value. within_limit turns false and the status changes to "over limit". Drag it back up.
  • Click any node to see why it has its value, traced back to the prices it came from.
  • Pause the feed and move the prices yourself.

Why this matters

This book has only 18 nodes, so recalculating everything on every tick would be instant too. Real books have thousands of positions and risk measures on top of them, where recalculating everything for every tick is too slow. The rule is the same at any size: recalculate what depends on the change, and stop where nothing changes. The benchmark below builds a graph of up to a million nodes in your browser and compares the two approaches.

A big graph, in your browser

Market prices feed trades, and trades add up through a tree of totals to one figure. Build it here, then compare changing a few prices with recalculating every node.

Size

Nothing built yet.

Results appear here: the time for each kind of update, measured in this browser.

Live: every Wikipedia edit, as it happens

Not everything is prices. Wikimedia publishes every edit to every wiki as it is saved, a few dozen a second. Here each edit becomes a row in a watershed collection, which keeps totals over its rows and a total per wiki. The table holds the latest 5,000 edits: each new edit adds a row and pushes out the oldest, and only the totals those two rows belong to are recalculated.

Starts when you scroll here

–

Edits stream from stream.wikimedia.org. Nothing is sent back.

Edits in the window
–
Made by bots
–
Bytes added
–
Bytes removed
–

Each update's work appears here.

Busiest wikis right now

WikiEditsBytes
Waiting for edits.
Why does a wiki have this count?
Pick a wiki in the table.

What the engine holds

Each edit is a row with three fields: the wiki, the change in size in bytes, and whether a bot made it. Each row also has calculated columns: is_bot, grew (bytes added, or 0) and shrank (bytes removed, or 0). Over the rows sit totals (the count, bot edits, bytes added and removed, the bot share) and, for each wiki, its own count and byte total.

What one edit costs

A new edit for English Wikipedia changes the overall totals and English Wikipedia's totals. The edit it pushes out changes the overall totals and its own wiki's. Every other wiki's totals are left alone. Each total updates only its path through a tree of partial sums, so the work stays small however many rows the window holds, and the totals are the same, to the last bit, as adding the rows up from scratch.