Skip to content

Keep the encoder quality cap for a small change to a sharpened tile, so typing is not 3 ms slower on the shipped encoder (851-4476) - #1484

Merged
pondorasti merged 5 commits into
mainfrom
alexandru/851-4476-screen-sharing-typings-worst-cases-are
Nov 27, 2026
Merged

pondorasti merged 5 commits into
mainfrom
alexandru/851-4476-screen-sharing-typings-worst-cases-are

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes 851-4476

What was wrong

On the encoder the app ships with (VCP behind the rate switch), a keystroke at normal typing speed took about 4 ms longer from capture to ready on the viewer than on the fallback encoder (17 ms against 13). After each keystroke the host runs a sharpening pass with the session's quality cap lowered (QP 6), and the next keystroke set it back (QP 28). The call that moves the cap is cheap (0.4 ms), but the first frame a session encodes after the cap moved takes about 3 ms longer (lab runs 45725747447 and 45726853454 on tamale). At a small display offset between the two Macs that cost a whole refresh (weekly run 44473660542: 36 against 20 ms median).

What changes

  • A small change to a tile whose last encode was a sharpening pass is encoded at the cap the session already has. Large changes, keyframes, reference refreshes and the second change in a row go back to the normal cap, so a run of changes is at the normal cap from its second frame, as before.
  • "Small" is measured by the host on the pixels: the sum over a tile's rows of the span from the first changed pixel to the last, at most 16,384 pixels (a 128×128 square, 0.8% of a 4K tile). Only tiles that were sharpened last are measured; every other tile still stops at its first difference.
  • Bitrate pressure wins: the low cap exists only while the encoder's bitrate is at least 20.8 Mbit/s. Below that every change is at the normal cap, and below 8 Mbit/s the rate switch has moved to VideoToolbox, which has no such cap. The rate switch, the bitrate governor, admission and recovery are not touched.
  • New host metrics: changeEncoded (a change's capture callback to the encoder's output), tilesChangedSmall, changesKeptSharp, encoderQPCapChanges. The host lab takes small=<pixels> (0 turns the rule off).

Threshold and before/after on the mini (host only)

Lab run 45736432148 on tamale: shipped encoder, 4K virtual display, 20 s per run, each scene run without the rule, with it (16,384), and without it again. Typing was played twice from the start.

Scene Rule Updates/s kbit/s Bytes per changed tile, mean (median) Encode p50 / p95 ms Capture to encoded p50 / p95 ms Cap changes
Typing off 17.0 63 320 (139) 5.7 / 8.5 8.7 / 12.2 287
Typing on 16.9 69 453 (146) 5.0 / 5.2 8.7 / 9.5 8
Typing off 17.0 87 644 (138) 7.4 / 8.5 8.7 / 12.2 285
Typing, again off 16.8 63 321 (138) 7.4 / 8.5 8.7 / 12.3 286
Typing, again on 16.9 70 455 (146) 5.0 / 5.1 8.7 / 9.5 8
Typing, again off 16.9 86 633 (135) 7.4 / 8.5 8.8 / 12.3 289
Idle cursor off 5.9 603 3,995 (133) 5.9 / 8.2 13.4 / 18.6 480
Idle cursor on 6.1 575 4,595 (185) 4.3 / 7.2 11.6 / 14.1 8
Idle cursor off 6.0 573 4,363 (138) 6.2 / 8.2 14.5 / 18.8 476
Scroll off 57.1 11,857 6,732 (5,214) 7.1 / 8.9 10.2 / 13.0 0
Scroll on 57.1 11,846 6,731 (5,218) 7.0 / 8.9 10.2 / 13.0 0
Scroll off 57.1 11,850 6,727 (5,230) 7.1 / 9.0 9.8 / 13.0 0
Window drag off 57.0 10,471 6,498 (4,290) 6.0 / 8.9 10.0 / 13.4 12
Window drag on 57.1 10,526 6,526 (4,344) 6.0 / 8.9 10.0 / 13.3 12
Window drag off 57.3 10,607 6,553 (4,427) 6.0 / 8.9 10.0 / 13.3 12
Video off 57.3 43,348 24,259 (23,246) 7.7 / 9.9 11.1 / 14.4 0
Video on 57.2 43,372 24,302 (23,438) 7.7 / 10.2 10.9 / 14.3 0
Video off 57.2 43,812 24,278 (23,361) 7.6 / 10.0 11.2 / 14.5 1
Test cards off 2.6 7,428 182,553 4.9 / 11.3 10.4 / 40.2 62
Test cards on 2.6 7,387 181,903 4.9 / 11.4 7.6 / 48.4 60
Test cards off 2.6 7,014 146,823 5.0 / 11.9 7.8 / 51.3 52

