swiftui-performance

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SwiftUI Performance

SwiftUI 性能优化

Overview

概述

Audit SwiftUI view performance end-to-end, from instrumentation and baselining to root-cause analysis and concrete remediation steps.
端到端审计SwiftUI视图性能,从插桩测试、基准测试到根因分析及具体的修复步骤。

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

1. 代码优先审查

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).
  • Top-level conditional view swapping (
    if/else
    returning different root branches).
  • 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.
收集信息:
  • 目标视图/功能代码。
  • 数据流:状态、环境、可观察模型。
  • 症状及复现步骤。
审查重点:
  • 大范围状态变更导致的视图无效风暴。
  • 列表中不稳定的标识(
    id
    频繁变更、每次渲染生成
    UUID()
    )。
  • 顶层条件视图切换(
    if/else
    返回不同的根分支)。
  • body
    中的繁重操作(格式化、排序、图片解码)。
  • 布局混乱(深层栈、
    GeometryReader
    、偏好链)。
  • 未进行下采样或调整大小的大尺寸图片。
  • 过度动画的层级结构(大型视图树使用隐式动画)。
输出内容:
  • 带有代码引用的可能根因。
  • 建议的修复和重构方案。
  • 必要时,提供最小复现示例或插桩测试建议。

2. Guide the User to Profile

2. 指导用户进行性能分析

