Skip to content

Studio: hold mid-scroll code block work on WebKit, fixing the #16865 scroll regression - #17149

Merged
danielhanchen merged 2 commits into
mainfrom
studio-fence-scroll-regression
Nov 9, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-fence-scroll-regression

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes a macOS WebKit scroll regression from #16865.

Problem

On macOS WebKit (Safari, the macOS app) #16865 barely colours code blocks mid-scroll, yet a fast wheel scroll over a long chat costs 2.3 to 3.7 s of frames over 50 ms (scroll plus settle) against 0.46 s before it, with frames up to 200 ms.

#16865 lets the fence queue run reaches, starts and worker answers while the reader scrolls. WebKit has no requestIdleCallback, so each of those steps falls back to a timer that lands inside the scroll. On WebKit the tokens come from the worker, so a block reached mid-scroll swaps its markup without colour. A mutation log of one scroll shows main inserting about 1,000 nodes inside code blocks from about 60 timer tasks (280 to 670 ms of script), against 25 to 45 attribute changes before #16865.

Bisect on macos-15 runners, each of #16865's pieces reverted one at a time on main (3 repeats, rotated): only reverting the queue's mid-scroll flag brings scroll plus settle back (1.2 s against pre 1.35 s and main 3.2 to 3.3 s). Reverting the cap, the code block action observers, pinning while streaming, the wrap observer, the scrolled-body flag or the header icon does not.

Fix

Keep #16865's mid-scroll path only where requestIdleCallback exists. Without it, fence work waits for the reader to stop scrolling, as before #16865. The cap, the header and the Chromium mid-scroll colour are unchanged.

Results

GitHub runners, 3 repeats per leg with the arm order rotated, every served page matched its build. Median (range).

macos-15 WebKit

pre-#16865 main main (A/A) this PR this PR (A/A)
scroll plus settle, frames over 50 ms 464 ms (163-877) 3727 ms (800-4912) 2327 ms (1153-2483) 236 ms (233-1177) 736 ms (188-990)
scroll longest frame 70 ms 208 ms 193 ms 43 ms 80 ms
mutations during the scroll 25 1262 to 1989 45 to 58
reopen to coloured 2257 ms 3016 ms 2947 ms 1257 ms 1466 ms
frames with an uncoloured block, fast scroll 92% 88% 89% 93% 93%
width change x4, frames over 50 ms 63 ms 471 ms 541 ms 345 ms 356 ms
typing key to frame, worst 24 ms 26 ms 28 ms 19 ms 19 ms

ubuntu-latest Chromium, CPU 4x: unchanged from main (the flag is on there), scroll plus settle 2.85 s against main 2.6 to 3.1 s.

Tests

  • Updated the capped-block source contract and added a WebKit paths test that pins the requestIdleCallback gate on both queue entries; it fails on main.
  • Fence and WebKit path tests pass (188), node scripts/typecheck.mjs --tests passes.

Not covered

