04

Paint Less: Culling, Adaptive Quality and Raster Tiles

A wide world holds thousands of SVG nodes and the child sees one screen of them. Day 4 makes the browser stop walking, painting and allocating for what nobody can see, without changing a single visible pixel.

The loop that ranked its own work

Days 2 and 3 fixed what the first traces pointed at. Day 4 starts a different rhythm, used for the rest of the course: a time-boxed loop where every item starts from a trace or profile of the current build at 6× CPU, is ranked by measured cost, is fixed one at a time, and is measured again before the next. Nothing is fixed because it looks slow.

Culling: `display:none` beats `visibility:hidden`

The first culling pass (Day 3's round) hid objects more than a screen away with visibility:hidden. It helped, but a trace showed the browser still walked those nodes in every paint and layerize. An experiment with CSS alone measured the alternative: display:none removed them from the render tree and cut a low-end market drag's main-thread time from 20.1 s to 17.1 s (−15 %).

There is a catch, and it is why an earlier attempt was rejected. Putting display:none on an object's root element broke two things: hit testing near the edge of the screen, and scrollIntoView on far objects (hosts and tests use it to bring an object on screen). The kept design hides the object's drawing (g.sa-body) and keeps its root and its invisible tap pad laid out.

<g data-actor="apple-12" role="button" aria-label="apple">   <- root: stays laid out
  <rect class="sa-hit" .../>                                 <- tap pad: stays
  <g class="sa-body" display="none"> ...parts... </g>        <- drawing: culled
</g>

Adaptive quality, level 3

Day 3's governor had two levels. Profiling a sustained low-end drag showed most per-frame invalidations came from idle objects breathing (a slow, gentle scale). Level 3 redraws idle objects, those not held, hovered, reacting, moving or talking, every fourth frame, staggered across the cast so a quarter of them update each frame. Busy objects still redraw every frame. A test proves both halves: the held object redraws at least 15 of 16 frames, an idle one at most 5.

Don't build strings for parts at rest

A CPU profile put garbage collection at 2.5 % of a low-end drag frame, mostly from transform strings rebuilt for every part of every object, every frame, including parts that had not moved. The fix compares a part's numeric inputs, rounded to the same 0.01 the writer uses, in a reused buffer, before building any string. A resting part now allocates nothing.

Scenery as images: raster tiles

A newer version of the worlds brought much richer scenery: a market world's svg held about 29 000 scenery nodes, roughly 1 500 near the view at any time, and a trace with scenery hidden cut an idle low-end frame's paint by about 75 %.

The fix turns each 1 024-unit tile of scenery into one SVG image, an <image> whose source is a blob of that tile's vectors. The browser decodes it once and draws it as a single cached image. Vector nodes stay underneath until their tile has loaded, so nothing ever appears late or blank.

Scenery as vectors vs as raster tiles, and the difference: at most 0.003 % of pixels differ, on pulsing door badges

Parity was checked in the browser, three worlds by three scroll positions: at most 0.003 % of pixels differed by more than 8/255, and those were door badges that pulse.

Before and after side by side

Low-end, 6× CPU, the market world, loop start vs loop end.

HUD at four moments: loop start 18–35 fps (52–78 % dropped), loop end 32–42 fps (43–49 %)

low-end, same run, median of 2loop startloop end
market idle, dropped98.7 %54.5 %
market drag-hold, dropped · input p9582.6 % · 99 ms45.0 % · 71 ms
playground idle, dropped89.3 %24.8 %
playground drag-hold95.8 % · 119 ms43.3 % · 82 ms
playground tap-spam99.6 % · 178 ms76.2 % · 139 ms
mid-range market drag-slow28.3 % · 106 ms9.3 % · 71 ms

One cell got worse in that run: low-end market tap-spam went from 48.9 % to 66.0 % dropped, while its input latency improved. Two interleaved reruns measured no difference (93/87 % vs 92/90 %), so the ledger records it as unresolved, not a win. Writing that down honestly is part of the method.

Design principles at work

  • Keep the contract, change the implementation. Hosts and tests rely on each object having a laid-out root with a label and a tap pad. Culling, tiles and strides all work behind that contract, which is why none of them needed a change outside the view.
  • Open/closed through levels. The governor's levels are additive policies; level 3 added a stride rule without touching levels 1 and 2.
  • Measure the alternative before choosing. visibility:hidden vs display:none was decided by a trace, and the rejected root-culling variant is in the ledger with the reason.

Key takeaways

  • visibility:hidden still costs paint traversal; display:none removes the subtree. Cull the drawing, keep the parts others depend on.
  • Idle animation is often the bulk of the per-frame work on slow devices: stride it, don't remove it.
  • Compare numbers before allocating strings; the cheapest allocation is the one you skip.
  • Turning thousands of static vectors into a few images is a large, invisible win, if you prove pixel parity.