sg

← Writing

The Version That Looked Faster

· performance · systems

On a cold load, a blurry preview layer filled 90% of Fractal's canvas 37.4% sooner. That's the number I would have put on a slide. But the sharp final images arrived 46.8% later, and the layer cost more bytes and more CPU, so I removed it.

To see why both things can be true, you need to know how the canvas fills in. When you zoom, the server draws the new level as 256×256 PNG images, called cells, and sends them to the browser. Until they arrive, the browser fills the gap with whatever blurrier images it already has. Where it has nothing, the view has a hole.

The first round of performance work cut wasted scanning in the rasterizer, the code that turns strokes into pixels. The second round ran two experiments from a written plan: the preview layer, and a change that lets the rasterizer skip shapes it can't hit. Each had pass/fail gates, set before agents wrote any of the code (the division of labor). The rasterizer change shipped and the preview layer didn't.

The idea: one coarser level first#

The preview layer, called coverage, added one extra level of blurrier images. For every set of final cells the browser asked for, the server also made the cells one level up, where one blurry image fills the space of a 2×2 group of sharp ones. It put them first: while final work waited, it built up to four coverage images before each final one. The final images themselves did not change.

The hope was that a blurry image of the right region beats a hole. The catch was that every coverage image is one more thing to draw and send while the final images wait.

The gates came first#

Before any code existed, the plan set three tests coverage had to pass to ship. They lean on area-time, which measures how much of the view sat empty and for how long, in viewport-milliseconds: a fully empty view for one millisecond counts as one. A cold load is the first load of the canvas, and p95 is the slow end of the runs.

  • Median uncovered area-time on the main cold-load and zoom route had to drop at least 20%.
  • The p95 time until final detail was ready could not get more than 10% worse.
  • A rise of more than 20% in CPU, bytes sent, or memory failed it.

The plan also named the catch before there was any result to argue with: if all coverage bought was sooner first pixels, paid for by drawing the same region twice, that was not a win to accept quietly.

How it was measured#

The test was fixed in advance too. Both versions drew the same fixed set of 6,481 strokes. The view was 1024×1024 CSS pixels at DPR 2, meaning two screen pixels per CSS pixel. All traffic went through a proxy that passed data along in order, slowed to 10 Mbps with a 40 ms round trip. Each run started with a new server and a fresh copy of the data, loaded the overview until ready, then ran the same eight-second continuous zoom from depth 0 to depth 22 and back. Five pairs of runs took turns between the baseline and the candidate, and the plan didn't allow rerunning to chase a better result. The baseline already included the rasterizer change from the other experiment, so none of that gain counts toward coverage. Everything ran on one local machine.

"Covered" means the renderer had an image to draw for that part of the view in that frame. It comes from the renderer's own plan for each frame, not from timing when the screen actually updated. Vector strokes may still draw over an uncovered area. CPU here is time spent on the browser's main thread. Every row is a median of five runs, except the p95 rows.1 A blank Gate cell means the row had no gate.

MetricBaselineCoverageChangeGate
Cold 50% covered (ms)2,809.91,840.5-34.5%
Cold 90% covered (ms)4,654.72,912.8-37.4%
Cold uncovered area-time (viewport-ms)1,999.41,177.5-41.1%Pass (needs 20% drop)
Cold final-ready (ms)5,118.37,513.6+46.8%
Cold final-ready p95 (ms)5,202.97,571.0+45.5%Fail (10% limit)
Motion uncovered area-time (viewport-ms)899.22,763.4+207.3%Fail (needs 20% drop)
Final-ready wait after motion (ms)10,577.114,389.4+36.0%
Final-ready wait after motion p95 (ms)11,653.615,429.4+32.4%Fail (10% limit)
Downstream TCP bytes (MB)27.935.9+28.4%Fail (20% limit)
Cold main-thread CPU (ms)545.1802.4+47.2%Fail (20% limit)
Zoom route and drain main-thread CPU (ms)2,349.12,609.6+11.1%Pass (20% limit)

The number I would have put on a slide#

The top three rows are real, and they're what I would have shown someone. On a cold load the canvas was half covered about a second sooner, and 90% covered 1.7 seconds sooner.

The rest of the table is the cost. Coverage images fought final detail for the same server workers and the same network link. On a cold load, final detail took 7.5 seconds instead of 5.1. During the zoom, the feature meant to reduce holes tripled uncovered area-time.

That zoom result isn't a fluke of a few runs, because the results don't overlap: every candidate run (2,753.9 to 3,111.6 viewport-ms) was worse than every baseline run (887.9 to 2,745.6). The closest pair, the worst baseline run and the best candidate run, was 8.3 viewport-ms apart.

Five of the seven gated rows in the table failed, and that settled it. The candidate itself worked: it passed 100 scratch checks, an independent review found no confirmed bug, and all ten runs produced byte-identical final artwork.2 I removed all of it anyway, including the experiment's change to the format the server and browser use to talk to each other, which it had moved to version 2. The shipped format is still version 1.

The half that shipped#

The other experiment in the plan, an active-edge sweep, did ship. It changes how the rasterizer finds the shapes it needs to draw. The rasterizer splits each pixel row into thin strips, and at each strip's height it asks every polygon and disk in a stroke's outline what it covers. The first round made that cheaper, but every strip still visited shapes that couldn't touch it. In overview cells, under 8% of the polygons, edges, and disks checked at a given height actually crossed it.

