guide-swiftui-performance-audit

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Guide Skill — This is an expert workflow/pattern guide, not API reference documentation. Originally from Dimillian/Skills by Thomas Ricouard. MIT License.
指南技能 — 这是一份专家级工作流程/模式指南,而非API参考文档。 最初来自Thomas Ricouard的Dimillian/Skills,采用MIT许可证。

SwiftUI Performance Audit

SwiftUI性能审计

Quick start

快速入门

Use this skill to diagnose SwiftUI performance issues from code first, then request profiling evidence when code review alone cannot explain the symptoms.
使用本技能先从代码层面诊断SwiftUI性能问题,当仅靠代码审查无法解释症状时,再要求用户提供性能分析证据。

Workflow

工作流程

  1. Classify the symptom: slow rendering, janky scrolling, high CPU, memory growth, hangs, or excessive view updates.
  2. If code is available, start with a code-first review using
    references/code-smells.md
    .
  3. If code is not available, ask for the smallest useful slice: target view, data flow, reproduction steps, and deployment target.
  4. If code review is inconclusive or runtime evidence is required, guide the user through profiling with
    references/profiling-intake.md
    .
  5. Summarize likely causes, evidence, remediation, and validation steps using
    references/report-template.md
    .
  1. 分类症状:渲染缓慢、滚动卡顿、CPU占用过高、内存增长、应用卡顿或视图更新过度。
  2. 若有代码可用,先借助
    references/code-smells.md
    开展代码优先审查。
  3. 若无代码可用,请求用户提供最小可用信息片段:目标视图、数据流、复现步骤以及部署目标。
  4. 若代码审查无结论或需要运行时证据,指导用户通过
    references/profiling-intake.md
    进行性能分析。
  5. 借助
    references/report-template.md
    总结可能的原因、证据、修复方案以及验证步骤。

1. Intake

1. 信息收集

Collect:
  • Target view or feature code.
  • Symptoms and exact reproduction steps.
  • Data flow:
    @State
    ,
    @Binding
    , environment dependencies, and observable models.
  • Whether the issue shows up on device or simulator, and whether it was observed in Debug or Release.
Ask the user to classify the issue if possible:
  • CPU spike or battery drain
  • Janky scrolling or dropped frames
  • High memory or image pressure
  • Hangs or unresponsive interactions
  • Excessive or unexpectedly broad view updates
For the full profiling intake checklist, read
references/profiling-intake.md
.
收集以下信息:
  • 目标视图或功能代码。
  • 症状及准确的复现步骤。
  • 数据流:
    @State
    @Binding
    、环境依赖以及可观察模型。
  • 问题出现在设备还是模拟器上,是在Debug还是Release模式下观察到的。
若可能,让用户对问题进行分类:
  • CPU峰值或电池消耗过快
  • 滚动卡顿或丢帧
  • 内存占用过高或图像内存压力大
  • 应用卡顿或交互无响应
  • 视图更新过度或范围超出预期
完整的性能分析收集清单请查看
references/profiling-intake.md

2. Code-First Review

2. 代码优先审查

Focus on:
  • Invalidation storms from broad observation or environment reads.
  • Unstable identity in lists and
    ForEach
    .
  • Heavy derived work in
    body
    or view builders.
  • Layout thrash from complex hierarchies,
    GeometryReader
    , or preference chains.
  • Large image decode or resize work on the main thread.
  • Animation or transition work applied too broadly.
Use
references/code-smells.md
for the detailed smell catalog and fix guidance.
Provide:
  • Likely root causes with code references.
  • Suggested fixes and refactors.
  • If needed, a minimal repro or instrumentation suggestion.
重点关注:
  • 因宽泛观察或环境读取导致的无效更新风暴。
  • 列表和
    ForEach
    中的不稳定标识。
  • body
    或视图构建器中执行的大量衍生计算。
  • 复杂层级、
    GeometryReader
    或偏好链导致的布局抖动。
  • 在主线程上执行的大型图像解码或缩放操作。
  • 应用范围过广的动画或过渡效果。
详细的代码问题目录及修复指导请查看
references/code-smells.md
需提供:
  • 带有代码引用的可能根本原因。
  • 建议的修复和重构方案。
  • 必要时,提供最小复现示例或 instrumentation 建议。