Updates/s is captures a second; kbit/s leaves out the first keyframes.

  • Typing: the slow keystrokes (the ones that follow a sharpening pass, the worst 5% here) go from 12.2 to 9.5 ms. The median does not move because half of this scene's keystrokes are in fast bursts that never switched. Bitrate sits between the two baselines, which drift with the scene (63 early, 87 late).
  • Idle cursor (a long run of small changes): 2 to 3 ms faster, bitrate inside the two baselines (603 and 573 against 575).
  • Scroll, window drag, video: no tile was small (4 in window drag), nothing moved.
  • Test cards: a card change is large in every tile, so it takes the old path. Seven cards in 20 s is too few for its timings to mean much; bytes are level.
  • Cost: measuring a sharpened tile to its end adds 0.1 ms (typing) to 0.8 ms (idle cursor) to the pixel comparison of that capture. It is inside the numbers above.

Threshold choice (lab runs 45733097776 and 45734977598, an earlier build that measured every tile): at 4,096 pixels the bench's typing changes were almost never small (22 of 626 tiles; the barcode alone spans more), at 16,384 nearly all were (671 of 684; idle cursor 519 of 526), and 65,536 added nothing on these scenes while allowing larger sharp frames. So 16,384. That earlier build also showed why only sharpened tiles are measured: measuring every changed tile cost window drag 0.4 ms per capture.

Two Macs

Run 45737633389: Apple High Performance, the shipped encoder with the rule and the shipped encoder without it, alternating in one dispatch (from the throwaway branch alexandru/851-4476-typing-lab-t2, which adds a lab system that turns the rule off). 4K virtual display, good network, 2 repetitions of 30 s, 22 runs, none skipped, display offset between the minis 5.2 ms. Image age in ms, repetition 1 / repetition 2:

Scene Metric Apple With the rule Without the rule
Typing median 21.9 / 21.9 21.9 / 21.9 38.5 / 38.5
Typing worst 5% 21.9 / 38.5 38.5 / 38.5 38.5 / 38.5
Typing updates/s 13.4 / 13.5 13.4 / 13.4 13.4 / 13.5
Typing Mbit/s, median second 0.24 / 0.30 0.15 / 0.16 0.15 / 0.16
Typing host CPU %, median 19 / 21 16 / 17 25 / 25
Scroll median 38.5 / 38.5 38.5 / 38.5 38.5 / 38.6
Scroll updates/s 60.0 / 60.0 57.4 / 57.4 57.5 / 57.4
Window drag median 38.6 / 38.6 38.6 / 38.7 38.6 / 38.7
Window drag updates/s 59.7 / 60.0 57.3 / 57.4 57.4 / 57.3
  • Typing's median is level with Apple with the rule and one refresh behind without it, at this display offset. From the per-frame records: a keystroke at normal speed that was captured on time took 13.8 ms from capture to ready on the viewer with the rule against 17.5 without (wait before encoding 5.4 against 8.3, encode 5.7 against 6.7, decode 3.1 against 4.1), so it was ready 0.6 ms after the host showed it instead of 4.2, and 322 of 328 made the refresh Apple makes against 48 of 363. Fast bursts were the same in both (13.0 and 13.3 ms).
  • Typing's worst 5% does not move. Those are the keystrokes ScreenCaptureKit hands over late (about 37% of them, 300 of 778 here), which is the capture cadence ticket (851-4475). They are 3 ms faster with the rule (15.0 against 17.9 ms capture to ready) but still land on the later refresh.
  • Scroll and window drag are the same with and without (updates per second too; the 57 against 60 is capture cadence).
  • Bytes: the host sent 5.95 and 5.98 MB over a whole typing session with the rule against 6.77 and 6.32 without. The report's total megabytes for typing are higher with the rule (1.2 against 0.6) because croissant's network interface received an unrelated burst for one or two seconds inside the measured window in both of those runs; the stream itself carried only the usual 10 keystrokes and 20 sharpening frames in those seconds, and the same bursts show in runs without the rule (this dispatch and the capture cadence agent's run 45729541410) outside the window.
  • This run used the fix before the review fixes below (commit 62c9378); those do not change what the shipped encoder does at this bitrate.

Sharpness (test cards on two Macs, viewer on a 4K virtual display so the picture is 1:1): run 45741611237, 2 repetitions each. With and without the rule every repetition gave the same: luma PSNR 61.2 dB (53.3 on the text), SSIM 0.9999, colour PSNR 57.2 dB, ΔE00 0.33 mean. A card change is large, so it takes the unchanged path.

Confirmation on the final code (run 45925610081, attempt 2; the fix with the review changes, b628ffe; typing only, 3 repetitions, display offset 10.3 ms):

Typing Apple With the rule Without the rule
Image age median, rep 1 / 2 / 3 27.0 / 26.9 / 26.9 27.0 / 26.9 / 26.9 27.0 / 26.9 / 26.9
Image age worst 5% 27.0 / 27.0 / 26.9 43.6 / 43.6 / 43.6 43.6 / 43.6 / 43.6
On-time keystroke at normal speed, capture to ready (median) 13.6 ms 17.2 ms
Ready after the host shows it (median) 0.3 ms 4.0 ms
Host bytes sent per session 6.15 to 6.25 MB 5.95 to 6.11 MB
Host CPU %, median 19 to 26 16 25
  • At this display offset the viewer refresh is late enough that both land on the same refresh, so image age is identical. The rule gains a refresh only at offsets where a keystroke ready 4 ms after the host misses the latch and one ready at 0.3 ms makes it: 5.2 ms in run 45737633389 above (21.9 against 38.5), and 3.1 ms in the 19 Nov weekly.
  • The 3.6 ms the rule saves reproduces exactly on the final code (13.8 against 17.5 in the earlier run).
  • Bytes: 2 to 4% more with the rule here, about 3 kbit/s; the earlier run had it the other way (5.95 to 5.98 against 6.32 to 6.77 MB). Level within run-to-run noise.
  • The worst 5% (43.6) is the late captures, unchanged by this PR; the capture cadence work (851-4475) removes them (its run 45925697606 at cap 480: 27.4 / 27.4).
  • Attempt 1 of this run measured but lost the host's half: tamale's artifact upload timed out reaching GitHub (ETIMEDOUT), so the report had nothing to read. The viewer's half of attempt 1 gave the same stage times (15.2 against 20.1 ms from the viewer's frame records).

