SwiftUI Performance Audit
Attribution: copied from @Dimillian’s Dimillian/Skills (2025-12-31).
Overview
Audit SwiftUI view performance end-to-end, from instrumentation and baselining to root-cause analysis and concrete remediation steps.
Workflow Decision Tree
- - If the user provides code, start with "Code-First Review."
- If the user only describes symptoms, ask for minimal code/context, then do "Code-First Review."
- If code review is inconclusive, go to "Guide the User to Profile" and ask for a trace or screenshots.
1. Code-First Review
Collect:
- - Target view/feature code.
- Data flow: state, environment, observable models.
- Symptoms and reproduction steps.
Focus on:
- - View invalidation storms from broad state changes.
- Unstable identity in lists (
id churn, UUID() per render). - Heavy work in
body (formatting, sorting, image decoding). - Layout thrash (deep stacks,
GeometryReader, preference chains). - Large images without downsampling or resizing.
- Over-animated hierarchies (implicit animations on large trees).
Provide:
- - Likely root causes with code references.
- Suggested fixes and refactors.
- If needed, a minimal repro or instrumentation suggestion.
2. Guide the User to Profile
Explain how to collect data with Instruments:
- - Use the SwiftUI template in Instruments (Release build).
- Reproduce the exact interaction (scroll, navigation, animation).
- Capture SwiftUI timeline and Time Profiler.
- Export or screenshot the relevant lanes and the call tree.
Ask for:
- - Trace export or screenshots of SwiftUI lanes + Time Profiler call tree.
- Device/OS/build configuration.
3. Analyze and Diagnose
Prioritize likely SwiftUI culprits:
- - View invalidation storms from broad state changes.
- Unstable identity in lists (
id churn, UUID() per render). - Heavy work in
body (formatting, sorting, image decoding). - Layout thrash (deep stacks,
GeometryReader, preference chains). - Large images without downsampling or resizing.
- Over-animated hierarchies (implicit animations on large trees).
Summarize findings with evidence from traces/logs.
4. Remediate
Apply targeted fixes:
- - Narrow state scope (
@State/@Observable closer to leaf views). - Stabilize identities for
ForEach and lists. - Move heavy work out of
body (precompute, cache, @State). - Use
equatable() or value wrappers for expensive subtrees. - Downsample images before rendering.
- Reduce layout complexity or use fixed sizing where possible.
Common Code Smells (and Fixes)
Look for these patterns during code review.
Expensive formatters in body
CODEBLOCK0
Prefer cached formatters in a model or a dedicated helper:
CODEBLOCK1
Computed properties that do heavy work
CODEBLOCK2
Prefer precompute or cache on change:
CODEBLOCK3
Sorting/filtering in body or ForEach
CODEBLOCK4
Prefer sort once before view updates:
CODEBLOCK5
Inline filtering in ForEach
CODEBLOCK6
Prefer a prefiltered collection with stable identity.
Unstable identity
CODEBLOCK7
Avoid id: \.self for non-stable values; use a stable ID.
Image decoding on the main thread
CODEBLOCK8
Prefer decode/downsample off the main thread and store the result.
Broad dependencies in observable models
CODEBLOCK9
Prefer granular view models or per-item state to reduce update fan-out.
5. Verify
Ask the user to re-run the same capture and compare with baseline metrics.
Summarize the delta (CPU, frame drops, memory peak) if provided.
Outputs
Provide:
- - A short metrics table (before/after if available).
- Top issues (ordered by impact).
- Proposed fixes with estimated effort.
References
Add Apple documentation and WWDC resources under references/ as they are supplied by the user.
- - Optimizing SwiftUI performance with Instruments: INLINECODE21
- Understanding and improving SwiftUI performance: INLINECODE22
- Understanding hangs in your app: INLINECODE23
- Demystify SwiftUI performance (WWDC23): INLINECODE24
SwiftUI 性能审计
归属:复制自 @Dimillian 的 Dimillian/Skills(2025-12-31)。
概述
端到端审计 SwiftUI 视图性能,涵盖从检测和基线建立到根本原因分析和具体修复步骤。
工作流决策树
- - 如果用户提供代码,从代码优先审查开始。
- 如果用户仅描述症状,要求提供最小代码/上下文,然后进行代码优先审查。
- 如果代码审查无结论,进入引导用户进行性能分析并请求跟踪或截图。
1. 代码优先审查
收集:
- - 目标视图/功能代码。
- 数据流:状态、环境、可观察模型。
- 症状和复现步骤。
重点关注:
- - 由宽泛状态变化引起的视图失效风暴。
- 列表中的不稳定标识(id 频繁变化、每次渲染生成 UUID())。
- body 中的繁重工作(格式化、排序、图像解码)。
- 布局抖动(深层堆栈、GeometryReader、偏好链)。
- 未进行降采样或调整大小的大图像。
- 过度动画层级(大型树上的隐式动画)。
提供:
- - 可能的根本原因及代码引用。
- 建议的修复和重构方案。
- 如有需要,提供最小复现或检测建议。
2. 引导用户进行性能分析
解释如何使用 Instruments 收集数据:
- - 使用 Instruments 中的 SwiftUI 模板(Release 构建)。
- 复现精确交互(滚动、导航、动画)。
- 捕获 SwiftUI 时间线和时间分析器。
- 导出或截图相关通道和调用树。
请求提供:
- - 跟踪导出或 SwiftUI 通道 + 时间分析器调用树的截图。
- 设备/操作系统/构建配置信息。
3. 分析与诊断
优先排查常见的 SwiftUI 问题:
- - 由宽泛状态变化引起的视图失效风暴。
- 列表中的不稳定标识(id 频繁变化、每次渲染生成 UUID())。
- body 中的繁重工作(格式化、排序、图像解码)。
- 布局抖动(深层堆栈、GeometryReader、偏好链)。
- 未进行降采样或调整大小的大图像。
- 过度动画层级(大型树上的隐式动画)。
根据跟踪/日志证据总结发现。
4. 修复
应用针对性修复:
- - 缩小状态作用域(@State/@Observable 更靠近叶子视图)。
- 稳定 ForEach 和列表的标识。
- 将繁重工作移出 body(预计算、缓存、@State)。
- 对昂贵的子树使用 equatable() 或值包装器。
- 在渲染前对图像进行降采样。
- 减少布局复杂度或在可能的情况下使用固定尺寸。
常见代码异味(及修复)
在代码审查中查找以下模式。
body 中的昂贵格式化器
swift
var body: some View {
let number = NumberFormatter() // 慢速分配
let measure = MeasurementFormatter() // 慢速分配
Text(measure.string(from: .init(value: meters, unit: .meters)))
}
优先在模型或专用辅助类中缓存格式化器:
swift
final class DistanceFormatter {
static let shared = DistanceFormatter()
let number = NumberFormatter()
let measure = MeasurementFormatter()
}
执行繁重工作的计算属性
swift
var filtered: [Item] {
items.filter { $0.isEnabled } // 每次 body 求值时运行
}
优先在变化时预计算或缓存:
swift
@State private var filtered: [Item] = []
// 当输入变化时更新 filtered
在 body 或 ForEach 中排序/过滤
swift
List {
ForEach(items.sorted(by: sortRule)) { item in
Row(item)
}
}
优先在视图更新前一次性排序:
swift
let sortedItems = items.sorted(by: sortRule)
在 ForEach 中内联过滤
swift
ForEach(items.filter { $0.isEnabled }) { item in
Row(item)
}
优先使用具有稳定标识的预过滤集合。
不稳定标识
swift
ForEach(items, id: \.self) { item in
Row(item)
}
避免对非稳定值使用 id: \.self;使用稳定 ID。
在主线程上解码图像
swift
Image(uiImage: UIImage(data: data)!)
优先在主线程外解码/降采样并存储结果。
可观察模型中的宽泛依赖
swift
@Observable class Model {
var items: [Item] = []
}
var body: some View {
Row(isFavorite: model.items.contains(item))
}
优先使用细粒度视图模型或逐项状态以减少更新扇出。
5. 验证
要求用户重新运行相同的捕获并与基线指标进行比较。
如果提供,总结差异(CPU、帧率下降、内存峰值)。
输出
提供:
- - 简短指标表(如有前后对比)。
- 主要问题(按影响排序)。
- 建议修复及预估工作量。
参考资料
根据用户提供的资料,在 references/ 目录下添加 Apple 文档和 WWDC 资源。
- - 使用 Instruments 优化 SwiftUI 性能:references/optimizing-swiftui-performance-instruments.md
- 理解和改善 SwiftUI 性能:references/understanding-improving-swiftui-performance.md
- 理解应用中的卡顿:references/understanding-hangs-in-your-app.md
- 揭秘 SwiftUI 性能(WWDC23):references/demystify-swiftui-performance-wwdc23.md