Skip to content

fix(rrweb): Skip unchanged and hidden canvases when sampling - #359

Merged
msonnb merged 3 commits into
sentry-v2from
ms/canvas-skip-unchanged
Nov 13, 2026
Merged

msonnb merged 3 commits into
sentry-v2from
ms/canvas-skip-unchanged

Conversation

@msonnb

@msonnb msonnb commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

With canvas sampling (what Sentry's replayCanvasIntegration uses), CanvasManager copies every canvas with createImageBitmap and 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. Manual snapshot() 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.width without a redraw clears the canvas without being detected.

🤖 Generated with Claude Code

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]>
@msonnb
msonnb marked this pull request as ready for review November 10, 2026 10:56
@msonnb
msonnb requested a review from logaretm November 10, 2026 10:56

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

@logaretm logaretm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: original.apply(this, arguments) avoids allocating an args array per call on this hot path.

Comment thread packages/rrweb/test/integration.test.ts Outdated
// rely on the structural check above.
});

it('should only snapshot sampled canvases that are shown and were drawn to', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@msonnb

msonnb commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

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.

@logaretm logaretm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

@msonnb
msonnb merged commit 3e057b3 into sentry-v2 Nov 13, 2026
21 checks passed
@msonnb
msonnb deleted the ms/canvas-skip-unchanged branch November 13, 2026 08:43
@linear-code

linear-code Bot commented Nov 13, 2026

Copy link
Copy Markdown

JS-4092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants