Repository navigation
perf(zero-swift): give VirtualList's size to SwiftUI instead of fitting it with Auto Layout - #273
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
VirtualListis a platform view (RowsScrollViewRepresentable) with nosizeThatFits. So a stack around it asked SwiftUI's platform view host for its layout traits, and AppKit answered withfittingSize: 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'ssizeThatFitsat 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.
blanking,-latency 50, 3 rounds: busy per frameissues, 6 rounds: busy per framesampleof a fling:AppKitPlatformViewHost.fittingSizeWrong and blank frames don't change (blanking: wrong 36–48 before and 33–48 after over 10 rounds, blank 0). The
issuesbase 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) andswift format lint --strict.Patch changeset.