On WebKit there is no requestIdleCallback, so each mid-scroll fence step
falls back to a timer that lands inside the scroll. The tokens there come
from the worker, so reaching a block mid-scroll swaps its markup without
colouring it, and scroll plus settle time on macOS WebKit rose from about
0.75 s to 4 to 5 s. Keep the mid-scroll path where idle callbacks exist
and let WebKit wait for the reader to stop, as before.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T14:51:22.885394Z 6712bff Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: 🔄 Running

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-11-08T14:50:23.706921Z 6712bff PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 6712bff6bd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit ae176c8 into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the studio-fence-scroll-regression branch November 9, 2026 13:06
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 9, 2026
…treaming (unslothai#17160)

Always-coloured code blocks while scrolling and streaming, now without
the WebKit scroll cost: the worker colours a block before its markup
swaps (every engine), and token colours go through shared classes so
WebKit can share their computed style.

## Update: token colours through shared classes (b86272f)

The remaining macOS WebKit cost of colouring mid-scroll was not
tokenising: each long frame carried 10 to 25 ms of JS and then 100 to
300 ms of style for a 700 to 1,700 node block, because every token span
held its colours inline and WebKit shares no computed style between
elements with an inline style (one 1,250-node block: 194 ms of style
inline, 60 ms with classes). `fence-token-classes.ts` writes one class
per declaration the two bundled themes can produce, once, before any
chat renders (WebKit restyles the whole document when a sheet changes
later); spans use those classes and keep inline style only for a value
outside the themes. Computed colour, style, weight and decoration match
the inline version on all 3,071 spans in light and dark (WebKit).

Clean runners (staging run 42635577083), main `ae176c8c18` twice (A/A),
arm order rotated, median (range):

| macos-15 WebKit | main | main (A/A) | before this update | this PR |
| --- | --- | --- | --- | --- |
| frames with an uncoloured block, fast scroll | 90% | 91% | 0% | 0% |
| scroll stop to all coloured | 2324 ms | 2480 ms | 198 ms | 78 ms
(58-97) |
| streaming frames with a plain line | 65% | 63% | 1% | 1% |
| scroll long tasks | 560 (342-777) | 639 (339-939) | 14920 | 656
(170-1141) |
| scroll plus settle long tasks | 1122 | 1184 | 14986 | 702 (170-1235) |
| scroll longest frame | 78 | 76 | 265 | 84 (59-109) |
| stream long tasks | 3346 | 4373 | 33690 | 494 |
| typing key to frame, worst | 30 ms | 34 ms | 32 ms | 22 ms |
| width change x4, frames over 50 ms | 366 | 398 | 658 | 619 |

ubuntu Chromium CPU 4x: uncoloured frames 0% (main 70-75%), scroll long
tasks 820 ms (main about 20 s), colour after stop 110 ms. Width change
remains about 1.6x main on macOS WebKit (inherited from the worker
path); that is the one open gap.

Follows unslothai#16865 (capped, scrollable chat code blocks with colour while
scrolling), now on main. unslothai#17149 (merged) holds unslothai#16865's mid-scroll fence
work on engines without `requestIdleCallback`; this branch runs it
mid-scroll on every engine again, because the worker now colours a block
before its markup swaps.

## Problem

Measured against unslothai#16865 and against main (its merge base) in a seeded
long chat with 180 code fences of mixed lengths (2 to about 450 lines)
in 12 languages:

- **Streaming.** About 70% of frames during a streamed reply show lines
that have arrived but are still plain, on main and on unslothai#16865 alike, in
Chromium and WebKit. A fence over 2,000 characters is answered with a
plain tail and re-coloured at most every 250 ms, so each new line waits
up to a quarter of a second (p95 lag 300 to 550 ms).
- **WebKit (Safari, the macOS app).** unslothai#16865 colours mid-scroll in
Chromium only. In WebKit 60 to 86% of frames during a fast wheel scroll
still show a plain block, and colour lands 4.4 to 6.3 s after the scroll
stops. WebKit has no `requestIdleCallback`, so every mid-scroll step of
the fence queue waited on a 100 ms timer, and the queue ran one fence
per step.
- **Main-thread cost in Chromium.** unslothai#16865 makes Chromium colour
mid-scroll by tokenizing each block on the main thread as it is reached.
A CPU profile of the scroll shows Shiki (the regex engine and the
TextMate scanner) as the largest JavaScript cost; at 4x CPU throttling a
fast scroll has frames of 4 to 10 s.

## Fix

- **Settled fences are tokenized by the worker on every engine.** The
worker that already serves reasoning blocks and long fences now takes
every settled chat fence while it answers (before: only WebKit and
fences over 20,000 characters). On WebKit even a short fence goes there,
since JavaScriptCore interprets lookbehind patterns. A short uncovered
tail (the fence that just streamed) still finishes on the main thread,
and if the worker is unavailable or stalls everything falls back to the
main thread exactly as before.
- **The worker is asked at once.** Posting the request no longer waits
in the quiet-work queue behind the reader; only applying the answer does
(and that step was already allowed mid-scroll).
- **Fences ahead of the reader are tokenized before they are reached.**
Speculation, which already pre-tokenizes unreached fences in the worker
and seeds the main-thread cache, no longer pauses for wheel or touch
scrolling (its main-thread part is posting and seeding) and no longer
waits for the main thread to warm a grammar once the worker is up. It is
bounded to fences within three viewports of the reader, so a long chat
does not pay for fences nobody scrolls to; a scroll looks again at most
every 200 ms.
- **A fence on screen with no tokens yet** is still tokenized in the
latch's own task so it never paints plain, but only within an 8 ms
budget per latch and never on WebKit; the rest take the worker's answer
a frame or two later.
- **The fence queue does more per step.** One run finishes several
queued fences while it stays inside 6 ms (each fence still commits on
its own and at most once per run), asks once per run whether a scroll, a
held pointer or a selection blocks it, and without `requestIdleCallback`
a mid-scroll step runs right after the frame instead of after a 100 ms
timer.
- **Streaming colours each line as it lands.** A growth of at most 1,024
characters past the last tokenized line is tokenized at once from the
cached grammar state. Only a longer one (a single live line that keeps
growing, such as minified JSON) keeps the throttled plain tail and the
sliced refresh.
- **Smaller worker replies.** Tokens with the same style share one style
object in the reply (the structured clone keeps shared references) and
the grammar state is not sent, roughly halving the clone cost per
result.

