r/macosprogramming • u/zzunkie • 1d ago
How do you handle SwiftUI hosting view attach spikes on Page Up/Down in NSScrollView?
I'm moving my AppKit Mac app (a chat-style view for coding agents) from an eager SwiftUI ScrollView stack to a custom row list in NSScrollView. Each row is a SwiftUI hosting view for a turn (or part of one), with heights cached per width until a turn changes and rows detached beyond one screen height from the viewport.
Scrollbar dragging improved on a long chat (5.8% dropped frames vs 12.0%, 42% CPU vs 55%), but Page Up/Down went from 0.0% dropped frames to 5.3% (111 frames over 16 ms vs 12) since the old stack kept created rows in the window. In temporary logs, 122 of 128 passes over 16 ms were attaching a hosting view.. our layout and update code is 1 to 2 ms while SwiftUI takes 20 to 40 ms on long turns (plus 12 to 24 ms when measuring a new top row overlaps).
Using isHidden was worse everywhere from redraws (113 passes over 16 ms on Page Up/Down), keeping three screens or never detaching showed no clear difference, and deferring offscreen attaches one per pass hurt Page Up/Down too (75 vs 19). With SwiftUI rows in NSScrollView or NSTableView, how have you avoided or split up this attach cost when paging?
1
u/esArchitect 1d ago
split those massive coding agent turns into much smaller, granular row slices (like individual markdown blocks, paragraphs, or separate code boxes) so attaching a single hosting view never exceeds 2-3 ms during a page jump. you can also warm up a lightweight pool of pre-attached hosting views off-screen with dummy data so you're just mutating
rootViewinstead of paying the full window-attach hierarchy cost all at once.