Explain how to collect data with Instruments:
  • Use the SwiftUI template in Instruments (always profile a 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.
说明如何使用Instruments收集数据:
  • 使用Instruments中的SwiftUI模板(始终针对Release构建版本进行性能分析)。
  • 复现具体的交互操作(滚动、导航、动画)。
  • 捕获SwiftUI时间线和时间分析器数据。
  • 导出或截图相关的分析栏和调用树。
请求提供:
  • 跟踪日志导出文件,或SwiftUI分析栏+时间分析器调用树的截图。
  • 设备/操作系统/构建配置信息。

3. Analyze and Diagnose

3. 分析与诊断

Prioritize likely SwiftUI culprits:
  • View invalidation storms from broad state changes.
  • Unstable identity in lists (
    id
    churn,
    UUID()
    per render).
  • Top-level conditional view swapping (
    if/else
    returning different root branches).
  • 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.
优先排查SwiftUI常见问题:
  • 大范围状态变更导致的视图无效风暴。
  • 列表中不稳定的标识(
    id
    频繁变更、每次渲染生成
    UUID()
    )。
  • 顶层条件视图切换(
    if/else
    返回不同的根分支)。
  • body
    中的繁重操作(格式化、排序、图片解码)。
  • 布局混乱(深层栈、
    GeometryReader
    、偏好链)。
  • 未进行下采样或调整大小的大尺寸图片。
  • 过度动画的层级结构(大型视图树使用隐式动画)。
结合跟踪日志/日志中的证据总结发现的问题。

4. Remediate

4. 修复优化

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.
应用针对性修复方案:
  • 缩小状态作用域(将
    @State
    /
    @Observable
    放在更靠近叶子视图的位置)。
  • 稳定
    ForEach
    和列表的标识。
  • 将繁重操作移出
    body
    (预计算、缓存、使用
    @State
    存储结果)。
  • 对开销大的子视图使用
    equatable()
    或值包装器。
  • 渲染前对图片进行下采样。
  • 降低布局复杂度,或尽可能使用固定尺寸。

Common Code Smells (and Fixes)

常见代码坏味道(及修复方案)

Look for these patterns during code review.
代码审查时需关注以下模式。

Expensive formatters in
body

body
中使用开销大的格式化器

swift
var body: some View {
    let number = NumberFormatter() // slow allocation
    let measure = MeasurementFormatter() // slow allocation
    Text(measure.string(from: .init(value: meters, unit: .meters)))
}
Prefer cached formatters in a model or a dedicated helper:
swift
final class DistanceFormatter {
    static let shared = DistanceFormatter()
    let number = NumberFormatter()
    let measure = MeasurementFormatter()
}
swift
var body: some View {
    let number = NumberFormatter() // slow allocation
    let measure = MeasurementFormatter() // slow allocation
    Text(measure.string(from: .init(value: meters, unit: .meters)))
}
建议在模型或专用工具类中缓存格式化器:
swift
final class DistanceFormatter {
    static let shared = DistanceFormatter()
    let number = NumberFormatter()
    let measure = MeasurementFormatter()
}

Computed properties that do heavy work

执行繁重操作的计算属性

swift
var filtered: [Item] {
    items.filter { $0.isEnabled } // runs on every body eval
}
Prefer precompute or cache on change:
swift
@State private var filtered: [Item] = []
// update filtered when inputs change
swift
var filtered: [Item] {
    items.filter { $0.isEnabled } // runs on every body eval
}
建议预计算或在输入变更时缓存结果:
swift
@State private var filtered: [Item] = []
// update filtered when inputs change

Sorting/filtering in
body
or
ForEach

body
ForEach
中进行排序/过滤

swift
List {
    ForEach(items.sorted(by: sortRule)) { item in
        Row(item)
    }
}
Prefer sort once before view updates:
swift
let sortedItems = items.sorted(by: sortRule)
swift
List {
    ForEach(items.sorted(by: sortRule)) { item in
        Row(item)
    }
}
建议在视图更新前完成排序:
swift
let sortedItems = items.sorted(by: sortRule)

Inline filtering in
ForEach

ForEach
中内联过滤

swift
ForEach(items.filter { $0.isEnabled }) { item in
    Row(item)
}
Prefer a prefiltered collection with stable identity.
swift
ForEach(items.filter { $0.isEnabled }) { item in
    Row(item)
}
建议使用带有稳定标识的预过滤集合。

Unstable identity

不稳定的标识

swift
ForEach(items, id: \.self) { item in
    Row(item)
}
Avoid
id: \.self
for non-stable values; use a stable ID.
swift
ForEach(items, id: \.self) { item in
    Row(item)
}
避免对非稳定值使用
id: \.self
;使用稳定的ID。

Top-level conditional view swapping

顶层条件视图切换

swift
var content: some View {
    if isEditing {
        editingView
    } else {
        readOnlyView
    }
}
Prefer one stable base view and localize conditions to sections/modifiers (for example inside
toolbar
, row content,
overlay
, or
disabled
). This reduces root identity churn and helps SwiftUI diffing stay efficient.
swift
var content: some View {
    if isEditing {
        editingView
    } else {
        readOnlyView
    }
}
建议使用一个稳定的基础视图,并将条件判断局限在局部区域/修饰器中(例如在
toolbar
、行内容、
overlay
disabled
中)。这可以减少根标识的频繁变更,帮助SwiftUI更高效地进行差异对比。

Image decoding on the main thread

主线程上进行图片解码

swift
Image(uiImage: UIImage(data: data)!)
Prefer decode/downsample off the main thread and store the result.
swift
Image(uiImage: UIImage(data: data)!)
建议在主线程外完成解码/下采样,并存储结果。

Broad dependencies in observable models

可观察模型中的宽泛依赖

swift
@Observable class Model {
    var items: [Item] = []
}

var body: some View {
    Row(isFavorite: model.items.contains(item))
}
Prefer granular view models or per-item state to reduce update fan-out.
swift
@Observable class Model {
    var items: [Item] = []
}

var body: some View {
    Row(isFavorite: model.items.contains(item))
}
建议使用细粒度的视图模型或每个项目单独的状态,以减少更新范围。

5. Verify

5. 验证

Ask the user to re-run the same capture and compare with baseline metrics. Summarize the delta (CPU, frame drops, memory peak) if provided.
请用户重新执行相同的性能捕获操作,并与基准指标进行对比。 如果用户提供数据,总结指标变化(CPU占用、掉帧情况、内存峰值)。

Outputs

输出内容

Provide:
  • A short metrics table (before/after if available).
  • Top issues (ordered by impact).
  • Proposed fixes with estimated effort.
提供:
  • 简短的指标对比表(如有前后数据则包含)。
  • 按影响优先级排序的主要问题。
  • 带有预估工作量的修复方案建议。

MCP Tool Notes

MCP工具说明

  • xcodebuildmcp: When building for profiling, use Release configuration. Debug builds include extra runtime checks that distort performance measurements. Always profile Release builds on a real device when possible.
  • xcodebuildmcp:构建用于性能分析的版本时,请使用Release配置。Debug构建包含额外的运行时检查,会扭曲性能测量结果。尽可能在真实设备上对Release构建版本进行性能分析。

Common Mistakes

常见错误

  1. Profiling Debug builds. Debug builds include extra runtime checks and disable optimizations, producing misleading perf data. Profile Release builds on a real device.
  2. Observing an entire model when only one property is needed. Break large
    @Observable
    models into focused ones, or use computed properties/closures to narrow observation scope.
  3. Using
    GeometryReader
    inside ScrollView items.
    GeometryReader forces eager sizing and defeats lazy loading. Prefer
    .onGeometryChange
    (iOS 18+) or measure outside the lazy container.
  4. Calling
    DateFormatter()
    or
    NumberFormatter()
    inside
    body
    .
    These are expensive to create. Make them static or move them outside the view.
  5. Animating non-equatable state. If SwiftUI cannot determine equality, it redraws every frame. Conform state to
    Equatable
    or use
    .animation(_:value:)
    with an explicit value.
  6. Large flat
    List
    without identifiers.
    Use
    id:
    or make items
    Identifiable
    so SwiftUI can diff efficiently instead of rebuilding the entire list.
  7. Unnecessary
    @State
    wrapper objects.
    Wrapping a simple value type in a class for
    @State
    defeats value semantics. Use plain
    @State
    with structs.
  8. Blocking
    MainActor
    with synchronous I/O.
    File reads, JSON parsing of large payloads, and image decoding should happen off the main actor. Use
    Task.detached
    or a custom actor.
  1. 对Debug构建版本进行性能分析:Debug构建包含额外的运行时检查并禁用了优化,会产生误导性的性能数据。请在真实设备上对Release构建版本进行性能分析。
  2. 仅需要单个属性时观察整个模型:将大型
    @Observable
    模型拆分为专注于单一功能的模型,或使用计算属性/闭包缩小观察范围。
  3. 在ScrollView项目中使用
    GeometryReader
    :GeometryReader会强制提前计算尺寸,破坏懒加载机制。建议使用
    .onGeometryChange
    (iOS 18+)或在懒加载容器外进行测量。
  4. body
    中调用
    DateFormatter()
    NumberFormatter()
    :这些对象的创建开销很大。请将它们设为静态属性,或移到视图外部。
  5. 对非Equatable状态应用动画:如果SwiftUI无法判断状态是否相等,会在每一帧都重绘视图。请让状态遵循
    Equatable
    协议,或使用带有显式值的
    .animation(_:value:)
  6. 大型扁平
    List
    未设置标识符
    :使用
    id:
    参数或让项目遵循
    Identifiable
    协议,这样SwiftUI可以高效地进行差异对比,而非重建整个列表。
  7. 不必要的
    @State
    包装对象
    :将简单值类型包装在类中用于
    @State
    会破坏值语义。请对结构体使用普通的
    @State
  8. 使用同步I/O阻塞
    MainActor
    :文件读取、大型JSON解析和图片解码应在
    MainActor
    之外执行。请使用
    Task.detached
    或自定义actor。

Review Checklist

审查清单

  • No
    DateFormatter
    /
    NumberFormatter
    allocations inside
    body
  • Large lists use
    Identifiable
    items or explicit
    id:
  • @Observable
    models expose only the properties views actually read
  • Heavy computation is off
    MainActor
    (image processing, parsing)
  • GeometryReader
    is not inside a
    LazyVStack
    /
    LazyHStack
    /
    List
  • Animations use explicit
    value:
    parameter
  • No synchronous network/file I/O on the main thread
  • Profiling done on Release build, real device
  • @Observable
    view models are
    @MainActor
    -isolated; types crossing concurrency boundaries are
    Sendable
  • body
    中未分配
    DateFormatter
    /
    NumberFormatter
    实例
  • 大型列表使用了
    Identifiable
    项目或显式
    id:
    参数
  • @Observable
    模型仅暴露视图实际需要的属性
  • 繁重计算操作在
    MainActor
    之外执行(图片处理、解析)
  • GeometryReader
    未在
    LazyVStack
    /
    LazyHStack
    /
    List
    内部使用
  • 动画使用了显式的
    value:
    参数
  • 主线程上未执行同步网络/文件I/O
  • 性能分析在真实设备的Release构建版本上完成
  • @Observable
    视图模型已隔离到
    @MainActor
    ;跨并发边界的类型遵循
    Sendable
    协议

References

参考资料

  • Demystify SwiftUI performance (WWDC23):
    references/demystify-swiftui-performance-wwdc23.md
  • Optimizing SwiftUI performance with Instruments:
    references/optimizing-swiftui-performance-instruments.md
  • Understanding hangs in your app:
    references/understanding-hangs-in-your-app.md
  • Understanding and improving SwiftUI performance:
    references/understanding-improving-swiftui-performance.md
  • 揭秘SwiftUI性能(WWDC23):
    references/demystify-swiftui-performance-wwdc23.md
  • 使用Instruments优化SwiftUI性能:
    references/optimizing-swiftui-performance-instruments.md
  • 了解应用卡顿问题:
    references/understanding-hangs-in-your-app.md
  • 理解并优化SwiftUI性能:
    references/understanding-improving-swiftui-performance.md