Tests

  • ScreenSharingVCPSmallChangeTests (hardware): the stack the app ships, built by ScreenSharingV2Encoders.make(ScreenSharingV2EncoderKind.configured(), …) on a manual clock and moved to the fast route. A small change after a sharpening pass leaves the cap alone (one cap change in the session); a large one sets it back (two). Fails without the fix (two cap changes for the small change).
  • ScreenSharingVCPEncoderStateTests: the cap for a small change after a sharpening pass, for a large change, another tile's small change, a refresh, the second change in a row, and after the bitrate fell below Apple's ladder. Two of them fail without the fix.
  • ScreenSharingV2TileComparerTests, ScreenSharingV2CaptureSlotTests, ScreenSharingV2HostSmallChangeTests: what counts as small, how long a waiting tile stays small, and that the host only calls a change small when its tile was sharpened last.

Checked by hand and not

  • Ran the whole ScreenSharingV2Tests bundle locally (154 tests without the hardware suites, plus the VCP hardware suites) on an M1 Pro.
  • Not checked in the running app: the change is in the host's encode path and has no UI. The lab and two-Mac runs drive the same host and encoder classes the app uses.
  • Not measured: the app's own window (the product viewer in the Mac app), the fallback encoder (it ignores the rule), a slow or lossy link on two Macs (the rule is off below 20.8 Mbit/s; covered by a unit test only), and a 120 Hz host.

Code review (medium) and what changed after it

Five findings, all fixed in b628ffe, each with a test where there is behaviour to test:

  1. The host measured a sharpened tile to its end even when the encoder in use cannot keep its cap (VideoToolbox, the slow route, a bitrate under 20.8 Mbit/s). Encoders now say so (keepsItsCapForSmallChanges) and the host measures only then.
  2. changeEncoded also timed refreshes of an old capture (a loss refresh after an idle screen would add seconds). It now leaves out refreshes.
  3. A two-plane capture counted a change in luma and again in chroma against one limit. Each plane is now held to the limit on its own.
  4. tilesChangedSmall and changesKeptSharp counted before the encode went through, and a refreshed tile counted as small. They count after, and a refreshed tile is not passed as small.
  5. One usage string was over the 120-column limit.

🤖 Generated with Claude Code

pondorasti and others added 5 commits November 25, 2026 17:44
…just sharpened (851-4476)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
… the new metrics (851-4476)

From the code review of #1484: the host no longer scans a sharpened tile to its end when the encoder in use
cannot keep its cap; each plane of a capture is held to the limit on its own; changeEncoded leaves out refreshes;
tilesChangedSmall and changesKeptSharp count only encodes that went through.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@pondorasti
pondorasti merged commit 67788a6 into main Nov 27, 2026
11 checks passed
@pondorasti
pondorasti deleted the alexandru/851-4476-screen-sharing-typings-worst-cases-are branch November 27, 2026 03:37
pondorasti added a commit that referenced this pull request Nov 27, 2026
…#1505)

#1484 added --small to every host-lab variant's arguments; this test still listed the old ones,
so main's checks-swift failed.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant