Skip to content
Source
Sign inSign up
pingdotgg/t3code

Picture-in-picture encodes a full JPEG about 10 times a second even when the page is unchanged: 5-10 ms of main-process time per frame #19163

Closedbompus(GitHub profile, opens in a new tab) opened this issue 2 comments

What happened

While a preview tab is in picture-in-picture, the desktop main process runs a capture loop: it sleeps 84 ms, calls webContents.capturePage(), then image.toJPEG(80), and only afterwards compares the encoded buffer with the previous frame (lastPictureInPictureFrame.equals(encoded)). A static page therefore pays the full JPEG encode on every tick, about 10 times a second (84 ms sleep plus capture plus encode), and toJPEG is synchronous, so it blocks the main process for its whole duration. The main process is also what answers the renderer's synchronous IPC calls, so those wait behind each encode.

Measured with real Electron 44.4.5 (Chromium 152) on a static, text-heavy page, 60 frames per size at the loop's 84 ms cadence, three runs, median per frame:

Captured imagecapturePagetoJPEG(80) (blocks main)JPEG size
1280 x 7733.1-4.3 ms4.8-5.3 msabout 425 KB
1919 x 10527.7-8.9 ms10.0-10.7 msabout 907 KB

Each tick takes the 84 ms sleep plus the capture and the encode, about 93 ms at 1280 x 773 and about 102 ms at 1919 x 1052, so the encode blocks the main process for about 5% of the time at the smaller size and about 10% at the larger one, per picture-in-picture tab, for a page that is not changing. Taking the bitmap and comparing it with the previous bitmap costs 0.3-0.8 ms (1280 x 773) and 0.5-0.8 ms plus 0.3 ms (1919 x 1052), roughly a tenth of the encode, so the encode can be skipped on unchanged frames. The capture itself (3-9 ms) still happens each tick, so this removes the encode, not the whole loop.

These were taken under xvfb with --disable-gpu, so Chromium rendered in software, at scale factor 1, with the host busy (load average 2.7-5.4). A real GPU-composited window and a Retina display (2x pixels) will differ, in the encoder cost most likely upward. I have not measured the app itself, only the same two calls the loop makes.

Diagnosis

  • apps/desktop/src/preview/Manager.ts:133: PICTURE_IN_PICTURE_FRAME_INTERVAL_MS = Math.ceil(1_000 / 12).
  • Manager.ts:2587-2599: image.toJPEG(PICTURE_IN_PICTURE_JPEG_QUALITY), then frameSession.lastPictureInPictureFrame?.equals(encoded). The unchanged-frame check runs after the encode.
  • Manager.ts:2689-2700: startFrameCapture sleeps and captures in a loop for as long as the picture-in-picture consumer is attached.

Steps to reproduce

  1. Open a preview tab and put it in picture-in-picture on a page that does not change.
  2. Watch main-process CPU, or profile the main process: NativeImage::toJPEG runs every 84 ms.
  3. Or run the isolated measurement described under Evidence.

Version

0.0.45. Manager.ts is unchanged on upstream/main at c616e16c8f (checked 2026-10-13).

Environment

Linux (WSL2), Electron 44.4.5 under xvfb (--no-sandbox --disable-dev-shm-usage --disable-gpu), static generated page at 1280 x 800 and 1920 x 1080 window sizes, three runs, nice -n 10. No private data.

Evidence

run1 1280x800  capture 4.1 toJPEG 5.2 max 7.8  toBitmap 0.3 bitmap-equals 0.1
run1 1920x1080 capture 8.9 toJPEG 10.7 max 16.0 toBitmap 0.8 bitmap-equals 0.3
run2 1280x800  capture 4.3 toJPEG 5.3 max 7.2  toBitmap 0.6 bitmap-equals 0.2
run2 1920x1080 capture 7.7 toJPEG 10.0 max 24.3 toBitmap 0.5 bitmap-equals 0.3
run3 1280x800  capture 3.1 toJPEG 4.8 max 5.9  toBitmap 0.2 bitmap-equals 0.1
run3 1920x1080 capture 8.8 toJPEG 10.5 max 14.5 toBitmap 0.7 bitmap-equals 0.3

All values in ms, median of 60 frames per size. The script creates one hidden `BrowserWindow`, loads a static page, and per tick times `await wc.capturePage()`, then `img.toJPEG(80)`, then `img.toBitmap()` plus a `Buffer.equals` against the previous bitmap, waiting 84 ms between ticks. Half-size resize plus encode (`img.resize({ width: w / 2 }).toJPEG(80)`) took 3.8-4.1 ms and 7.3-7.8 ms, so encoding at the picture-in-picture window's own size is a smaller saving than skipping unchanged frames.

#4397 (merged) added background capture and picture-in-picture. #17242 (open) changes how fresh pixels are captured from background previews, in the same capture code, so a change here needs to be coordinated with it. #14730 (open) is about inactive previews rendering with the panel closed, not this loop. No existing issue or PR mentions toJPEG or encoding unchanged picture-in-picture frames.

Fix applied or workaround

None applied. I can open a pull request for this once triage confirms the direction, and #17242's author should weigh in because it touches the same loop. It is not written yet; the change I would measure compares the captured bitmap with the previous one and skips toJPEG, the base64 string and the IPC send when nothing changed. I would measure it in the real app on a GPU-composited window before claiming a gain.

Filed by

claude (sonnet-5-5) in T3 Code. The lead came from Sol (gpt-6.1-sol, Codex); I measured the real Electron calls myself.

  1. bompus(GitHub profile, opens in a new tab) added labelsvia-triage

  2. Note

    Grok responding on behalf of Julius.

    I'm treating this as a performance enhancement, not a bug. Picture-in-picture works correctly. It just does wasted work on frames that haven't changed.

    I confirmed the order on main. In apps/desktop/src/preview/Manager.ts, capturePreviewFrame runs image.toJPEG(80) first and only then checks lastPictureInPictureFrame?.equals(encoded). startFrameCapture repeats this every PICTURE_IN_PICTURE_FRAME_INTERVAL_MS (84 ms) while picture-in-picture is attached. So a static page gets encoded on every tick.

    The 5–10 ms/frame figures came from an isolated script under xvfb with --disable-gpu, not from the app. I'm not treating them as the size of the gain yet. That's also why this isn't marked accepted.

    If you open a PR, it needs to:

    • Compare the bitmap (or a cheap hash of it) before encoding, and skip toJPEG, the base64 and the IPC send when nothing changed.
    • Include before/after main-process numbers from the real desktop app on a GPU-composited window, ideally on a 2x display.
    • Coordinate with #17242 (@Quicksaver), which changes the same capturePreviewFrame path. Rebase on it, or agree on an order, so the two don't conflict.
  3. juliusmarminge added labelsenhancement

  4. juliusmarminge added labelsperformance

  5. Real-app GPU-composited numbers for the picture-in-picture change are now in the PR #19193 description. Windows, full desktop app built from the PR base and head, Electron 44.4.5, NVIDIA RTX 5070 with GPU compositing confirmed enabled, 5 alternating runs per cell: a static page costs 6.72 ms per capture tick before and 1.82 ms after at 1x (3.7x less), and 23.92 ms before and 6.69 ms after at a forced 2x scale (3.6x less). A page that changes every tick costs about 13% more at 1x (6.05 ms before, 6.81 ms after), because each tick now fingerprints the bitmap, and shows no clear change at forced 2x. A real Windows 200% display-scale setting was not run; 2x was forced with --force-device-scale-factor=2.

  6. juliusmarminge closed this

Sign in to join this conversation.