Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Diagnoses SwiftUI performance issues — slow rendering, janky scrolling, high CPU/memory — through code review and Instruments profiling guidance.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/optimizing-swiftui-performance-instruments.md
1# Optimizing SwiftUI Performance with Instruments (Summary)23Context: WWDC session introducing the next-generation SwiftUI Instrument in Instruments 26 and how to diagnose SwiftUI-specific bottlenecks.45## Key takeaways67- Profile SwiftUI issues with the SwiftUI template (SwiftUI instrument + Time Profiler + Hangs/Hitches).8- Long view body updates are a common bottleneck; use "Long View Body Updates" to identify slow bodies.9- Set inspection range on a long update and correlate with Time Profiler to find expensive frames.10- Keep work out of `body`: move formatting, sorting, image decoding, and other expensive work into cached or precomputed paths.11- Use Cause & Effect Graph to diagnose *why* updates occur; SwiftUI is declarative, so backtraces are often unhelpful.12- Avoid broad dependencies that trigger many updates (e.g., `@Observable` arrays or global environment reads).13- Prefer granular view models and scoped state so only the affected view updates.14- Environment values update checks still cost time; avoid placing fast-changing values (timers, geometry) in environment.15- Profile early and often during feature development to catch regressions.1617## Suggested workflow (condensed)18191. Record a trace in Release mode using the SwiftUI template.202. Inspect "Long View Body Updates" and "Other Long Updates."213. Zoom into a long update, then inspect Time Profiler for hot frames.224. Fix slow body work by moving heavy logic into precomputed/cache paths.235. Use Cause & Effect Graph to identify unintended update fan-out.246. Re-record and compare the update counts and hitch frequency.2526## Example patterns from the session2728- Caching formatted distance strings in a location manager instead of computing in `body`.29- Replacing a dependency on a global favorites array with per-item view models to reduce update fan-out.30