Skip to content

Studio: stop the autoscroll follow flag restyling the whole thread in WebKit - #17035

Merged
danielhanchen merged 3 commits into
mainfrom
studio-sheet-rewrap
Nov 9, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-sheet-rewrap

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Problem

On long chats in WebKit (macOS Safari / WKWebView), opening or closing Run settings costs 3-4x what it costs on a short chat (real macos-15 runners at 150 turns: longest frame 120-159 ms against 32-49 ms). The sidebar toggle grows the same way (120-245 ms against 35-46 ms).

At 1440 px the sheet does not re-wrap anything: the message column is capped at 744 px and stays 744 px when the chat area narrows from 1160 to 888 px. Only 3 of 300 rows are in layout in WebKit (7 in Chromium) after #16986. The cost that grows with the chat comes from the autoscroll owner instead:

  • Any width change of the chat column opens a follow window, and the owner flips data-aui-following on at the start and off in the settle tail about 600 ms later.
  • The flag sat on the hidden portal host, and index.css reached the viewport through it: .aui-thread-host[data-aui-following] + .aui-thread-root .aui-thread-viewport. For a rule that reaches its subject through a sibling and then a descendant combinator, WebKit invalidates every descendant of the host's later siblings, which is the whole thread.
  • The flip runs inside the autoscroll tick, and the tick reads scrollHeight right after, so the restyle is forced there: 40-70 ms per flip at 150 turns against 1-4 ms at 1 turn, twice per toggle (once in the opening frame, once in the settle tail).

Chromium only restyles the viewport for the same rule (2.6 ms per flip at 150 turns), which is why Chromium and Edge were already close to flat.

Fix

  • use-intent-aware-autoscroll.tsx: the flag is set on the viewport itself (the element the hook already owns), through one setFollowing helper used by the tick, the idle frame and teardown.
  • index.css: the rule matches the flag on its subject, .aui-thread-viewport[data-aui-following] { overflow-anchor: none; }. Same property, same element, same window as before, so scroll anchoring behaves as it did.
  • thread.tsx: drops the now unused markerRef from the portal host.

Results

Local, Playwright WebKit with a macOS user agent and Chromium, 1440x900, 1 and 150 turns (300 messages, 57k nodes). Arms interleaved per rep: main, a second build of main served by a separate server (A/A), and this PR. 5 runs x 3 toggles per cell, median [min-max].

