DEV Community

Abdelrahman Ahmed Abdelazeez
Abdelrahman Ahmed Abdelazeez

Posted on

Stepping backwards through a program should be free

Post 1 was about making the recording small. This one is about what happens when you press the back arrow a thousand times.

In a normal debugger, stepping back is expensive, because the only way to know the past is to run the program again from the start. In this visualizer the past is a file. The program runs once, the tracer writes every snapshot down, and after that the browser holds all of it in memory. Stepping back is not a rewind. It is a different index.

A step is an index, not a computation

Playback state is one number, the event index, and every snapshot is already sitting in the array:

const [index, setIndex] = useState(0);
const seek = (i: number) => { setPlaying(false); setIndex(clampStep(i, count)); };
Enter fullscreen mode Exit fullscreen mode

That is the whole of "jump to step 900". There is no replaying from step 0, no re-running the program, no undo log to unwind. Reading the snapshot you landed on is a property read on an object that was built when the file was loaded.

I measured it, because "should be fast" is not a number. On the 2,000-event recording (a 300-node linked list built by a loop), jumping to a random index and reading that snapshot's heap object averaged 0.08 microseconds over 20,000 jumps. The snapshot holds about 17 objects on average, and each one is a reference into the shared node pool, so nothing is copied on the way there.

Recording File size (pooled) Load: expand Jump to any step
300-node list, 2,000 events 1.21 MB 30.2 ms 0.08 µs
40-dict list, 367 events 0.37 MB 9.3 ms 0.13 µs
120-node list, 400 events 0.22 MB 2.7 ms 0.06 µs

Worth saying plainly: pooling does not make the load faster. Expanding the pooled 2,000-event file takes 30 ms, while the 4.8 MB unpooled version of the same run parses in 25 ms, because unpacking does real work per node. Pooling buys bytes on disk and on the wire. It does not buy parse time. The second recording shows the other side: 0.37 MB expands in 9 ms where its unpooled 6.7 MB takes 44 ms, because that program repeats the same forty dictionaries over and over.

The step is free. The picture is not.

Stepping is free and the app still felt sluggish on big graphs, which tells you where the cost actually lives: turning one snapshot into boxes and arrows. That happens on every step, and the back arrow asks for a picture that was already drawn once.

Three things keep it cheap.

Scenes are built in order, on demand, in slices. Lane placement depends on the step before it, so scenes cannot be built out of order. Instead of building all of them on load, the cache builds only up to the index you asked for, and it can do that in small time slices so a big recording never freezes the page. The comment on that function reads: "at most max scenes and at most ~ms milliseconds, so big scenes never hold the page for long."

Arrow routing is cached by what it depends on. Laying out boxes is cheap; routing arrows around boxes is the expensive part, so it is done only for a scene someone actually looks at, and the result is cached under a signature built from the links themselves (from, cell, to, port). The cache holds 24 scenes. Stepping back to a scene you already saw reuses its routed paths instead of recomputing them.

The source panel re-renders two lines, not the whole file. Each line is its own memoized component, so a step changes the old line and the new one. This was the single change with a measurable effect on big source files. On a 2,500-line program with the CPU slowed to a sixth, seek p95 fell from 154 ms to 66.5 ms and total long-task time from 2.3 s to 1.6 s. The price is a slower first load for a file that size, 1,077 ms to 1,365 ms, because every line now carries a breakpoint button.

What the numbers do and do not say

Seek p95 is the number that matters for step-back, and on normal recordings it sits at the floor of the measurement: 33.3 ms, which is two animation frames at 60 Hz. That is not a speed record, it is the resolution of the ruler. What the measurement can show is the slow cases: at six times slower CPU, 1,806 events lands at 69.7 ms p95, 10,000 synthetic events at 34.3 ms, and the 2,500-line file at 66.5 ms.

One caveat that matters more than any of these: everything is in the browser, which is why stepping is free and also why there are caps. The reader refuses files over 20 MiB, stops at 10,000 events, and checks the expanded size as it unpacks so a small file cannot claim to expand into something that eats the tab. A run of a real program with a big heap hits those caps long before it hits a slow frame. The choice is deliberate: better to say "this recording is too big to replay" than to hand someone a tab that dies mid-step.

The lesson that transfers

Stepping backwards is free when the past is data instead of a re-execution. The work does not disappear, it moves: to the load, where it is paid once, and to drawing, where it is paid per step and can be cached by exactly what it depends on. If you find yourself making step-back fast by making the program cheaper to re-run, you are solving the wrong problem.

Numbers are from one machine (Node 22.23.5 for the jump and load figures, Chrome with CPU throttling for the seek figures), on synthetic programs, using the app's own measurement harness. Rerun them on your own hardware before quoting them.

Top comments (0)