## Results

### On top of the scroll-regression fix (clean runners, run 2)

Arms: pre-unslothai#16865 `340b48c6f1`, main `997441a41c` twice (A/A), the fix
`6712bff6bd` twice (A/A), and this branch on the fix `886bc177b7`. 3
repeats per leg, arm order rotated, every served page matched its build.
Median (range).

**macos-15 WebKit**

| | pre-unslothai#16865 | main | main (A/A) | fix | fix (A/A) | this PR |
| --- | --- | --- | --- | --- | --- | --- |
| frames with an uncoloured block, fast scroll | 92% | 88% | 89% | 93% |
93% | 0% |
| scroll stop to all coloured | 1213 ms | 2482 ms | 2122 ms | 2037 ms |
2210 ms | 107 ms |
| streaming frames with a plain line | 75% | 73% | 73% | 73% | 74% | 1%
|
| reopen to coloured | 2257 ms | 3016 ms | 2947 ms | 1257 ms | 1466 ms |
1267 ms |
| scroll longest frame | 70 ms | 208 ms | 193 ms | 43 ms | 80 ms | 189
ms |
| scroll plus settle, frames over 50 ms | 464 ms (163-877) | 3727 ms
(800-4912) | 2327 ms (1153-2483) | 236 ms (233-1177) | 736 ms (188-990)
| 3489 ms (1091-4837) |
| width change x4, frames over 50 ms | 63 ms | 471 ms | 541 ms | 345 ms
| 356 ms | 635 ms |
| typing key to frame, worst | 24 ms | 26 ms | 28 ms | 19 ms | 19 ms |
29 ms |

**ubuntu-latest Chromium, CPU 4x**

| | pre-unslothai#16865 | main | main (A/A) | fix | fix (A/A) | this PR |
| --- | --- | --- | --- | --- | --- | --- |
| frames with an uncoloured block, fast scroll | 90% | 0% | 15% | 3% |
9% | 0% |
| streaming frames with a plain line | 71% | 70% | 68% | 70% | 69% | 0%
|
| scroll longest frame | 50 ms | 450 ms | 517 ms | 433 ms | 517 ms | 133
ms |
| scroll plus settle, long tasks | 60 ms | 2622 ms | 3118 ms | 2850 ms |
3330 ms | 957 ms |
| scroll script time (CDP) | 1544 ms | 8260 ms | 8737 ms | 8657 ms |
8760 ms | 3377 ms |
| typing key to frame, worst | 47 ms | 40 ms | 46 ms | 44 ms | 47 ms |
43 ms |

The fix does not touch Chromium (it has `requestIdleCallback`), so its
Chromium arms read as main. On macOS WebKit this branch buys full colour
mid-scroll and while streaming at about main's pre-fix scroll cost; on
Chromium at 4x it colours everything at about a third of main's scroll
cost, still above pre-unslothai#16865.

### Clean GitHub runners, run 1 (on main, before the fix)

Arms: pre-unslothai#16865 main `340b48c6f1`, current main `997441a41c` built
twice (A/A), and this branch on current main. One job per OS and repeat,
3 repeats with the arm order rotated, each arm's served page checked
against its own build. Median (range).

**ubuntu-latest Chromium**