Cost of one flag flip (forced style + layout, 16 flips per cell, the element each build's hook writes):

Engine Turns main main (A/A) This PR
WebKit (mac UA) 150 56.5 ms [31-261] 62 ms [43-111] 4 ms [3-9]
WebKit (mac UA) 1 4 ms [1-86] 2 ms [1-112] 1 ms [0-3]
Chromium 150 2.6 ms [1.5-11.5] 4.7 ms [1.6-25.5] 0.1 ms [0-1.1]
Chromium 1 0.1 ms 0.1 ms 0.0 ms

Script time inside animation frames during a toggle (the autoscroll ticks, where the restyle is forced), WebKit:

Surface Turns main main (A/A) This PR
Run settings open 150 141 [82-646] 173 [123-555] 16 [1-80]
Run settings close 150 142 [103-1068] 184 [104-3010] 17 [4-175]
Run settings open / close 1 8 / 9 7 / 6 5 / 5
Browser pane open 150 288 [153-686] 346 [62-927] 138 [0-600]
Browser pane close 150 311 [8-1803] 359 [2-571] 266 [0-1436]

Long / short ratio of that script time: Run settings 15.8-30.7x on main (both builds), 3.2-3.4x with this PR.

Chromium, style / layout thread time from a trace of one open + close at 150 turns: Run settings 28.0 / 5.8 ms (main), 14.9 / 6.0 ms (A/A), 16.7 / 5.6 ms (this PR), against 13-15 / 2.5 ms at 1 turn. Browser pane 73 / 101, 51 / 77, 52 / 68 ms (about 45-51 / 46-50 ms at 1 turn). Elements restyled per toggle are the same in all arms (418-422 for Run settings). Chromium was already close to flat, and this PR keeps it there.

Longest frame (ms, median [min-max]):

Engine Surface Turns main main (A/A) This PR
WebKit Run settings open 1 / 150 132 / 197 104 / 178 182 / 219
WebKit Run settings close 1 / 150 79 / 177 149 / 233 127 / 227
WebKit Browser pane open 1 / 150 636 / 444 260 / 535 364 / 881
WebKit Browser pane close 1 / 150 201 / 200 124 / 230 145 / 462
Chromium Run settings open 1 / 150 50 / 167 67 / 83 50 / 150
Chromium Run settings close 1 / 150 67 / 167 67 / 67 67 / 183
Chromium Browser pane open 1 / 150 100 / 167 100 / 183 100 / 183
Chromium Browser pane close 1 / 150 100 / 117 67 / 117 83 / 217

The longest-frame numbers do not separate the arms. This host ran at a load average of about 390 on 49 cores, the two main builds differ by up to 2x in the same cell, and Chromium traces show single style passes of 541 ms wall time for 9 ms of thread time. The table is here for completeness. The flip cost and the script time above are the measurements that hold up on this host.

What else runs during a toggle at 150 turns (WebKit, after this PR):

  • Rows re-wrapped: Run settings, none at 1440 px (column stays 744 px). Browser pane, the 3 rows in layout (744 -> 580 px); the other 297 stay skipped. No rows were skipped or released during a toggle at the bottom of the thread, so the distant-rows.ts anchor correction did not run.
  • Forced layouts in ResizeObserver callbacks during a pane open: the side-panel inset observer, the resizable panel group observer, the code block data-scrolls-x check and the thread width steps observer. Median 98-120 ms at 150 turns and 52-61 ms at 1 turn in all three arms; they are where the per-frame layout of the pane's 260 ms flex-basis transition gets forced. Run settings fires none of them.
  • The sheet has no animation: its width goes from 0 to its full size in one commit, so the chat re-wraps once. The browser pane does animate flex-basis. Turning that transition off locally did not make WebKit cheaper at 150 turns (longest frame with it off: open 684 ms, close 334 ms; with it on: 459 ms and 257 ms; within noise), so this PR leaves it alone.

Behaviour

Main and this PR, WebKit (mac UA) and Chromium, 150 turns:

  • Pinned to the bottom: 0 px from the bottom after every Run settings and browser pane toggle in both arms (WebKit 5 of 5 runs). The viewport computes overflow-anchor: none while following and auto after the window closes, in both arms and both engines.
  • Scrolled up 3000 px: the first visible row moves 0.0 px when Run settings opens or closes, in both arms and both engines. The browser pane moves it by 24 px (47 px when pinned), identically on main.
  • Screenshots with the sheet and the pane open and closed, at the bottom and scrolled up: 14 of 16 pixel-identical between main and this PR, all 8 WebKit ones included. The other two (Chromium, pane closed) differ in a 1-2 px wide strip of glyph antialiasing at a line end.

Tests

  • New tests/thread-follow-flag-selector.test.ts (3 tests): the flag is matched on the viewport as the subject, the hook writes it on the viewport through one setter, and no thread rule reaches the viewport through an attribute on a sibling. All 3 fail on main.
  • npm test: 15160 passed, 1 failed. The failure is the KaTeX stylesheet Vite test, which also fails on main with a symlinked node_modules.
  • node scripts/typecheck.mjs --tests: clean.
  • Long-chat gate (tests/studio/playwright_long_thread_gate.py): PASS. Its run_settings area restyles 571 -> 1,217 elements from 40 to 200 turns, the same as other trees from today (567-577 -> 1,209-1,216).
  • tests/studio/test_autoscroll_harness_contract.py: 16 passed. tests/studio/playwright_chat_autoscroll.py fails the same way on main and on this PR on this host ("the content did not grow while detached; the re-pin check proved nothing").

Not covered

  • Real Safari / WKWebView on macOS and the desktop shells. Local WebKit is WebKitGTK with a macOS user agent. The invalidation that made the flip expensive is shared WebCore code, but the frame-level win on a real Mac was not measured.
  • Frame-level gains on this host: the longest frame stays within the A/A spread. A rerun of the menu bench on macos-15 runners would settle it.
  • Windows WebView2 and Linux WebKitGTK were not run. Edge uses the same Blink invalidation as Chromium (already cheap here), and WebKitGTK parks distant rows through message-window-controller.ts.
  • Windows narrower than about 1300 px, where opening Run settings does narrow the message column and re-wrap the rows in layout: not measured.
  • Streaming: the flag flips at the start and end of every follow window during a reply too. That path uses the same setter but was not measured here.

Real macOS runner check

macos-15, current main vs a second build of the same main (A/A) vs this PR merged onto main, 150 turns, 4 reps in balanced order, all built == served.

  • WebKit max frame: Run settings, sidebar toggle and browser pane are within the spread between the two main builds (A/A per rep 0.51-3.34x on the same runner). No surface is worse beyond that spread in more than 2 of 4 reps, and the tools menu control behaves the same. So on real macOS the per-flip saving does not show up at frame level; it removes a whole-thread restyle per flip without a measurable cost.
  • Chromium: style time for opening the sidebar is 0.39-0.61x of main in every rep, and Run settings open style time and close max frame improve beyond A/A in 3 of 4 reps.

The autoscroll owner flips data-aui-following at the start and end of every
follow window, which any width change of the chat column opens (run settings,
browser pane, sidebar). The flag sat on the hidden portal host and index.css
reached the viewport through it with
`.aui-thread-host[data-aui-following] + .aui-thread-root .aui-thread-viewport`.
WebKit invalidates every descendant of the host's later siblings for such a
rule, so each flip restyled the whole thread: 31-261 ms (median 57-62) per flip
at 150 turns against 1-4 ms at 1 turn. The flip lands inside the autoscroll
tick, which then forces the restyle, so run settings and the sidebar paid it
twice per toggle, once when opening the window and once in the settle tail
600 ms later.

The flag now sits on the viewport and the rule matches it as the subject:
`.aui-thread-viewport[data-aui-following]`. It is the same overflow-anchor: none
on the same element for the same window, so scrolling behaves as before. A flip
now costs 3-9 ms in WebKit and 0-1 ms in Chromium at 150 turns. The unused
markerRef goes away.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 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-07T15:08:07.584623Z 0741f40 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: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 0741f405c2

ℹ️ 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 78429b3 into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the studio-sheet-rewrap branch November 9, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant