Caching and snapshot churn
I wanted to know whether caching pays off when only a few cards in a 24-card canvas dashboard change each frame.
This page pits full drawElementImage() replay against a main-thread captureElementImage() cache,
a plain offscreen 2D canvas per card, and a clipped dirty-rect redraw using nine-argument drawElementImage().
Below that, a separate test churns ElementImage snapshots at 60 fps and watches frame time and memory.
Idle. The 24 cards above are drawn once on load with zero open snapshots. Pick a strategy or press Run full sweep to time your device.
| Strategy | Dirty | rAF p50 | rAF p95 | Paint JS | LoAF (>50 ms / post-main) | Snapshots (made / peak / end) | Canvas MB |
|---|---|---|---|---|---|---|---|
| Not run yet. Press Run selected or Run full sweep above. | |||||||
Show the code the four partial-update strategies
- Full replay. Resets the canvas and replays five-argument
drawElementImage()for all 24 cards every frame. - Main-thread
captureElementImage()cache. Re-captures only cards ine.changedElementsor marked dirty, closing the old snapshot before capturing the replacement so open snapshots stay at or below 24. - Plain offscreen 2D canvas cache. Draws dirty cards once and copies each dirty card into a plain non-drawable
<canvas>; clean cards blit withctx.drawImage(). - Clipped dirty-rect + nine-argument source-rect draw. Keeps the existing canvas pixels, clips to the mutating metric strip of each dirty card, and updates it with nine-argument
drawElementImage().
Snapshot churn at 60 fps
Taking an ElementImage snapshot on a timer and forgetting to call close() holds GPU memory until garbage collection.
This test measures the other case: taking snapshots at 60/s (and 240/s at 4 cards per frame) and closing every one either straight away inside the same paint handler or in batches of 8, against a direct drawElementImage() baseline.
Not run yet. Every snapshot created during the test is closed before the mode finishes.
| Churn mode | rAF p50 | rAF p95 | Paint JS | Capture + close | LoAF (>50 ms / post-main) | Snapshots (made / peak / end) | Canvas MB |
|---|---|---|---|---|---|---|---|
| Not run yet. Press Run churn test above. | |||||||
Show the code 60 fps snapshot churn and pool teardown
- Churn loop. Captures 1 or 4 cards per frame inside
paint, draws the snapshot, and either callsclose()straight away or drains a pool of 8 every eighth frame.
How it works
- All 24 cards are live
drawablechildren of one<canvas content="drawable" layoutsubtree>. Each card has a static header with a working pin button and a lower metric strip whose text, badge and progress bar mutate at the chosen dirty ratio (0, 1, 6 or 24 cards per frame). - In the
captureElementImage()cache strategy, the paint handler checkse.changedElementsalongside its dirty set and closes each replacedElementImagebefore callingcaptureElementImage()for the new one, keeping open handles capped at 24. When a cell finishes or stops, every cached snapshot is closed sowindow.__gfx.snapshots.openreturns to 0. - In the plain offscreen 2D canvas strategy, each card gets a plain non-drawable
<canvas>(nolayoutsubtreeand nowillReadFrequently). Dirty cards are drawn once withdrawElementImage()and copied into their offscreen canvas withdrawImage(); clean cards skipdrawElementImage()and blit from their offscreen canvas. All 24 offscreen canvases are zeroed and handed towindow.__gfx.retire()on teardown. - In the clipped dirty-rect strategy, the paint handler skips
ctx.reset()after the first frame, sets a clip rect around each dirty card's lower metric strip, and calls nine-argumentdrawElementImage(el, sx, sy, sw, sh, dx, dy, dw, dh, { preserveElementGeometry: true })in CSS pixel coordinates. - Each cell runs a short warmup and then records
requestAnimationFramep50 and p95 deltas, JS paint-handler duration, Long Animation Frame (long-animation-frame) count and post-main-thread time (presentationTime - (startTime + duration)), open snapshot counts fromwindow.__gfx.snapshots, and estimated canvas memory fromwindow.__gfx.memory(). - Pressing "Run full sweep" runs all 16 partial-update cells plus all 4 churn modes and posts a compact summary to
/api/reportsviawindow.__gfx.report()on deployed origins.
Live checks
Each check runs on its own tiny fixture on the hidden test stage and closes every snapshot and canvas when it finishes.
| Result | Check | Detail |
|---|
Findings
- Not yet measured on real hardware or headless Chromium 157. Press "Run full sweep" on a desktop GPU and on Chrome for Android to compare whether main-thread snapshot caching, offscreen 2D canvas blitting or clipped nine-argument dirty-rect draws beat full 24-card
drawElementImage()replay at 0%, 5% and 25% dirty ratios. - Close before replace matters for the snapshot budget. With 24 cards cached in
ElementImagesnapshots, capturing a replacement before closing the old one momentarily hits 25 open handles and tripscontext-guard.js. Closing the replaced snapshot first keeps the live count capped at 24 and teardown returns it to 0. - Nine-argument partial redraws need geometry care. Earlier experiments (
/input-ideas/,/select-selection/) found that nine-argumentdrawElementImage()cuts an element's hit region to the last source rectangle drawn unlesspreserveElementGeometry: trueis honoured. The live check above reports what this browser does on the un-redrawn top half of a card. - Plain offscreen 2D caches trade memory for zero replay on clean cards. Holding 24 card-sized offscreen canvases roughly doubles canvas memory on the board (still under 12 MB on a phone at 2.625 scale), and needs
window.__gfx.retire()on teardown so removed buffers don't linger until garbage collection.