| | pre-unslothai#16865 | main | main (A/A) | this PR |
| --- | --- | --- | --- | --- |
| Scroll frames with a plain block | 92% (92 to 92) | 0% (0 to 0) | 0%
(0 to 0) | 0% (0 to 0) |
| Scroll stop to all visible coloured | 707 ms (707 to 790) | 21 ms (20
to 21) | 42 ms (42 to 42) | 29 ms (27 to 36) |
| Streaming frames with a plain line | 73% (73 to 73) | 73% (73 to 74) |
73% (73 to 73) | 0% (0 to 0) |
| Streaming line lag, p95 | 233 ms (233 to 250) | 233 ms (233 to 267) |
250 ms (233 to 250) | 0 ms (0 to 0) |
| Longest scroll frame | 17 ms (17 to 17) | 17 ms (17 to 17) | 17 ms (17
to 33) | 17 ms (17 to 17) |
| Long tasks, scroll plus settle | 0 ms (0 to 0) | 0 ms (0 to 0) | 0 ms
(0 to 0) | 0 ms (0 to 0) |
| Long tasks, opening the chat | 55 ms (55 to 65) | 62 ms (59 to 71) |
64 ms (60 to 73) | 58 ms (52 to 64) |
| Worst keystroke after the scroll | 16 ms (16 to 17) | 16 ms (16 to 17)
| 15 ms (15 to 17) | 16 ms (15 to 16) |
| Four width changes, frames over 50 ms | 0 ms (0 to 0) | 0 ms (0 to 0)
| 0 ms (0 to 0) | 0 ms (0 to 0) |

**ubuntu-latest Chromium, CPU 4x**

| | pre-unslothai#16865 | main | main (A/A) | this PR |
| --- | --- | --- | --- | --- |
| Scroll frames with a plain block | 90% (89 to 91) | 0% (0 to 0) | 0%
(0 to 0) | 0% (0 to 0) |
| Scroll stop to all visible coloured | 952 ms (828 to 1152) | 89 ms (76
to 108) | 88 ms (84 to 142) | 81 ms (61 to 125) |
| Streaming frames with a plain line | 70% (70 to 71) | 69% (69 to 70) |
71% (70 to 71) | 0% (0 to 0) |
| Streaming line lag, p95 | 317 ms (317 to 383) | 317 ms (317 to 367) |
333 ms (317 to 367) | 0 ms (0 to 0) |
| Longest scroll frame | 67 ms (33 to 133) | 233 ms (217 to 2283) | 250
ms (233 to 2350) | 100 ms (83 to 117) |
| Long tasks, scroll plus settle | 67 ms (0 to 593) | 1868 ms (1601 to
5236) | 1845 ms (1499 to 5317) | 653 ms (266 to 734) |
| Long tasks, opening the chat | 708 ms (680 to 1011) | 1168 ms (918 to
1667) | 1076 ms (994 to 1685) | 1044 ms (860 to 1478) |
| Worst keystroke after the scroll | 41 ms (40 to 43) | 44 ms (42 to 44)
| 42 ms (42 to 46) | 39 ms (35 to 44) |
| Four width changes, frames over 50 ms | 50 ms (0 to 83) | 383 ms (333
to 450) | 367 ms (350 to 417) | 450 ms (216 to 600) |

**macos-15 WebKit**

| | pre-unslothai#16865 | main | main (A/A) | this PR |
| --- | --- | --- | --- | --- |
| Scroll frames with a plain block | 91% (90 to 91) | 84% (78 to 91) |
91% (78 to 91) | 0% (0 to 0) |
| Scroll stop to all visible coloured | 1327 ms (1169 to 1350) | 2891 ms
(2853 to 3428) | 3455 ms (2670 to 3711) | 142 ms (59 to 206) |
| Streaming frames with a plain line | 74% (73 to 75) | 75% (71 to 75) |
71% (70 to 75) | 1% (1 to 1) |
| Streaming line lag, p95 | 321 ms (295 to 328) | 314 ms (251 to 329) |
299 ms (245 to 300) | 0 ms (0 to 0) |
| Longest scroll frame | 91 ms (72 to 96) | 292 ms (205 to 301) | 277 ms
(175 to 358) | 295 ms (256 to 350) |
| Long tasks, scroll plus settle | 753 ms (702 to 1520) | 5583 ms (3851
to 7053) | 4339 ms (2934 to 5455) | 4960 ms (4358 to 6028) |
| Long tasks, opening the chat | 2480 ms (2222 to 2669) | 3177 ms (2788
to 3578) | 3164 ms (3022 to 3631) | 2877 ms (2752 to 2916) |
| Worst keystroke after the scroll | 43 ms (32 to 44) | 64 ms (34 to 69)
| 40 ms (32 to 47) | 58 ms (53 to 88) |
| Four width changes, frames over 50 ms | 197 ms (151 to 256) | 855 ms
(680 to 1215) | 784 ms (695 to 911) | 1069 ms (958 to 1069) |

**macos-15 Chromium**