3. Guide the User to Profile

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

If code review does not explain the issue, ask for runtime evidence:
  • A trace export or screenshots of the SwiftUI timeline and Time Profiler call tree.
  • Device/OS/build configuration.
  • The exact interaction being profiled.
  • Before/after metrics if the user is comparing a change.
Use
references/profiling-intake.md
for the exact checklist and collection steps.
若代码审查无法解释问题,请求用户提供运行时证据:
  • SwiftUI时间线和Time Profiler调用树的追踪导出文件或截图。
  • 设备/操作系统/构建配置信息。
  • 正在分析的具体交互操作。
  • 若用户正在对比变更效果,提供变更前后的指标数据。
具体的清单和收集步骤请查看
references/profiling-intake.md

4. Analyze and Diagnose

4. 分析与诊断

  • Map the evidence to the most likely category: invalidation, identity churn, layout thrash, main-thread work, image cost, or animation cost.
  • Prioritize problems by impact, not by how easy they are to explain.
  • Distinguish code-level suspicion from trace-backed evidence.
  • Call out when profiling is still insufficient and what additional evidence would reduce uncertainty.
  • 将证据映射到最可能的类别:无效更新、标识频繁变更、布局抖动、主线程任务、图像成本或动画成本。
  • 按影响优先级排序问题,而非按解释难度。
  • 区分代码层面的怀疑和追踪数据支持的证据。
  • 指出何时性能分析仍不充分,以及需要哪些额外证据来减少不确定性。

5. Remediate

5. 修复方案

Apply targeted fixes:
  • Narrow state scope and reduce broad observation fan-out.
  • Stabilize identities for
    ForEach
    and lists.
  • Move heavy work out of
    body
    into derived state updated from inputs, model-layer precomputation, memoized helpers, or background preprocessing. Use
    @State
    only for view-owned state, not as an ad hoc cache for arbitrary computation.
  • Use
    equatable()
    only when equality is cheaper than recomputing the subtree and the inputs are truly value-semantic.
  • Downsample images before rendering.
  • Reduce layout complexity or use fixed sizing where possible.
Use
references/code-smells.md
for examples, Observation-specific fan-out guidance, and remediation patterns.
应用针对性修复:
  • 缩小状态范围,减少宽泛观察的扩散。
  • 稳定
    ForEach
    和列表的标识。
  • 将大量计算从
    body
    转移到衍生状态(由输入更新)、模型层预计算、 memoized 助手或后台预处理中。仅将
    @State
    用于视图自有状态,而非作为任意计算的临时缓存。
  • 仅当相等性检查比重新计算子树成本更低且输入真正具有值语义时,才使用
    equatable()
  • 在渲染前对图像进行降采样。
  • 降低布局复杂度,尽可能使用固定尺寸。
示例、Observation特定的扩散指导以及修复模式请查看
references/code-smells.md

6. Verify

6. 验证

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.
Use
references/report-template.md
when formatting the final audit.
需提供:
  • 简短的指标表格(若有,包含变更前后数据)。
  • 按影响排序的首要问题。
  • 带有预估工作量的修复建议。
格式化最终审计报告时请使用
references/report-template.md

References

参考资料

  • Profiling intake and collection checklist:
    references/profiling-intake.md
  • Common code smells and remediation patterns:
    references/code-smells.md
  • Audit output template:
    references/report-template.md
  • Add Apple documentation and WWDC resources under
    references/
    as they are supplied by the user.
  • Optimizing SwiftUI performance with Instruments:
    references/optimizing-swiftui-performance-instruments.md
  • Understanding and improving SwiftUI performance:
    references/understanding-improving-swiftui-performance.md
  • Understanding hangs in your app:
    references/understanding-hangs-in-your-app.md
  • Demystify SwiftUI performance (WWDC23):
    references/demystify-swiftui-performance-wwdc23.md
  • 性能分析收集清单:
    references/profiling-intake.md
  • 常见代码问题及修复模式:
    references/code-smells.md
  • 审计输出模板:
    references/report-template.md
  • 用户提供的Apple文档和WWDC资源请添加到
    references/
    目录下。
  • 使用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