Skip to content

perf(zero-swift): draw VirtualList's debug tint and TestHost's header in a shape; warn about bare-style backgrounds on macOS 27 - #284

Merged
tmm merged 1 commit into
mainfrom
swift-perf-macos-layouts
Dec 1, 2026

Conversation

@tmm

@tmm tmm commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Option D of the perf sweep: why macOS laid the window's root out 3–5 times per moving frame, against 0.36 on iOS.

Cause. On macOS 27, a hosting view whose content has a background or an overlay drawn from a bare ShapeStyle (.background(.quaternary), a Color, a material) resolves its safe area against the window's corners (_BackgroundStyleModifier.BackgroundInfo → SafeAreaInsets.resolve(regions:cornerAdaptation:in:) → NSHostingView.rootTransform), which registers the view for changes to its geometry in the window. Every scroll then invalidates it (NSClipView.scrollToPoint → NSViewHierarchyNoteGeometryInWindowDidChange → NSHostingView.geometryInWindowDidChange). Its setNeedsUpdateConstraints reaches SwiftUI's AppKitPlatformViewHost, which invalidates the window root's layout. TestHost's header was such a view. Rendering each snapshot, updateNSView, and resetting the header's and footer's rootView are not involved.

macOS 26.3 doesn't do this. macOS 27.2 does, whether the app is linked against the 26.5 or the 27.3 SDK. A standalone harness shows it: 200 hosting-controller rows in an NSScrollView, scrolled by script. Rows with a bare-style background cost 7.0–7.5 ms per step there, against 0.8 ms for the same rows in a Rectangle. No public modifier or hosting option tried (ignoresSafeArea, safeAreaPadding, compositingGroup, containerShape, sceneBridgingOptions = [], hiding the view, and others) stops it, so ZeroVirtual can't avoid it on an app's behalf.

Change.

  • TestHost's header: .background(.quaternary, in: Rectangle()), so the frame probe measures the list.
  • The debug blank-space tint (-ZeroVirtualTintBlankSpace YES) draws each row's backdrop in a Rectangle.
  • VirtualList's docs: draw backgrounds and overlays in rows, the header and the footer in a shape.

Release builds behave the same. ZeroVirtual itself doesn't get faster: an app whose list content uses bare-style backgrounds keeps paying until it changes that content. zbugs-swift's list content already uses shapes (in: Capsule(), in: RoundedRectangle).

Same pixels. Window captures of TestHost's issue list, main against this branch on macOS 27, are identical files in light, in dark, and in dark with the debug tint on. A harness also rendered .background(style) and .background(style, in: Rectangle()) identically for .quaternary and the window background, in light and dark.

Numbers. TestHost's issue list with SwiftUI rows, Release, macOS 27.2 on a Mac satellite (mac.large). main (2cc09db) and this branch ran in turns, alternating which went first. layouts counts SwiftUI's hosting views' layouts (the list's and the window root's) per frame the list moved (-probe-layouts 1). Busy is the main thread's time per frame, from runs without the counter.

Run main This branch
issues: layouts per moving frame 3.25 0.22
issues: busy ms per frame, 3 rounds 3.33, 3.33, 2.66 2.20, 2.23, 1.95
issues: frames over 16.7 ms 79, 77, 58 51, 54, 47
blanking -latency 50: layouts per moving frame 4.87 1.84
blanking: busy ms per frame, 3 rounds 4.04, 4.04, 4.03 3.38, 3.42, 3.42
blanking: frames over 8.3 ms / 16.7 ms 991/134, 987/127, 983/140 966/121, 991/133, 975/130
Debug, -ZeroVirtualTintBlankSpace YES, issues: layouts per moving frame 50.13 0.22
same: busy ms per frame (Debug) 5.25 2.77

The macOS root now lays out about as often as iOS's (0.36). Wrong and blank frames don't change: issues 0 and 0 on both; blanking wrong 39–51 on main and 37–45 here, blank 0 on both. blanking's 120 Hz stalls barely move, because they come from page changes rather than from this.

Tests. macOS 27: swift test --filter ZeroVirtualTests, and swift format lint --strict.

Patch changeset: the tint and the docs ship in the library.

… in a shape, and warn against bare-style backgrounds on macOS 27

On macOS 27, a hosting view whose content has a background or an overlay
drawn from a bare ShapeStyle resolves its safe area against the window's
corners and so follows its geometry in the window. Every scroll of the
clip view then invalidates it, and the invalidation reaches the window's
root hosting view through SwiftUI's platform view host: the root and that
view lay out again on every frame the list scrolls. macOS 26 doesn't do
this, whichever SDK the app is built with.

TestHost's header used .background(.quaternary), so the frame probe
measured this instead of the list. It now draws in a Rectangle, which
renders the same pixels. The debug blank-space tint draws each row's
backdrop the same way, and VirtualList's docs tell apps to.

Amp-Thread-ID: https://ampcode.com/threads/T-884aab88-be49-4bc7-96a1-044f0e71dabf
Co-authored-by: tmm <tmm@tmm.dev>
@tmm
tmm merged commit 96af57f into main Dec 1, 2026
41 of 42 checks passed
@tmm
tmm deleted the swift-perf-macos-layouts branch December 1, 2026 08:24
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