| | pre-unslothai#16865 | main | main (A/A) | this PR |
| --- | --- | --- | --- | --- |
| Scroll frames with a plain block | 91% (91 to 92) | 0% (0 to 0) | 0%
(0 to 0) | 0% (0 to 0) |
| Scroll stop to all visible coloured | 1251 ms (1001 to 1538) | 81 ms
(65 to 85) | 94 ms (70 to 108) | 84 ms (74 to 134) |
| Streaming frames with a plain line | 68% (65 to 68) | 67% (64 to 68) |
66% (65 to 71) | 0% (0 to 0) |
| Streaming line lag, p95 | 317 ms (300 to 317) | 317 ms (300 to 317) |
300 ms (283 to 317) | 0 ms (0 to 0) |
| Longest scroll frame | 133 ms (133 to 133) | 150 ms (133 to 150) | 150
ms (133 to 150) | 133 ms (133 to 150) |
| Long tasks, scroll plus settle | 0 ms (0 to 0) | 110 ms (0 to 196) |
116 ms (86 to 501) | 0 ms (0 to 0) |
| Long tasks, opening the chat | 158 ms (67 to 324) | 295 ms (219 to
327) | 304 ms (136 to 328) | 254 ms (181 to 290) |
| Worst keystroke after the scroll | 99 ms (85 to 107) | 93 ms (84 to
96) | 106 ms (74 to 129) | 92 ms (82 to 107) |
| Four width changes, frames over 50 ms | 1200 ms (1033 to 1533) | 584
ms (567 to 1600) | 1616 ms (1383 to 1617) | 1483 ms (1233 to 1717) |

### Loaded development host (earlier, against unslothai#16865 before it merged)

Median of 3 repeats, arms interleaved per repeat (range in brackets).
main = the merge base of unslothai#16865. 60-turn chat, 180 fences, 1400x1000
viewport, 200 wheel ticks of 120 px every 8 ms from the bottom, then a
scripted stream into a 143-line Python fence. "Long tasks" are Long
Tasks API durations in Chromium and the sum of frames over 50 ms in
WebKit.

**Chromium**

| | main | unslothai#16865 | this PR |
| --- | --- | --- | --- |
| Frames with an uncoloured block in view, fast wheel scroll | 90% (90
to 91) | 0% (0 to 0) | 0% (0 to 0) |
| Scroll stop to every visible block coloured | 744 ms (720 to 817) | 73
ms (67 to 225) | 80 ms (65 to 119) |
| Streaming: frames with an arrived line still plain | 74% (68 to 75) |
72% (70 to 74) | 0% (0 to 0) |
| Streaming: line colour lag, p95 | 300 ms (283 to 333) | 283 ms (283 to
283) | 0 ms (0 to 0) |
| Open a long chat: mount to visible blocks coloured | 2318 ms (1623 to
2938) | 1991 ms (1869 to 3100) | 1708 ms (1496 to 1751) |
| Reopen it: mount to visible blocks coloured | 1031 ms (896 to 1566) |
1324 ms (1082 to 1391) | 774 ms (773 to 888) |
| Open: longest frame | 133 ms (117 to 217) | 150 ms (117 to 233) | 150
ms (117 to 167) |
| Open: time in long tasks | 420 ms (304 to 576) | 394 ms (375 to 899) |
452 ms (438 to 582) |
| Scroll: longest frame | 33 ms (17 to 733) | 167 ms (167 to 167) | 117
ms (67 to 250) |
| Scroll plus settle: time in long tasks | 0 ms (0 to 1221) | 736 ms
(489 to 1463) | 234 ms (185 to 880) |
| Streaming: longest frame | 67 ms (67 to 467) | 217 ms (100 to 683) |
83 ms (83 to 1417) |
| Streaming: time in long tasks | 57 ms (52 to 3157) | 293 ms (69 to
2689) | 72 ms (70 to 2731) |
| Typing: key to next frame, median | 12 ms (8 to 13) | 10 ms (10 to 13)
| 12 ms (9 to 13) |
| Typing: key to next frame, worst | 22 ms (16 to 61) | 56 ms (22 to 81)
| 40 ms (17 to 48) |

**Chromium, CPU throttled 4x**