Active-edge sweep over one pixel rowA stroke made of 10 polygons crosses one pixel row. The row is cut into 8 strips plus a cut at each polygon vertex height inside it. At the marked height the sweep only visits the polygons whose vertical range contains it; the other 8 are never visited.A stroke is 10 polygons12345678910one pixel rowThe row, magnified8 equal strips, plus a cut at each vertex heightvertex cutvertex cutvertex cutvertex cut12 strips after the cuts, each sampled at mid-heightthis heightAt this height, the sweep visits only polygons that reach it12345678910skippedvisitedskipped
One pixel row under the sweep. Only the two polygons that reach the marked height are visited; the other eight are skipped without walking their edges.

The sweep keeps a set of the shapes that cross the current height. As it moves down the cell, it adds shapes whose top it has reached and drops the ones it has passed. The math and its order don't change, so the output is exact.

Its gate asked for three things: median raster time had to improve at least 10%, deep zoom could not get more than 5% slower, and allocations and peak memory could not grow unexplained. For each view, the test drew and PNG-encoded 64 cells one after another. Every view cleared the 10% bar, and deep zoom got faster instead of slower. Numbers are medians of five alternating pairs:

ViewBefore (ms)After (ms)Change
Overview2,230.21,659.1-25.6%
Depth 131,167.1508.8-56.4%
Depth 222,396.1724.1-69.8%

All 192 cells matched the old images exactly, down to the PNG hash.

It isn't free. The sweep's extra arrays add 6 to 7% more allocated bytes per render, and peak process memory rose by under 2%. The arrays live only as long as one render, so I took that trade.3

A threshold that would have been right for the wrong reason#

Neither of those verdicts depended on where a gate was set. Coverage missed by wide margins: p95 up 45.5% against a 10% limit, and cold CPU up 47.2% against 20%. The sweep cleared its 10% bar with 25.6% to 69.8%. A threshold only matters when a result lands near it, and a later experiment landed near one.

That experiment was about the showcase artwork. The program that makes it writes lines with many closely spaced points, so three candidates thinned out the redundant ones. They cut compressed size, measured offline, by 5.2%, 7.0%, and 11.0%, and raster CPU by 4.2%, 9.5%, and 10.4%. The experiment started with a gate: compressed size had to drop at least 15%. The best candidate came in at 11.0%, so the gate would have rejected all three on size.

Nothing justified 15%, so I removed it. Now any repeatable gain can qualify, as long as quality and the other measures hold. All three candidates were still rejected, for a different reason. Crops drawn at full resolution at deep zoom showed shapes moving. One curved pen edge became nearly straight and shifted several pixels at camera depth 20.

The cause is magnification. Each stroke stores its points relative to the tile it was drawn on, where one unit is one tile's width, and the stroke's tile depth is how deep that tile sits. An error in those points grows on screen with every level you zoom past that depth. In device pixels, it moves this far, which is what shifted the pen edge above:

1024 × 2^(cameraDepth - strokeTileDepth) × DPR × localError

The 1,024 comes from the canvas, not the window: it is the camera's base size, and at zoom 0 the whole canvas is 1,024 CSS pixels across.4 So a tile at depth d spans 1024 × 2^(zoom - d) CSS pixels, and DPR turns that into device pixels.

Each candidate had a tolerance, the largest error it allowed while thinning points. The pen stroke above was drawn on a depth-14 tile, so at depth 20 the 0.0001 tolerance is 1024 × 2^6 × 1 × 0.0001, about 6.55 pixels at DPR 1, close to the shift in the crop.

Deeper zoom makes the same kind of error far larger. A tolerance of 0.00005 is 1/20,000 of a tile, but on a tile at depth 10, viewed at depth 24 and DPR 1, an error that size becomes 1024 × 2^14 × 1 × 0.00005, roughly 839 device pixels.5

So the size gate would have reached the same verdict and hidden the real finding. Tolerances picked without thinking about the zoom range didn't keep shapes in place at native deep zoom. Read through the size gate, the lesson would have been "remove more points," which moves shapes even further. A candidate that allows for maximum zoom magnification might work, but it hasn't been built.

What's left#

Two questions from this round are still open. One comes from the coverage runs, where the diagnostics showed more PNG data arriving for cells the browser no longer wanted: 13.5 MB of images outside the current view in the baseline, and 17.2 MB with coverage.6 That points at stale images still being sent during the zoom. But the numbers don't say whether the server queue, the proxy, or network timing caused it, so I haven't acted on them.

The other is whether the remaining bars had reasons behind them. The project's performance report has a table of experiments that didn't ship, and each row puts the measured gain next to the test it failed. Coverage is in it, and so are two interval-sort variants, at 8.84% and 8.99% against a 10% payoff threshold. Those decisions stand as recorded, but I haven't gone back to check whether 10% had any better reason behind it than 15%.

Footnotes

  1. With five runs, p95 is just the slowest one. ↩

  2. Some follow-ups, like frame captures and resource runs with several viewers, never ran, and I don't count them as passes. ↩

  3. These are render times on their own. They don't mean the view in the browser got 69.8% faster. ↩

  4. The benchmark viewport also being 1,024 pixels wide is a coincidence. ↩

  5. Nobody saw a shift that big. It's how far an error equal to the tolerance would land. ↩

  6. Those are PNG image sizes alone, counted separately from the TCP totals in the table. ↩