Skip to content

perf(zero-swift): give VirtualList's size to SwiftUI instead of fitting it with Auto Layout - #273

Merged
tmm merged 1 commit into
mainfrom
swift-perf-virtual-list-size
Nov 20, 2026
Merged

tmm merged 1 commit into
mainfrom
swift-perf-virtual-list-size

Conversation

@tmm

@tmm tmm commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

VirtualList is a platform view (RowsScrollViewRepresentable) with no sizeThatFits. So a stack around it asked SwiftUI's platform view host for its layout traits, and AppKit answered with fittingSize: an Auto Layout engine populated over the scroll view's whole subtree, every row's hosting view included. The list's body reads each snapshot, so on macOS this ran in each root layout while scrolling (StackLayout.prioritize → ViewLeafView.layoutTraits → AppKitPlatformViewHost.intrinsicLayoutTraits → fittingSize → _populateEngineWithConstraintsForViewSubtree): 9% of the main thread's samples in a fling.

The representable now gives its size: the proposal, with 0 for an unspecified dimension. That is what the fitting gave, since the list is fully flexible with an ideal size of 0.

Same size. A scratch probe (not committed) hosted the representable in 11 layouts: bare, VStack, VStack with Spacer, HStack, fixed width, .fixedSize() both ways, ScrollView, ZStack, max height, and layout priority. Each ran at two window sizes, with and without 40 row views, and the probe recorded the list's frame and the root's sizeThatFits at five proposals. Before and after gave identical output in all 44 cases on macOS and all 44 on the iOS Simulator.

Numbers. TestHost, issue list with SwiftUI rows, Release, macOS on a Mac satellite (M4 Pro), variants taken in turns. Busy time is the main thread's time per frame while scrolling.

Run Before After
blanking, -latency 50, 3 rounds: busy per frame 2.67–3.12 ms (median 2.69) 2.21–2.30 ms (median 2.30)
same: time in frames over 8.3 ms 5.3–7.0 s 4.9–5.4 s
issues, 6 rounds: busy per frame 1.41–2.64 ms (median 2.24) 1.20–1.66 ms (median 1.54)
same: frames over 16.7 ms, median 23.5 10
sample of a fling: AppKitPlatformViewHost.fittingSize 180 main-thread samples 6

Wrong and blank frames don't change (blanking: wrong 36–48 before and 33–48 after over 10 rounds, blank 0). The issues base swings between about 1.5 ms and 2.2–2.6 ms from run to run; after the change it stays at the low end. On the iOS Simulator the change made no measurable difference: the root lays out 0.36 times per frame there, against 3.25 on macOS, and run-to-run noise was ±25%.

Tests. macOS: swift test --filter ZeroVirtualTests (132 tests) and swift format lint --strict.

Patch changeset.

…ng it with Auto Layout

`VirtualList` is a platform view (`RowsScrollViewRepresentable`) without
`sizeThatFits`, so a stack around it asked the platform view host for its
layout traits, and AppKit's `fittingSize` populated an Auto Layout engine
over the scroll view's subtree, every row's hosting view included. The
list's body reads each snapshot, so this ran in each root layout while
scrolling: 9% of the main thread's samples in a fling on macOS.

The representable now says its size: the proposal, and 0 where a
dimension is unspecified, which is what the fitting gave (fully flexible,
ideal size 0). A scratch probe of 11 layouts (bare, VStack, HStack,
fixed width, `.fixedSize()` both ways, ScrollView, ZStack, max height,
layout priority) at two window sizes, with and without 40 row views,
gave the same list frames and `sizeThatFits` results before and after,
on macOS and on the iOS Simulator.

TestHost, issue list with SwiftUI rows, macOS on a Mac satellite (M4 Pro),
runs taken in turns, main thread's busy time per frame:

- `blanking` script, `-latency 50`, 3 rounds: 2.67-3.12 -> 2.21-2.30 ms
  (median 2.69 -> 2.30); 120 Hz stall time 5.3-7.0 -> 4.9-5.4 s
- `issues` script, 6 rounds: 1.41-2.64 -> 1.20-1.66 ms (median 2.24 ->
  1.54); stalls over 16.7 ms, median 23.5 -> 10
- `sample` of a fling: `AppKitPlatformViewHost.fittingSize` 180 -> 6
  main-thread samples

Wrong and blank frames are unchanged. On the iOS Simulator the change
made no measurable difference (the root lays out 0.36 times per frame
there, against 3.25 on macOS).

Amp-Thread-ID: https://ampcode.com/threads/T-884aab88-be49-4bc7-96a1-044f0e71dabf
Co-authored-by: tmm <tmm@tmm.dev>
@tmm
tmm merged commit 93d8c4a into main Nov 20, 2026
32 checks passed
@tmm
tmm deleted the swift-perf-virtual-list-size branch November 20, 2026 22:06
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