| | main | unslothai#16865 | this PR |
| --- | --- | --- | --- |
| Frames with an uncoloured block in view, fast wheel scroll | 91% (90
to 91) | 90% (81 to 93) | 29% (22 to 69) |
| Scroll stop to every visible block coloured | 9321 ms (8753 to 10000)
| 4236 ms (3928 to 5647) | 986 ms (189 to 3531) |
| Streaming: frames with an arrived line still plain | 64% (64 to 67) |
61% (58 to 63) | 0% (0 to 0) |
| Streaming: line colour lag, p95 | 450 ms (400 to 517) | 467 ms (450 to
483) | 0 ms (0 to 0) |
| Open a long chat: mount to visible blocks coloured | 3030 ms (2996 to
4688) | 5276 ms (5174 to 6797) | 5312 ms (4518 to 7253) |
| Reopen it: mount to visible blocks coloured | 17 ms (12 to 3927) |
2448 ms (2330 to 2734) | 2456 ms (21 to 2836) |
| Open: longest frame | 717 ms (717 to 1133) | 783 ms (700 to 1083) |
833 ms (717 to 933) |
| Open: time in long tasks | 1094 ms (973 to 3471) | 2363 ms (2169 to
3094) | 1889 ms (1336 to 1904) |
| Scroll: longest frame | 400 ms (133 to 567) | 4633 ms (4633 to 5250) |
1217 ms (317 to 1300) |
| Scroll plus settle: time in long tasks | 8723 ms (7981 to 10670) |
16718 ms (15880 to 25452) | 6675 ms (5592 to 12675) |
| Streaming: longest frame | 483 ms (350 to 1100) | 400 ms (350 to 1183)
| 500 ms (433 to 783) |
| Streaming: time in long tasks | 2767 ms (2618 to 15238) | 4849 ms
(2432 to 15343) | 6317 ms (2561 to 6795) |
| Typing: key to next frame, median | 37 ms (36 to 41) | 36 ms (34 to
37) | 34 ms (34 to 35) |
| Typing: key to next frame, worst | 157 ms (98 to 194) | 106 ms (85 to
284) | 91 ms (65 to 96) |

**WebKit (Playwright, macOS user agent)**

| | main | unslothai#16865 | this PR |
| --- | --- | --- | --- |
| Frames with an uncoloured block in view, fast wheel scroll | 88% (88
to 90) | 89% (88 to 90) | 0% (0 to 0) |
| Scroll stop to every visible block coloured | 1647 ms (1624 to 1708) |
4324 ms (3088 to 5195) | 101 ms (72 to 114) |
| Streaming: frames with an arrived line still plain | 72% (72 to 73) |
66% (66 to 67) | 1% (1 to 2) |
| Streaming: line colour lag, p95 | 313 ms (303 to 374) | 250 ms (238 to
313) | 0 ms (0 to 0) |
| Open a long chat: mount to visible blocks coloured | 1554 ms (1328 to
3037) | 3850 ms (3663 to 4508) | 2596 ms (2084 to 2792) |
| Reopen it: mount to visible blocks coloured | 1308 ms (1221 to 1552) |
2703 ms (19 to 2730) | 1946 ms (1726 to 2246) |
| Open: longest frame | 362 ms (353 to 507) | 725 ms (685 to 1283) | 784
ms (655 to 854) |
| Open: time in long tasks | 1749 ms (1629 to 1999) | 2717 ms (2150 to
2738) | 3082 ms (2552 to 3374) |
| Scroll: longest frame | 141 ms (78 to 157) | 370 ms (359 to 438) | 374
ms (331 to 949) |
| Scroll plus settle: time in long tasks | 1641 ms (1391 to 2827) | 3110
ms (3083 to 3697) | 4473 ms (3850 to 11729) |
| Streaming: longest frame | 555 ms (507 to 666) | 1001 ms (886 to 1070)
| 706 ms (565 to 774) |
| Streaming: time in long tasks | 1345 ms (690 to 2804) | 2050 ms (1416
to 2521) | 1477 ms (1140 to 2128) |
| Typing: key to next frame, median | 16 ms (13 to 26) | 18 ms (14 to
22) | 15 ms (14 to 20) |
| Typing: key to next frame, worst | 1269 ms (1074 to 1563) | 2917 ms
(2569 to 3099) | 3970 ms (3767 to 5032) |

