Skip to content

Text in a list's rows rasterizes from 4,096 pixels on the Mac, so scrolling a grid of cards stops drawing labels on the main thread - #2274

Merged
ccheever merged 4 commits into
expo:mainfrom
trcoffman:upstream/mac-list-text-raster
Dec 1, 2026

Conversation

@trcoffman

Copy link
Copy Markdown
Contributor

What happens. Scrolling a wide grid on macOS, each frame's Core Animation commit is mostly AppKit drawing card labels on the main thread. We hit it in a library grid: 278 cards in 18 columns, each card a cover, a one-line author (125×20 pt) and a two-line title (125×32 pt). In a 30-fling scroll the CPU drew about 1,500 times. It also redrew the empty button around each card's labels.

Cause. A paragraph rasterizes on a worker and shows as a layer only above TextRasterizer.minPixels (16,384). At 2× scale those labels are 10,000 and 16,000 pixels, just under, so textIsSmall sends them to draw(_:). The cutoff's comment explains it: a surface has a fixed cost, and a table that built one per narrow cell as each row mounted scrolled worse. That holds when rows are built mid-scroll. A list's rows are mostly kept and reused instead, so its labels are drawn again on the main thread at each redisplay, while a surface is made once.

Fix. Text inside a list (any list ancestor; NodeView.inList, kept until the view moves) rasterizes from minPixelsInList (4,096). Other text keeps 16,384.

Measured. A driven macOS scroll of the grid above: 30 flings with turns, one phaseless continuous wheel event a frame, 60 Hz, card labels identical on screen.

Before After
CPU draws (NodeView.draw) ~1,500 37
Core Animation commit p50 / p95 / max 4.7 / 5.9 / 9.1 ms 1.8 / 2.0 / 2.6 ms
Frame update p50 5.9 ms 2.8 ms

Not measured. The narrow-cell table that motivated the cutoff. If that benchmark still exists, it's the case to rerun: a list whose rows mount while it scrolls now makes a surface per label of 4,096 pixels or more. Raising minPixelsInList, or tying it to whether rows are kept, are the knobs if it regresses.

Tests. TextRasterMacTests, TextMetricsTests and HDRColorTests pass. Two PressWashMacTests failures (testCaptureCopiesOwnWhatAppKitDrewAgain, testOffscreenDrawingRestoresOwnedCanvasTextWithoutAnotherEdit) occur with and without this change on our machine (macOS 26.6). Tested on f032000129 plus our other local patches. The diff applies to e65895d009.


