Community研究与数据分析github.com

steipete/swiftui-performance-audit

SwiftUI 性能审计:渲染、滚动、CPU/内存、更新、布局、Instruments。

swiftui-performance-audit 是什么?

这是一个全面的 SwiftUI 性能审计技能,支持 Claude Code、Cursor 和 Codex 等 AI 编码代理。它提供了一个端到端的工作流,从代码审查开始,引导用户使用 Instruments 进行性能分析,然后进行深入诊断并提供具体的修复步骤。该技能专注于识别常见的 SwiftUI 性能问题,如视图失效风暴、列表标识不稳定、body 中的繁重工作、布局抖动以及未下采样的大图像。它包含一个决策树来引导工作流,一个常见的代码异味(和修复)列表,以及一个验证步骤,让用户重新运行分析并与基线指标进行比较。最终输出包括一个简短的指标表、按影响排序的关键问题、建议的修复方案以及估算的工作量。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/steipete/agent-scripts/tree/main/skills/swiftui-performance-audit

Installed? Explore more 研究与数据分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

SwiftUI 性能审计

归因:从 @Dimillian 的 Dimillian/Skills 复制(2025-12-31)。

概述

对 SwiftUI 视图性能进行端到端审计,从仪表化和基线化到根本原因分析和具体的修复步骤。

工作流决策树

  • 如果用户提供了代码,从“代码优先审查”开始。
  • 如果用户仅描述了症状,要求提供最小的代码/上下文,然后进行“代码优先审查”。
  • 如果代码审查没有结论,转向“引导用户进行性能分析”,并要求提供 trace 或截图。

1. 代码优先审查

收集:

  • 目标视图/功能代码。
  • 数据流:状态、环境、可观察模型。
  • 症状和复现步骤。

关注:

  • 由广泛状态变化引起的视图失效风暴。
  • 列表中不稳定的标识(ID 频繁变化,每次渲染生成 UUID())。
  • body 中的繁重工作(格式化、排序、图片解码)。
  • 布局抖动(深层栈、GeometryReader、偏好链)。
  • 未下采样或调整大小的大图像。
  • 过度动画的层级结构(大型树上的隐式动画)。

提供:

  • 带有代码引用的可能根本原因。
  • 建议的修复和重构方法。
  • 如果需要,提供最小复现或仪表化建议。

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

解释如何使用 Instruments 收集数据:

  • 使用 Instruments 中的 SwiftUI 模板(Release 构建)。
  • 复现确切的交互(滚动、导航、动画)。
  • 捕获 SwiftUI 时间线和时间分析器。
  • 导出或截图相关的轨道和调用树。

请求:

  • Trace 导出或 SwiftUI 轨道 + 时间分析器调用树的截图。
  • 设备/操作系统/构建配置。

3. 分析和诊断

优先考虑可能的 SwiftUI 问题:

  • 由广泛状态变化引起的视图失效风暴。
  • 列表中不稳定的标识(ID 频繁变化,每次渲染生成 UUID())。
  • body 中的繁重工作(格式化、排序、图片解码)。
  • 布局抖动(深层栈、GeometryReader、偏好链)。
  • 未下采样或调整大小的大图像。
  • 过度动画的层级结构(大型树上的隐式动画)。

使用来自 traces/日志的证据总结发现。

4. 修复

应用有针对性的修复:

  • 缩小状态作用域(@State/@Observable 更靠近叶子视图)。
  • 为 ForEach 和列表稳定标识。
  • 将繁重工作移出 body(预计算、缓存、@State)。
  • 对昂贵的子树使用 equatable() 或值包装器。
  • 在渲染前下采样图像。
  • 降低布局复杂度或尽可能使用固定尺寸。

常见代码异味(及修复)

在代码审查期间查找这些模式。

body 中的昂贵格式化器

var body: some View {
    let number = NumberFormatter() // 缓慢的分配
    let measure = MeasurementFormatter() // 缓慢的分配
    Text(measure.string(from: .init(value: meters, unit: .meters)))
}

优先使用缓存在模型或专用辅助类中的格式化器:

final class DistanceFormatter {
    static let shared = DistanceFormatter()
    let number = NumberFormatter()
    let measure = MeasurementFormatter()
}

进行繁重工作的计算属性

var filtered: [Item] {
    items.filter { $0.isEnabled } // 每次 body 评估时运行
}

优先预计算或在更改时缓存:

@State private var filtered: [Item] = []
// 当输入更改时更新 filtered

在 body 或 ForEach 中排序/过滤

List {
    ForEach(items.sorted(by: sortRule)) { item in
        Row(item)
    }
}

优先在视图更新前排序一次:

let sortedItems = items.sorted(by: sortRule)

在 ForEach 中内联过滤

ForEach(items.filter { $0.isEnabled }) { item in
    Row(item)
}

优先使用具有稳定标识的预过滤集合。

不稳定的标识

ForEach(items, id: \.self) { item in
    Row(item)
}

避免对非稳定值使用 id: \.self;使用稳定 ID。

在主线程上解码图片

Image(uiImage: UIImage(data: data)!)

优先在主线程之外解码/下采样并存储结果。

可观察模型中的广泛依赖

@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

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

steipete/agent-transcript

GitHub PR/issue agent transcripts: redact, preview, and insert safely.

steipete/beeper

Beeper cache: contact hints, room lookup, WhatsApp/iMessage traces, FTS.

steipete/browser-use

Existing Chrome automation: Chrome plugin first, mcporter fallback.

steipete/clawsweeper-status

ClawSweeper status: URLs, workflow health, active workers, ops snapshot.

steipete/clickclack

ClickClack ops: chat app, Hetzner deploy, DNS/docs/app, Docker rollout.

steipete/cloudflare-registrar

Cloudflare Registrar: domain availability, prices, registration via mcporter.

steipete/codex-debugging

Codex debugging: codex-rs core/tui/exec/cli/app-server/config.

steipete/create-cli

CLI UX/spec: args, flags, help, output, errors, config, dry-run.

steipete/discord-clawd

Discord-backed OpenClaw agent/session relay; not archive search.

steipete/domain-dns-ops

DNS/domain ops: registrars, zones, redirects, DNS/HTTP verify, manager truth.

steipete/frontend-design

Frontend UI: pages, apps, components, polished non-generic design.

steipete/github-author-context

GitHub contributor context: identity, activity, trust, company/team signal.

steipete/github-cache-hygiene

GitHub quota/cache hygiene: gh, ghx, xcache, gitcrawl, mirrors, limits.

steipete/github-deep-review

GitHub deep review: bugs, PRs, best fix, stale-or-real, read code first.

steipete/github-project-triage

GitHub issue/PR triage: queues, CI, blockers, risk, proof, next actions.

steipete/hopper-debugger

Hopper debugging: macOS/iOS binaries, ObjC/Swift symbols, dyld, LLDB.

steipete/instruments-profiling

Instruments/xctrace profiling: macOS/iOS traces, binaries, args, exports.

steipete/mac-maintenance

Mac upkeep: brew update/upgrade, pull clean repos, empty Trash.

steipete/maintainer-orchestrator

Open-source maintainer orchestration: Codex app workers, work recovery, dependencies, vision, releases.

steipete/markdown-converter

Markdown conversion: PDF, Office, HTML, data, OCR, audio, ZIP, YouTube.

相关技能