Two earlier interleaved runs of the same comparison (with an earlier
revision of this branch) agree on the colour rows: this branch at 0 to
1% uncoloured scroll frames in Chromium and WebKit unthrottled. At 4x
the figure is unstable on this host for both branches: this branch read
0 to 1%, 29% and 75% in different runs, unslothai#16865 0 to 26% and 90%; the
plain blocks are ones not yet reached (the reach step starves under
throttling plus host load), not ones waiting for tokens, 0% plain
streamed lines in Chromium and 1 to 3% in WebKit, and 0 ms streaming
lag. Their cost rows moved up to 2x between runs on the loaded host (see
Not covered).

Where this branch costs more than unslothai#16865, all in WebKit: opening (3.1 s
against 2.7 s in long frames), scroll plus settle (4.5 s against 3.1 s,
because the colouring unslothai#16865 leaves until after the stop now happens
during the scroll), and the worst single keystroke after the scroll (4.0
s against 2.9 s; main reads 1.3 s, so most of that gap predates this
branch). A long-callback probe around the typing window found no fence
work running there in either arm; the likeliest cause is that this
branch leaves every block the scroll passed coloured, so the page holds
more highlighted spans when typing restyles it, but that is not proven.

CPU profile of the same fast scroll in Chromium: on unslothai#16865 the Shiki
regex engine and TextMate scanner are the largest JavaScript cost (about
0.9 s of the scroll window; 2.4 s of opening the chat); on this branch
neither appears in the main thread's top functions, and what remains is
applying worker replies (0.24 s), `getSelection` (0.21 s, the existing
selection guard) and layout reads.

## Skipping layout for lines outside a capped block (measured
separately, not included)

Asked alongside the worker change: lines of a capped block far outside
its box are still laid out. I measured two ways of skipping that work,
each against this branch on its own, and neither is in this PR.

**`content-visibility: auto` with `contain-intrinsic-size: auto 1lh` on
each line** of a settled block's `code` (only with line numbers and wrap
off, since style containment would number every line 1 and wrapped lines
have no fixed height). Same bench, 3 interleaved repeats:

| | this PR | plus line containment |
| --- | --- | --- |
| Chromium: scroll, layout time (CDP) | 164 ms | 292 ms |
| Chromium: scroll, total task time (CDP) | 3.7 s | 7.0 s |
| Chromium 4x: scroll plus settle, long tasks | 4.8 s | 15.9 s |
| Chromium: open, layout time (CDP) | 362 ms | 345 ms |
| Chromium: four width changes, layout time (CDP) | 6 ms | 4 ms |
| WebKit: scroll plus settle, frames over 50 ms | 5.1 s | 14.8 s |
| WebKit: open, frames over 50 ms | 2.7 s | 2.6 s |

Opening is unchanged, and width changes were already cheap. Scrolling
the page gets much worse, because every line becomes its own containment
root and the browser has to recheck them all as the page moves. With
three 2,000-line blocks: forced relayout on a width change 1.4 against
1.3 ms in Chromium (a block over 240 lines lays out at max-content
width, so its lines do not relayout when the column changes) and 39
against 44 ms in WebKit, mount to colour within noise. It also changes
behaviour: a long line below the box no longer counts toward the block's
horizontal scroll width until it is scrolled into view (3 to 4 of 180
blocks shrank), and in one WebKit run two blocks came out 6 px taller.

**The WebKitGTK line window on every engine** (lines outside the window
render as plain text; this applies to blocks over 240 lines), three
2,000-line blocks: frames while scrolling inside a block improve (worst
frame 63 to 27 ms in Chromium, 165 to 24 ms in WebKit), but a width
change costs more (forced relayout 1.2 to 45 ms in Chromium, 36 to 114
ms in WebKit), mounting gets worse in WebKit (worst frame 771 to 1548
ms) and Chromium long tasks rise (1.36 to 1.73 s). This matches the
existing comment that line-window updates regress fast scrollbar drags
in Chromium. It is a possible follow-up limited to scrolling inside very
long blocks, not a general win.

## Behaviour

- Rendered output is unchanged: the `<pre>` HTML of 43 fully coloured
blocks over 12 languages is byte-identical between main, unslothai#16865 and this
branch, in Chromium and WebKit (same hash in both engines). Light and
dark themes, Shiki CSS variables, copy, wrap, line numbers and Expand
code are untouched; they read the same tokens.
- Without a Worker (or if it fails or stalls), fences highlight on the
main thread as before; the existing failover and stall paths are
unchanged.
- Print and find-in-page keep their existing paths (print still
tokenizes what the worker has not answered, the find bar still flushes
the queue).
- Holding a scrollbar thumb, a pointer, or a text selection inside a
block still holds colouring, as before.

