Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(desktop): picture-in-picture stops re-encoding an unchanged page #20070

Mergedjuliusmarmingemerged 10 commits intomain fromrefs/pull/20070/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

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

Picture-in-picture main-process time per capture tick, real Electron 44.4.5 on a P3 MacBook Pro display, median of 40 ticks. Static page: 13.83 ms on main, 2.85 ms with this PR. Page changing every tick: 10.99 ms on main, 11.54 ms with this PR.

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 (toJPEG plus 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 typecheck in 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 ToSkColorSpace and Skia's SkColorSpaceXformSteps, 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


Devin Review

Fixes #19163

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. juliusmarminge added labelsmacroscope-review

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      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 b8f0f32. Prior analysis still applies.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. 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 main measurement shown below.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded20.9 KiB20.9 KiB0 B (0.0%)29.3 KiB✅
      CodexLive turn messages220 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.2 KiB21.2 KiB0 B (0.0%)29.3 KiB✅
      ClaudeLive turn messages220 (0.0%)8✅

      Baseline: eef0e4e · PR result: ecdd4eb · Source CI: failure

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

    5. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      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_status to false in 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.ts
      • apps/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.ts
      The 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.ts
      The 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

      Suggested reviewers: maria-rcks

      Merge Risk: ⚪ Minimal · up to ecdd4

      No confirmed merge-blocking issue remains. The PiP optimization is ready for normal merge checks.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Title checkPassedThe title clearly and concisely describes the main performance improvement: skipping re-encoding for unchanged picture-in-picture pages.
      Description checkPassedThe 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
      Comment `@coderabbitai help` to get the list of available commands.
    6. juliusmarminge merged this3962ae8

    7. juliusmarminge closed this

Sign in to join this conversation.