How this was made. Claude Opus 5.5 (Anthropic's model, running in Claude Code) found this, wrote the change, the tests and this description, and ran the measurements, at the direction of the account owner, who is opening the PR. The numbers come from runs on one Mac (macOS 26.6, 60 Hz display).

@ccheever ccheever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The measurement is convincing for a grid whose rows are kept. Merged with main, the 17 Mac test classes I ran (including TextRasterMacTests, RowReuseMacTests and CollectionMacTests) pass. Before this can land:

  1. The gate fails seams. inList's walk up the superviews adds a NodeView cast and a tree walk past the recorded counts (nodeViewCasts 597 of 596, treeWalks 591 of 589). I reproduced this with bun scripts/gate.mjs --seams on this PR alone. Main passes. The list could be told to the node when it mounts, as other row facts are, rather than found by walking up.

Astra (GPT-6.2 Astra) reviewed it too. These are its code traces, which I have not reproduced:

  1. A recycled row can show the previous item's label. Paint "A" in an 80×20 pt label at 2×, rebind the row offscreen to "B", and scroll it on screen before B's raster lands. The pending job has B's key while the view still holds A's surface, the urgent check accepts it, and updateLayer shows A on B's row (TextRasterMac.swift:141, NodeViewMac.swift:903). Before this change that label drew directly, so it could not happen.
  2. Rows that mount mid-fling put a surface per label back on the main thread. This is the case the 16,384 cutoff guards. With VoiceOver on, Mac row reuse is off (RenewMac.swift:54), so a fling past the mounted window builds fresh rows, and each label of 4,096 pixels or more misses the cache and rasterizes synchronously (TextRasterMac.swift:153). Please measure that case, or limit the lower cutoff to rows that are kept or reused.
  3. An eager list keeps a surface per label it has shown. Scroll Carousel's 2,000-card strip end to end at 2×: each qualifying label keeps its surface after it leaves the viewport, even once the shared cache evicts its entry, so memory grows with the cards visited (apps/carousel/app.contract:71, TextRasterMac.swift:379).

The inList cache holds up. Astra checked that kind == "list" is the list on the Mac, and in an AppKit probe, moving an ancestor between a list and a plain container in the same window sent viewDidMoveToWindow to the label both times.

🤖 Generated with Claude Code

A grid's card labels (125×32 and 125×20 pt) sat just under minPixels at 2× and
were drawn on the main thread at every redisplay: about 1,500 CPU draws in a
30-fling scroll, and most of each frame's commit (4.7 ms p50). Rasterized they
are drawn once on a worker: 37 draws, 1.8 ms.

Only in a kept row: a row of a list that windows its rows and rebinds them
(ListHosting.owns, row reuse on), which each node learns from the parent it
mounts under (prepareToMount(under:)) rather than by walking up. An eager list's
rows and rows built anew (VoiceOver or Switch Control on) keep 16,384. A renewed
view drops its raster, so a rebound row never shows the old item's label.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@trcoffman
trcoffman force-pushed the upstream/mac-list-text-raster branch from 957ed21 to 2d7bcae Compare November 30, 2026 00:05
@trcoffman

Copy link
Copy Markdown
Contributor Author

Thanks. All four are addressed in 2d7bcaef, rebased on ddb666fa. The lower cutoff now applies only to text in a kept row: a row of a list that windows its rows and rebinds them.

  1. seams. The walk is gone, as you suggested. Each node is told the parent it mounts under: placeChildren passes the parent to prepareToMount(under:), an existing call made as each child is added, by changing three existing lines in PresenterMac.swift. NodeView.inKeptRow follows those recorded parents and is cached until the next mount (NodeView.mountEpoch). A parent counts if collections.owns(parent.id) and the runtime's rowReuse is on. It adds no NodeView cast and no superview walk: bun scripts/gate.mjs passes every part, seams and caps included, and PresenterMac.swift stays at 1,500 lines.
  2. A recycled row shows the previous label. Agreed, and it applied to any rasterized label in a rebound row, not only those this change rasterizes. NodeView.renew() now drops a view's text raster (dropTextRaster()), as it already drops a picture so it's "never shown under the new item". A rebound label then has no raster, as a fresh one doesn't, and the urgent path rasterizes the new item's first pixels as it shows. New test: TextRasterMacTests.testARenewedLabelKeepsNoneOfTheOldItemsPixels.
  3. Rows mounted mid-fling with VoiceOver on. Limited, as you suggested. With row reuse off (VoiceOver, Switch Control, EXACT_ROW_REUSE=0), no row is kept, so labels keep 16,384. RowReuse.adopt bumps the mount epoch when reuse turns on or off.
  4. An eager list keeps a surface per label. An eager list isn't one the list host windows (ListHosting.owns), so its labels keep 16,384 too.

New test: RowReuseMacTests.testRowTextIsKeptOnlyWhereRowsAreRebound checks that a row's title is kept with reuse on, that the status text outside the list isn't, and that a row in a list without reuse isn't.

Measured with the same driven grid scroll, now with two-cover cards: commit p50 4.9 ms, p95 6.0 ms, no frame interval over 25 ms. That's unchanged from the unscoped version, since the grid's rows are kept.

Tests on our machine (macOS 26.6).

  • Pass: TextRasterMacTests, TextMetricsTests, TableFeedMacTests, StagedApplyMacTests, CollectionMacTests and HDRColorTests. One CollectionMacTests case failed once when the classes ran side by side, and passed in two runs on its own.
  • RowReuseMacTests.testAReboundRowShowsItsItemAsAFreshRowDoes fails here with 30 table-row emphasized mismatches. It fails the same way on bare ddb666fa, so it's environmental on this machine; it passed in your run.
  • PressWashMacTests.testOffscreenDrawingRestoresOwnedCanvasTextWithoutAnotherEdit fails with and without this change, as before.

How this was made. Claude Opus 5.5 (Anthropic's model, running in Claude Code) made these changes and wrote this reply, at the direction of the account owner.

@ccheever ccheever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Recording the parent at mount is the right shape, and seams now passes. Merged with main, the gate passes. Points 2 and 3 of my last review are fixed. Point 1 is fixed for ordinary mounts, and point 4 is partly fixed.

Before landing

  1. inKeptRow can recurse forever. It asks parent.inKeptRow before caching its own answer (TextRasterMac.swift:326). A detached node also keeps the mountParent it was last given. So if two nodes ever record each other, the first uncached lookup recurses until the stack overflows. Astra (GPT-6.2 Astra) traced one way in: B is portaled under A, a shared-element flight lifts B, and B returns to its declared parent while A is portaled under B. placeChildren skips the flying B, so B keeps A as its parent while A records B (PresenterMac.swift:1388, FlightsMac.swift:305). I have not reproduced that sequence. The recursion and the stale parent are in the code either way. Walk the parents in a loop that stops at a node it has already visited, or at a fixed depth, and clear mountParent when a node is removed.
  2. Rows the runner won't rebind still get the lower cutoff. The runner builds fresh rows where a row can't be rebound, such as a row with a text input (reuse.rs:33, :125). inKeptRow only checks the session-wide rowReuse flag, so a fling through such rows rasterizes each new label on the main thread. That is the case point 3 of my last review was about. Ask whether the row is actually rebound, not whether reuse is on.
  3. An eager list inside a windowed list's fixed content inherits true. inKeptRow accepts any descendant of an owned list, and the runner places a list's fixed content beside its row wrappers (views.rs:195). An eager list in a fixed header therefore keeps a surface for every label it has shown. That is point 4 again, one level down. Stop the walk at the row wrapper, or at the first list it meets.

Smaller

  • Native table cells and both staged attachment paths still call prepareToMount() without a parent (TableFeedMac.swift:117, StagingMac.swift:54, :98). So labels in an NSTableView row keep 16,384, as before this change. If that's intended, a line in inKeptRow's comment would say so. The new test uses table: false.
  • useRowReuse(false) called directly leaves an inKeptRow cached until the next epoch. RowReuse.adopt bumps the epoch, but the runtime's setter doesn't (Bridge.swift:390).

Astra found no leak: mountParent is weak, and forget() releases the raster.

🤖 Generated with Claude Code

ccheever and others added 3 commits December 1, 2026 01:12
`inKeptRow` walks the recorded mount parents in a loop to the first list
and stops at a node it has visited. It counts a row only once the list
has rebound it: a row the runner never rebinds (one holding a text input),
rows built while reuse is off, and an eager list in a windowed list's
fixed content keep 16,384. A node leaving its container forgets its
parent, and staged mounts record theirs. The answer no longer reads the
runtime's reuse flag, so `Runtime.rowReuse` is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ccheever

ccheever commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

I finished the last review's points on this branch (46d98c1, then a merge of main) so it can land:

  • inKeptRow walks the recorded parents in a loop that stops at a node it has seen, and at the first list. A node clears mountParent when it leaves its container.
  • A row counts as kept only once its list has rebound it (its wrapper renewed at least once). Rows the runner builds anew keep 16,384: one holding a text input, every row while reuse is off, a row not yet rebound. So Runtime.rowReuse is gone.
  • An eager list in a windowed list's fixed content stops the walk at itself.
  • Staged mounts record their parent. Native table rows keep 16,384, the case that cutoff was measured on.

One difference from your version: the first screenful keeps 16,384 until its rows are first rebound, since only a rebound row is known to stay built. Each fix has a test that fails on 2d7bcae. RowReuseMacTests and TextRasterMacTests pass, along with StagedApplyMacTests, the collection and table classes and the gate.

🤖 Generated with Claude Code

@ccheever
ccheever merged commit aa6940c into expo:main Dec 1, 2026
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.

2 participants