## Tests

- New node tests that fail on unslothai#16865: the queue runs several fences per
step within its budget and asks about blocking once per kind; an
off-thread fence asks the worker at once and only the answer waits; a
short growth of a large streaming fence is coloured in the same call;
speculation stops at its horizon and a scroll resumes it; worker results
carry shared styles and no grammar state; the routing, latch and
speculation wiring.
- Token parity: worker-route tokens, after a structured clone, equal
main-thread tokens for 18 languages (plus bg, fg, theme name and root
style).
- Updated tests that pinned the old routing or used growths short enough
to now be tokenized at once (they now grow past the threshold, so they
still cover the throttled path).
- `npm test`: 15,133 of 15,136 pass. Failures: the KaTeX stylesheet Vite
test (fails with a symlinked `node_modules` on this host, with or
without this change) and once a wall-clock test
(`background-load-notice`: a hung read is abandoned), which passed in
the other full run and 3 of 3 reruns. `node scripts/typecheck.mjs
--tests` passes. ESLint on the changed files reports nothing new.
- Browser smokes:
- `playwright_long_thread_gate.py`: PASS on the final commit (40 turns
0.83 ms/key selector matching, 200 turns 0.74 ms/key, `:has()` 0.15 to
0.16 ms/key, area matrix 30 areas x 14 pages). Two earlier runs on this
host failed in the area matrix (`source_card` hover never opened in
liquid-3d at 200 turns, and once a 60 s page-load timeout); the same
gate on unslothai#16865 failed once too, on a different check (dialog style
recalc 35 ms over a 30 ms budget), so these read as load flakes.
- `playwright_code_block_flicker.py` (Chromium): every variant behaved
as its contract says (tree: 0 collapses, 0 placeholder frames, 0 sweep
shift over 3 repeats).
- `playwright_find_in_page.py`: Chromium 226/226. WebKit is flaky on
this host on both sides: unslothai#16865 failed 1 of 3 runs (one check), this
branch failed 4 of 4 runs (one check in three of them, a cascade of 27
in one), with a different failing check ID every run. That fixture has
no code fences, so none of the changed code runs there; this is not
proven harmless on a quiet host.

## Not covered

- On clean runners this branch still costs more main-thread time than
pre-unslothai#16865 main during a scroll (macOS WebKit 1.9 to 5.0 s of long
frames over scroll plus settle against 0.4 to 0.75 s; Chromium at 4x
0.65 s against 0.07 s) and when opening a chat. Pre-unslothai#16865 main colours
nothing until the scroll stops, and then only what is on screen; any
version that keeps every block coloured as it passes has to build that
markup during the scroll. Against current main it is lower in Chromium
at 4x (0.65 s against 1.85 s) and similar in WebKit. Removing the cap's
CSS (max-height and overflow, the custom scrollbars, every rule on the
code body, scrollbar-gutter) does not change macOS WebKit's scroll cost
on main, and JavaScript is only 0.2 to 0.6 s of it, so the rest is
WebKit style, layout and paint from the mid-scroll DOM work.
- The slow first keystroke after a scroll in the loaded-host WebKit
tables (seconds) is a Linux Playwright WebKit effect: each switch
between pointer and keyboard restyles every highlight span there. On
macOS WebKit the worst keystroke is 40 to 64 ms for every arm.
- The timing host was heavily loaded (load average 300 to 480 on 49
CPUs, shared with other jobs), so main-thread costs vary up to 2x
between repeats; the colour metrics (share of frames uncoloured, lag)
are stable across runs, the cost numbers are medians of 3 interleaved
repeats and should be read as direction, not precision.
- CPU throttling is Chromium-only (CDP); WebKit ran unthrottled. WebKit
has no long-task API, so its "long-task total" is the sum of frames over
50 ms.
- Playwright WebKit on Linux with a macOS user agent and platform, not
Safari on macOS, the macOS app's WKWebView, WebKitGTK (Linux desktop) or
WebView2 (Windows desktop); Firefox was not run.
- WebKit costs more main-thread time than unslothai#16865 when opening and across
a scroll plus settle (see Results), and its worst keystroke after a
scroll is higher; the cause of the keystroke gap is not pinned down.
- No live model streaming; the streaming numbers come from a scripted
stream (24 characters every 16 ms into a 143-line Python fence).

---------

Co-authored-by: Nilay Yadav <[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