From 67481af2c2772b7d41e7e2b333cc5b1d09d17794 Mon Sep 17 00:00:00 2001 From: Valera Date: Sat, 8 Aug 2026 21:19:52 +0000 Subject: [PATCH] tape/ a quiet node was found by walking the whole tape for it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A frame carries every node's standing value, and `held` searched back from the cursor for the last fire of each node that was quiet. A node that fires twice a day — spl's market cap — or not at all that day — its classification — made that a walk over the entire retained tape, once per frame. Measured on spl's 42-node graph holding a day: 139ms to land a cursor at tick 159 000, 185ms for `n`, and linear in wherever the cursor sat. `fired` indexes it: per node, ascending, the positions it fired on. Positions and not `abs` ids because between thinning passes `ticks` only grows at the back, so a recorded position cannot move — and `thin`, the one thing that does move them, rebuilds the index in the walk it was already making. Derived, so it stays out of `TapeFile` and `load` rebuilds it. Measured, same machine, same day, spl at capacity 250 000: seek/step_until/step_until_change 0.8-139ms -> flat 0.9ms tape thread (viz_cost tape_s) 1.107s -> 1.109s, medians of 3 RSS 391MB -> 398MB graph thread untouched The sweep's synthetic mix gains a twice-a-day node, without which none of this was visible: its rarest node fired hourly, so the published latency was one nobody waits for. The reactivity chart now shows the raw round trip rather than the round trip minus the loopback hop, for the same reason. Co-Authored-By: Claude Opus 5 (1M context) --- README.md | 65 +++++++++++++++++++---------------- docs/.readme_assets/other.md | 65 +++++++++++++++++++---------------- docs/capacity.txt | 38 ++++++++++---------- exec_viz/examples/capacity.rs | 22 +++++++++--- exec_viz/src/tape.rs | 49 +++++++++++++++++++++++--- 5 files changed, 153 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 2c1443e..296d2e8 100644 --- a/README.md +++ b/README.md @@ -88,15 +88,14 @@ An op can name a tick that has not been recorded yet. The server says so (`pendi ### Picking a `capacity` -`capacity` is the only number `Viz::new` asks for that has no right answer — it is RAM traded -against reactivity, and the exchange rate is your graph's, not this crate's. What it constrains: +`capacity` is the only number `Tape::new` asks for that has no right answer — it buys scrollback with +RAM, and the exchange rate is your graph's, not this crate's. What it constrains: | | | |---|---| | `capacity / 2` | the newest ticks kept **whole** — the stretch that stays tick-exact | | `capacity / 4` | the ceiling on the thinned *backbone*, and so how deep the squeeze goes | | `capacity / 2` | the recycle channel's depth: one pass frees between a quarter and a half of the buffer, and the recorder takes all of it back rather than allocating | -| `capacity` | the bound on the carry-forward scan a frame does for every quiet node, which is what landing a cursor costs | Thinning is **per node, not per tick**: a pass raises the exponent of whichever node is claiming most of the backbone until the backbone fits, so a node firing 288 times a day keeps all 288 while the @@ -108,40 +107,48 @@ that each want ~3 000 land on ~1 790 apiece, and everything rarer than that keep had. Read the other way: a 5-minute bar over three days is 864 fires and is safe from about 8 000; over a month it is 8 640 and wants ~65 000. -Bigger is not free, and the price is not where it looks. Recording cost does not move with -`capacity` at all — the graph thread's leg of a fire is the same two renderings either way. What -moves is the cost of *landing* a cursor, because a frame carries every node's standing value and a -node quiet for a while is found by scanning back for it. +**The price is memory, and only memory.** Recording cost does not move with `capacity` — the graph +thread's leg of a fire is the same two renderings either way — and neither does cursor latency: every +replay op is a binary search, so a keypress costs the same against 260 000 retained ticks as against +2 000. In the block below the whole latency column is the loopback socket; the tape's own share does +not clear the noise. -The block below is one synthetic graph on one machine. `Viz::bytes` is exported so the same reading -can be taken against yours. +That is worth stating because it was not always true. A frame carries every node's standing value, +and a node that has been quiet is found by searching back for its last fire — which used to be a +linear scan. On a 42-node graph holding a day, one node that fires twice a day was enough to put a +scrub at 139ms, growing with wherever the cursor sat. `Inner::fired` indexes it, and the synthetic +mix below now carries a twice-a-day node so the number here cannot quietly go back to being one +nobody waits for. + +The block is one synthetic graph on one machine. `Viz::bytes` is exported so the same reading can be +taken against yours. ``` -318420 ticks (2 days at spl's 159210/day), 5 nodes, 107 B/tick of card faces -fires over the run: Book=318420 Screen=3185 Bar:1m=2895 Bar:5m=579 Bar:1h=49 +318420 ticks (2 days at spl's 159210/day), 6 nodes, 107 B/tick of card faces +fires over the run: Book=318420 Screen=3185 Bar:1m=2895 Bar:5m=579 Bar:1h=49 Cap=4 footprint — Viz::bytes over the retained ticks - 2048 │▬ 3.0 MB (1477 B/tick) - 8192 │▬▬ 5.4 MB (818 B/tick) - 20000 │▬▬▬▬ 11.3 MB (665 B/tick) - 65536 │▬▬▬▬▬▬▬▬▬▬▬ 33.8 MB (554 B/tick) - 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 132.6 MB (524 B/tick) - -reactivity — /api/seek mid-tape *above* a bare 53µs loopback hop, median of 64 - 2048 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 8192 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 20000 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 65536 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 14µs - 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 34µs + 2048 │▬ 1.2 MB (691 B/tick) + 8192 │▬ 4.0 MB (637 B/tick) + 20000 │▬▬▬▬ 9.8 MB (594 B/tick) + 65536 │▬▬▬▬▬▬▬▬▬▬▬▬ 34.1 MB (558 B/tick) + 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 121.0 MB (548 B/tick) + +reactivity — one /api/seek mid-tape, median of 64; the bare loopback hop under it is 53µs + 2048 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 8192 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 68µs + 20000 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 65536 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 68µs addressability — fires still reachable by `step_until`, against the run's own - capacity retained Book Screen Bar:1m Bar:5m Bar:1h - 2048 2007 2007/318420 251/3185 231/2895 147/579 49/49 - 8192 6557 6557/318420 691/3185 899/2895 579/579 49/49 - 20000 16951 16951/318420 1796/3185 1784/2895 579/579 49/49 - 65536 61047 61047/318420 3185/3185 2895/2895 579/579 49/49 - 262144 252884 252884/318420 3185/3185 2895/2895 579/579 49/49 + capacity retained Book Screen Bar:1m Bar:5m Bar:1h Cap + 2048 1767 1767/318420 248/3185 229/2895 147/579 49/49 4/4 + 8192 6316 6316/318420 689/3185 897/2895 579/579 49/49 4/4 + 20000 16395 16395/318420 1793/3185 1781/2895 579/579 49/49 4/4 + 65536 61059 61059/318420 3185/3185 2895/2895 579/579 49/49 4/4 + 262144 220713 220713/318420 3185/3185 2895/2895 579/579 49/49 4/4 ``` diff --git a/docs/.readme_assets/other.md b/docs/.readme_assets/other.md index 855949f..80cc4f3 100644 --- a/docs/.readme_assets/other.md +++ b/docs/.readme_assets/other.md @@ -1,14 +1,13 @@ ### Picking a `capacity` -`capacity` is the only number `Viz::new` asks for that has no right answer — it is RAM traded -against reactivity, and the exchange rate is your graph's, not this crate's. What it constrains: +`capacity` is the only number `Tape::new` asks for that has no right answer — it buys scrollback with +RAM, and the exchange rate is your graph's, not this crate's. What it constrains: | | | |---|---| | `capacity / 2` | the newest ticks kept **whole** — the stretch that stays tick-exact | | `capacity / 4` | the ceiling on the thinned *backbone*, and so how deep the squeeze goes | | `capacity / 2` | the recycle channel's depth: one pass frees between a quarter and a half of the buffer, and the recorder takes all of it back rather than allocating | -| `capacity` | the bound on the carry-forward scan a frame does for every quiet node, which is what landing a cursor costs | Thinning is **per node, not per tick**: a pass raises the exponent of whichever node is claiming most of the backbone until the backbone fits, so a node firing 288 times a day keeps all 288 while the @@ -20,40 +19,48 @@ that each want ~3 000 land on ~1 790 apiece, and everything rarer than that keep had. Read the other way: a 5-minute bar over three days is 864 fires and is safe from about 8 000; over a month it is 8 640 and wants ~65 000. -Bigger is not free, and the price is not where it looks. Recording cost does not move with -`capacity` at all — the graph thread's leg of a fire is the same two renderings either way. What -moves is the cost of *landing* a cursor, because a frame carries every node's standing value and a -node quiet for a while is found by scanning back for it. +**The price is memory, and only memory.** Recording cost does not move with `capacity` — the graph +thread's leg of a fire is the same two renderings either way — and neither does cursor latency: every +replay op is a binary search, so a keypress costs the same against 260 000 retained ticks as against +2 000. In the block below the whole latency column is the loopback socket; the tape's own share does +not clear the noise. -The block below is one synthetic graph on one machine. `Viz::bytes` is exported so the same reading -can be taken against yours. +That is worth stating because it was not always true. A frame carries every node's standing value, +and a node that has been quiet is found by searching back for its last fire — which used to be a +linear scan. On a 42-node graph holding a day, one node that fires twice a day was enough to put a +scrub at 139ms, growing with wherever the cursor sat. `Inner::fired` indexes it, and the synthetic +mix below now carries a twice-a-day node so the number here cannot quietly go back to being one +nobody waits for. + +The block is one synthetic graph on one machine. `Viz::bytes` is exported so the same reading can be +taken against yours. ``` -318420 ticks (2 days at spl's 159210/day), 5 nodes, 107 B/tick of card faces -fires over the run: Book=318420 Screen=3185 Bar:1m=2895 Bar:5m=579 Bar:1h=49 +318420 ticks (2 days at spl's 159210/day), 6 nodes, 107 B/tick of card faces +fires over the run: Book=318420 Screen=3185 Bar:1m=2895 Bar:5m=579 Bar:1h=49 Cap=4 footprint — Viz::bytes over the retained ticks - 2048 │▬ 3.0 MB (1477 B/tick) - 8192 │▬▬ 5.4 MB (818 B/tick) - 20000 │▬▬▬▬ 11.3 MB (665 B/tick) - 65536 │▬▬▬▬▬▬▬▬▬▬▬ 33.8 MB (554 B/tick) - 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 132.6 MB (524 B/tick) - -reactivity — /api/seek mid-tape *above* a bare 53µs loopback hop, median of 64 - 2048 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 8192 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 20000 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 65536 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 14µs - 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 34µs + 2048 │▬ 1.2 MB (691 B/tick) + 8192 │▬ 4.0 MB (637 B/tick) + 20000 │▬▬▬▬ 9.8 MB (594 B/tick) + 65536 │▬▬▬▬▬▬▬▬▬▬▬▬ 34.1 MB (558 B/tick) + 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 121.0 MB (548 B/tick) + +reactivity — one /api/seek mid-tape, median of 64; the bare loopback hop under it is 53µs + 2048 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 8192 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 68µs + 20000 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 65536 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 68µs addressability — fires still reachable by `step_until`, against the run's own - capacity retained Book Screen Bar:1m Bar:5m Bar:1h - 2048 2007 2007/318420 251/3185 231/2895 147/579 49/49 - 8192 6557 6557/318420 691/3185 899/2895 579/579 49/49 - 20000 16951 16951/318420 1796/3185 1784/2895 579/579 49/49 - 65536 61047 61047/318420 3185/3185 2895/2895 579/579 49/49 - 262144 252884 252884/318420 3185/3185 2895/2895 579/579 49/49 + capacity retained Book Screen Bar:1m Bar:5m Bar:1h Cap + 2048 1767 1767/318420 248/3185 229/2895 147/579 49/49 4/4 + 8192 6316 6316/318420 689/3185 897/2895 579/579 49/49 4/4 + 20000 16395 16395/318420 1793/3185 1781/2895 579/579 49/49 4/4 + 65536 61059 61059/318420 3185/3185 2895/2895 579/579 49/49 4/4 + 262144 220713 220713/318420 3185/3185 2895/2895 579/579 49/49 4/4 ``` diff --git a/docs/capacity.txt b/docs/capacity.txt index bc93ce3..66cffb5 100644 --- a/docs/capacity.txt +++ b/docs/capacity.txt @@ -1,24 +1,24 @@ -318420 ticks (2 days at spl's 159210/day), 5 nodes, 107 B/tick of card faces -fires over the run: Book=318420 Screen=3185 Bar:1m=2895 Bar:5m=579 Bar:1h=49 +318420 ticks (2 days at spl's 159210/day), 6 nodes, 107 B/tick of card faces +fires over the run: Book=318420 Screen=3185 Bar:1m=2895 Bar:5m=579 Bar:1h=49 Cap=4 footprint — Viz::bytes over the retained ticks - 2048 │▬ 3.0 MB (1477 B/tick) - 8192 │▬▬ 5.4 MB (818 B/tick) - 20000 │▬▬▬▬ 11.3 MB (665 B/tick) - 65536 │▬▬▬▬▬▬▬▬▬▬▬ 33.8 MB (554 B/tick) - 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 132.6 MB (524 B/tick) + 2048 │▬ 1.2 MB (691 B/tick) + 8192 │▬ 4.0 MB (637 B/tick) + 20000 │▬▬▬▬ 9.8 MB (594 B/tick) + 65536 │▬▬▬▬▬▬▬▬▬▬▬▬ 34.1 MB (558 B/tick) + 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 121.0 MB (548 B/tick) -reactivity — /api/seek mid-tape *above* a bare 53µs loopback hop, median of 64 - 2048 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 8192 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 20000 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 13µs - 65536 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 14µs - 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 34µs +reactivity — one /api/seek mid-tape, median of 64; the bare loopback hop under it is 53µs + 2048 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 8192 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 68µs + 20000 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 65536 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 66µs + 262144 │▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ 68µs addressability — fires still reachable by `step_until`, against the run's own - capacity retained Book Screen Bar:1m Bar:5m Bar:1h - 2048 2007 2007/318420 251/3185 231/2895 147/579 49/49 - 8192 6557 6557/318420 691/3185 899/2895 579/579 49/49 - 20000 16951 16951/318420 1796/3185 1784/2895 579/579 49/49 - 65536 61047 61047/318420 3185/3185 2895/2895 579/579 49/49 - 262144 252884 252884/318420 3185/3185 2895/2895 579/579 49/49 + capacity retained Book Screen Bar:1m Bar:5m Bar:1h Cap + 2048 1767 1767/318420 248/3185 229/2895 147/579 49/49 4/4 + 8192 6316 6316/318420 689/3185 897/2895 579/579 49/49 4/4 + 20000 16395 16395/318420 1793/3185 1781/2895 579/579 49/49 4/4 + 65536 61059 61059/318420 3185/3185 2895/2895 579/579 49/49 4/4 + 262144 220713 220713/318420 3185/3185 2895/2895 579/579 49/49 4/4 diff --git a/exec_viz/examples/capacity.rs b/exec_viz/examples/capacity.rs index 358649a..fcc8e79 100644 --- a/exec_viz/examples/capacity.rs +++ b/exec_viz/examples/capacity.rs @@ -36,7 +36,13 @@ const BAR: usize = 44; const MIN: usize = TICKS_PER_DAY / 1440; /// The book node carries the whole per-tick face budget on its own: spl spreads it over four that /// all fire on nearly every tick, and what the tape holds is the sum either way. -const NODES: [Node; 5] = [ +/// +/// `Cap` is here because a mix without it measures the wrong thing. A frame carries every node's +/// standing value, and a node that has been quiet is found by searching back for its last fire — so +/// the cost of landing a cursor is set by the *rarest* node, not the busiest. spl's market cap fires +/// twice a day and its classification can go a whole one without firing; a mix whose rarest node +/// still fires hourly reports a latency nobody has. +const NODES: [Node; 6] = [ Node { name: "Book", period: 1, @@ -67,6 +73,12 @@ const NODES: [Node; 5] = [ face: 24, dims: &[5], }, + Node { + name: "Cap", + period: TICKS_PER_DAY / 2, + face: 24, + dims: &[1], + }, ]; const CARD: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; const BEGIN: &str = ""; @@ -272,11 +284,11 @@ fn render(rows: &[Row], fires: &[usize], ticks: usize, hop_us: f64) -> String { chart(format_args!("footprint — Viz::bytes over the retained ticks\n"), &|r| r.bytes as f64 / 1e6, &|r, v| { format!("{v:.1} MB ({} B/tick)", r.bytes / r.retained) }); - // Above the hop, not including it: a loopback round trip is the larger half of every reading here - // and none of it is the tape's, so charting the raw number draws five bars of the same length. + // What one keypress costs, socket included. An earlier version charted this minus the loopback hop, + // which drew a prettier curve and told the reader a number they never wait for. chart( - format_args!("reactivity — /api/seek mid-tape *above* a bare {hop_us:.0}µs loopback hop, median of {SAMPLES}\n"), - &|r| (r.seek_us - hop_us).max(0.0), + format_args!("reactivity — one /api/seek mid-tape, median of {SAMPLES}; the bare loopback hop under it is {hop_us:.0}µs\n"), + &|r| r.seek_us, &|_, v| format!("{v:.0}µs"), ); diff --git a/exec_viz/src/tape.rs b/exec_viz/src/tape.rs index abd6672..6f3d374 100644 --- a/exec_viz/src/tape.rs +++ b/exec_viz/src/tape.rs @@ -99,6 +99,7 @@ impl Tape { opened: 0, fires: Vec::new(), keep: Vec::new(), + fired: Vec::new(), sealed: false, dropped: dropped.clone(), series: Vec::new(), @@ -246,6 +247,7 @@ impl Viz { )); } let file: TapeFile = rmp_serde::decode::from_read(&mut r).map_err(std::io::Error::other)?; + let ticks: VecDeque = file.ticks.into(); Ok(Self { inner: Arc::new(Mutex::new(Inner { @@ -254,10 +256,11 @@ impl Viz { bucket_ms: file.bucket_ms, topology: file.topology, cost: file.cost, - ticks: file.ticks.into(), opened: file.opened, fires: file.fires, keep: file.keep, + fired: index(&ticks), + ticks, sealed: true, dropped: Arc::new(AtomicUsize::new(file.dropped)), series: file.series, @@ -597,6 +600,14 @@ pub(crate) struct Inner { /// Per-node "keep one fire in `2^keep[i]`", positional with `topology` and only ever raised — /// see [`Inner::thin`]. keep: Vec, + /// Per node, ascending, the `ticks` *positions* it fired on — what [`Inner::held`] binary-searches + /// instead of scanning. Positions and not `abs` ids because between thinning passes `ticks` only + /// grows at the back, so a position already recorded cannot move; [`Inner::thin`], which is the + /// one thing that does move them, rebuilds this in the walk it was already making. + /// + /// Derived, so it is not in [`TapeFile`] — [`Viz::load`] rebuilds it rather than trusting a file + /// to agree with the ticks beside it. + fired: Vec>, /// The recording is over — see [`Inner::head`]. sealed: bool, /// The recorder's drop tally, shared — see [`Recorder::dropped`]. @@ -672,6 +683,15 @@ impl Inner { debug_assert!(self.ticks.len() < self.capacity); let abs = self.opened; self.opened += 1; + let p = self.ticks.len(); + // After the push, so `p` is the position the tick lands on. Appending only ever extends each + // node's list at the back, which is what keeps them ascending without a sort. + self.fired.resize(ranks.len(), Vec::new()); + for (i, r) in ranks.iter().enumerate() { + if *r > 0 { + self.fired[i].push(p); + } + } self.ticks.push_back(Tick { abs, ts_ns, acts, ranks }); freed } @@ -728,6 +748,7 @@ impl Inner { false => freed.push(t.acts), } } + self.fired = index(kept.iter()); self.ticks = kept; freed } @@ -835,10 +856,15 @@ impl Inner { /// Node `i`'s newest act at or before position `p` that fired; `None` if it never has. Searched /// rather than stamped, so what a scrubbed frame carries forward is a value the tape can still /// show — a remembered tick number would outlive the tick a thinning pass dropped. - /// ponytail: linear scan, bounded by `capacity`; index the fires per node if a frame ever costs - /// enough to feel. + /// + /// Searched over [`Inner::fired`] rather than back over the ticks: a frame calls this once per + /// quiet node, and a node that has not fired for a long stretch — a twice-a-day market cap, a + /// classification that never triggers — walked the whole retained tape for it. That put a scrub + /// at 139ms on a 42-node graph holding a day, growing linearly with where the cursor sat. fn held(&self, i: usize, p: usize) -> Option> { - self.ticks.iter().take(p + 1).rev().find_map(|t| t.acts.get(i).filter(|a| a.vals.is_some())) + let fired = self.fired.get(i)?; + let q = fired[..fired.partition_point(|&q| q <= p)].last()?; + self.ticks[*q].acts.get(i).filter(|a| a.vals.is_some()) } /// `n` retained ticks on, not `n` absolute ones: in a thinned stretch the ticks between two @@ -955,6 +981,21 @@ impl From<&Plot> for PlotOut { } } +/// Per node, the positions in `ticks` it fired on — [`Inner::fired`] built from scratch, which is +/// what both the thinning pass and a tape read back off disk need. Ascending because the ticks are. +fn index<'a>(ticks: impl IntoIterator) -> Vec> { + let mut fired: Vec> = Vec::new(); + for (p, t) in ticks.into_iter().enumerate() { + fired.resize(fired.len().max(t.ranks.len()), Vec::new()); + for (i, r) in t.ranks.iter().enumerate() { + if *r > 0 { + fired[i].push(p); + } + } + } + fired +} + /// Drops module paths at every depth, so a card reads as the types it names: /// `Buffer` /// → `Buffer`. A segment is a module iff it starts