Repository navigation
fix(rrweb): Skip unchanged and hidden canvases when sampling - #359
Conversation
With canvas sampling, every canvas on the page was copied with `createImageBitmap` and encoded in the worker on each sample, even when it had not changed or was not displayed. Track draws on 2D contexts and skip canvases that were not drawn to or resized since their last snapshot, and skip canvases that are not rendered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f1a0a93. Configure here.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
logaretm
left a comment
There was a problem hiding this comment.
One thing we need to keep in mind is that the canvas APIs are usually a hot path in the user-code/library, so we need to have as little overhead as possible. Think thousand of calls per render op if not more.
So my comments usually wouldn't be raised in other cases, but in this one, it is worth a consideration. LMK what you think.
| return; | ||
| } | ||
|
|
||
| if (!canvasElement) { |
There was a problem hiding this comment.
This also runs for a manual snapshot()calls. Manual mode never samples again on its own, so a canvas that's hidden at that moment and shown later stays stale until the next manual call.
So should we skip this for manual mode?
// initCanvasFPSObserver
this.takeSnapshot(timestamp, undefined, /* skipUnchanged */ true);
// takeSnapshot
if (skipUnchanged) { /* checkVisibility + dirty/size check */ }| ): listenerHandler { | ||
| const handlers: listenerHandler[] = []; | ||
| const prototype = win.CanvasRenderingContext2D.prototype; | ||
| for (const prop of Object.getOwnPropertyNames(prototype)) { |
There was a problem hiding this comment.
With overhead in mind, it seems we can have a couple of optimizations here:
- We can skip any non-drawing APIs that wouldn't change the canvas like
getImageData. - Wrapping every function adds a caller overhead.
So maybe by using a denylist we can address both, that way we don't patch read-only functions.
const NON_DRAWING = new Set([
// path building, pixels only change on fill/stroke
'beginPath', 'closePath', 'moveTo', 'lineTo', 'arc', 'arcTo', 'ellipse',
'bezierCurveTo', 'quadraticCurveTo', 'rect', 'roundRect',
// reads / factories
'getImageData', 'measureText', 'isPointInPath', 'isPointInStroke',
'getTransform', 'getLineDash', 'getContextAttributes', 'isContextLost',
'createLinearGradient', 'createRadialGradient', 'createConicGradient',
'createPattern', 'createImageData',
]);| if (this.canvas) { | ||
| onDraw(this.canvas); | ||
| } | ||
| return original.apply(this, args); |
There was a problem hiding this comment.
nit: original.apply(this, arguments) avoids allocating an args array per call on this hot path.
| // rely on the structural check above. | ||
| }); | ||
|
|
||
| it('should only snapshot sampled canvases that are shown and were drawn to', async () => { |
There was a problem hiding this comment.
nit: the agent raised these uncovered cases:
- resize without a draw still snapshots
- hidden canvas redrawn, then shown, gets snapshotted
- untracked (WebGL) canvas still snapshots every sample
- Only skip hidden and unchanged canvases in the sampling loop, so a manual `snapshot()` still captures hidden canvases. - Leave non-drawing 2D context methods (path building, transforms, reads) unpatched, and skip `onDraw` for repeated draws to the same canvas. - Pass `arguments` instead of rest args to avoid an allocation per call. - Cover resizes without a draw, canvases redrawn while hidden, untracked WebGL canvases, and manual snapshots in tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, good point about the perf concerns. Did the suggested changes in eae9d63 and also added a small cache of the last drawn-to canvas to save some map lookups. |

With canvas sampling (what Sentry's
replayCanvasIntegrationuses),CanvasManagercopies every canvas withcreateImageBitmapand WebP-encodes it in the worker on every sample, only to discard the result when it matches the last one. Because on-error replays keep a rolling buffer, this costs CPU in every session; with a static waveform canvas open, Safari became sluggish (getsentry/sentry-javascript#26232).This tracks draws on 2D contexts with a cheap patch and skips canvases that were neither drawn to nor resized since their last snapshot, and canvases where
checkVisibility()is false. Because canvas calls are a hot path, the patch leaves methods that don't change pixels (path building, transforms, reads) unwrapped, doesn't serialize arguments, and does a single comparison for repeated draws to the same canvas. Canvases we can't track (WebGL, or a 2D context drawn to before recording started) are snapshotted as before. Manualsnapshot()calls are unaffected, since nothing samples again after them. Unchanged canvases were already not re-sent, so recorded output stays the same; full snapshots still serialize canvas content.Measured with the issue's repro in Chromium over 10s: a static canvas went from 19 bitmap copies to 1, a hidden one from 20 to 0, and a canvas redrawn every second still records all 10 frames.
Known gap:
canvas.width = canvas.widthwithout a redraw clears the canvas without being detected.🤖 Generated with Claude Code