perf(desktop): picture-in-picture stops re-encoding an unchanged page #20070
main fromrefs/pull/20070/head·merged by juliusmarminge View on GitHubPicture-in-picture no longer re-encodes a page that has not changed. On a P3 MacBook Pro display, a static page costs 2.9 ms of main-process time per capture tick instead of 13.8 ms. A page that changes every tick costs about the same as before.

Problem#
While a preview tab is in picture-in-picture, capturePreviewFrame runs every 84 ms. It called image.toJPEG(80) and only afterwards compared the encoded bytes with the previous frame, so a static page still paid a synchronous JPEG encode in the Electron main process on every tick.
Change#
capturePreviewFrame now fingerprints the captured bitmap first (${width}x${height}:${crc32(bitmap)}) and returns early when it matches the last delivered frame. toJPEG, the base64 string and the IPC send run only for a frame that changed. As before, the fingerprint is stored only after a successful send, so a failed delivery is retried on the next tick. It is cleared when picture-in-picture attaches or detaches, or when its renderer reloads.
The bitmap is read with an invalid target color space. Since Electron 43, toBitmap() converts to sRGB by default. Captures on P3 Macs are P3, so that conversion alone took about 65 ms per tick at 2560x1536, which is more than the JPEG encode it was meant to skip. The original version of this PR measured only Windows and Linux, where captures are already sRGB, so this didn't show up. Electron's documented "source" value still converts in 44.4.5. An invalid color space has no Skia color space, so readPixels copies the pixels without a transform on every platform. The fingerprint only compares frames with each other, so color accuracy does not matter. If toBitmap throws or returns an empty buffer, the frame has no fingerprint and is always delivered, so picture-in-picture cannot freeze.
Scope and approval#
Triaged on #19163. The triage reply asked for exactly this: compare the bitmap or a cheap hash before encoding, skip toJPEG, base64 and the IPC send for an unchanged frame, and real-app numbers.
Verification#
- Measured above in real Electron 44.4.5 on this Mac (both displays report P3 primaries, 10-bit). Each tick ran main's work (
toJPEGplus compare) and this PR's work (fingerprint, then encode only if changed) on the same capture.- Before the color-space fix, this branch cost 49.8 ms (static) and 48.4 ms (changing), against main's 13.8 ms and 11.0 ms.
- At an 800x600 window: main 9.0 / 6.8 ms, this PR 1.5 / 7.4 ms.
Manager.test.ts: an unchanged page is captured but not re-encoded or re-sent, a changed page is, and an unreadable bitmap is always delivered. The existing retry and reload tests guard where the fingerprint is stored and cleared. 102/102 pass.vp run typecheckin apps/desktop, targeted lint and format: clean.- Two independent review rounds (Claude Opus 5.5 and GPT-6.1 Sol). Round 2 traced the invalid color space through Electron's converter, Chromium's
ToSkColorSpaceand Skia'sSkColorSpaceXformSteps, none of which is platform-specific. - Not run in Electron on Windows or Linux in this round; the contributor's earlier Windows GPU numbers are in #19193. The no-transform behavior of an invalid color space is how Electron behaves today, not a documented contract, so it should be re-measured on the next Electron upgrade.
Original work by @bompus in #19193, taken over and finished by maintainers.
Claude Opus 5.5 and Claude Fable 5.1 in Claude Code, with review by GPT-6.1 Sol in Codex.
🤖 Generated with Claude Code
Fixes #19163
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
juliusmarminge added labelsmacroscope-review
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
ℹ️ The exact PR base did not have a successful artifact. Baseline uses the latest successful
mainmeasurement shown below.Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 20.9 KiB 20.9 KiB 0 B (0.0%) 29.3 KiB ✅ Codex Live turn messages 2 2 0 (0.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.2 KiB 21.2 KiB 0 B (0.0%) 29.3 KiB ✅ Claude Live turn messages 2 2 0 (0.0%) 8 ✅ Baseline:
eef0e4e· PR result:ecdd4eb· Source CI: failureScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 108.5 KiB
- Claude decoded thread snapshot: 108.8 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
Important
Review skipped#
Review was skipped as selected files did not have any reviewable changes.
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Team
- Run ID:
8be50c8d-1927-4999-a825-038acafdf18e
📥 Commits
-
Reviewing files that changed from the base of the PR and between ecdd4eba8ff42ef9bb2d2f2072490cdd0a079427 and b8f0f323f8d42edcdd8affecef7c487d95a2ba88.
You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- 🔍 Trigger review
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Team
- Run ID:
1f1b92a2-5460-4b1b-ae5a-0a5de8b5dac1
📥 Commits
-
Reviewing files that changed from the base of the PR and between 9d26a7986c3fafa4e2cfe50f32ad8cf725081ac4 and ecdd4eba8ff42ef9bb2d2f2072490cdd0a079427.
📒 Files selected for processing: 2
-
apps/desktop/src/preview/Manager.test.tsapps/desktop/src/preview/Manager.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
-
📝 Walkthrough
-
Picture-in-picture capture now fingerprints bitmap pixels before JPEG encoding. Identical readable frames skip encoding and delivery. Changed or unreadable frames are encoded and delivered.
Changes: PiP frame deduplication
Layer / File(s) Summary Bitmap fingerprint calculation
apps/desktop/src/preview/Manager.tsThe bitmap fingerprint combines image dimensions with a CRC32 checksum. It returns null when bitmap conversion fails or returns no bytes. PiP frame selection and lifecycle
apps/desktop/src/preview/Manager.ts,apps/desktop/src/preview/Manager.test.tsThe PiP session stores and resets the fingerprint. The capture path skips JPEG encoding and delivery for identical readable frames. Tests cover identical, changed, and unreadable pixels. Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Refactor
Possibly related PRs
- pingdotgg/t3code#19193: Changes PiP frame deduplication in
PreviewManagerusing raw bitmap fingerprints.
Suggested reviewers:
maria-rcks - pingdotgg/t3code#19193: Changes PiP frame deduplication in
Merge Risk: ⚪ Minimal · up to
ecdd4-
No confirmed merge-blocking issue remains. The PiP optimization is ready for normal merge checks.
Pre-merge checks:
4-
Check name Status Explanation Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Title check The title clearly and concisely describes the main performance improvement: skipping re-encoding for unchanged picture-in-picture pages. Description check The description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the implementation, approval context, test coverage, performance results, limitations,…
✨ Finishing Touches: 💡 1
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
🛠️ Fix failing CI checks: 💡
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
juliusmarminge merged this3962ae8
juliusmarminge closed this
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR not approvable — This production change adds fingerprint-based gating to the picture-in-picture capture pipeline, skipping JPEG encoding and IPC delivery for unchanged frames. The scope and tests are focused, but the new data-dependent suppression of existing work warrants human review.
No code changes detected at
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).b8f0f32. Prior analysis still applies.