Community研究與資料分析github.com

steipete/swiftui-performance-audit

SwiftUI 性能審計:渲染、滾動、CPU/記憶體、更新、佈局、Instruments。

swiftui-performance-audit 是什麼?

此技能提供一套端到端的 SwiftUI 視圖性能審計流程,從基準測試、程式碼審查,到使用 Instruments 進行性能分析,最後產出具體的改善方案。它支援多種平台,包括 iOS、macOS 等使用 SwiftUI 的環境。技能首先引導使用者進行「程式碼優先審查」,找出常見的效能瓶頸,如視圖無效風暴、不穩定的列表身份、body 中的繁重計算、佈局抖動、大型圖片未經縮放等。若程式碼審查無法解決問題,則指導使用者使用 Instruments 的 SwiftUI 模板收集數據,並根據追蹤結果進行根因分析。最後,技能會提供針對性的修復建議,如縮小狀態作用域、穩定 ForEach 的身份、將繁重工作移出 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 視圖的效能,包含儀器化、設定基準線、根本原因分析,以及具體的修復步驟。

工作流程決策樹

  • 如果使用者提供程式碼,從「程式碼優先審查」開始。
  • 如果使用者僅描述症狀,要求提供最小化程式碼/情境,然後進行「程式碼優先審查」。
  • 如果程式碼審查無結論,轉向「引導使用者進行性能分析」,並要求提供追蹤結果或螢幕截圖。

1. 程式碼優先審查

收集資訊:

  • 目標視圖/功能程式碼。
  • 資料流程:狀態、環境、可觀測模型。
  • 症狀與重現步驟。

重點關注:

  • 由廣泛狀態變更導致的視圖無效風暴。
  • 列表中不穩定的身份(ID 混亂、每次渲染都使用 UUID())。
  • body 中的繁重工作(格式化、排序、圖片解碼)。
  • 佈局抖動(深層堆疊、GeometryReader、偏好鏈)。
  • 未經降採樣或縮放的大型圖片。
  • 過度動畫的層級結構(對大型視圖樹使用隱式動畫)。

提供:

  • 帶有程式碼參考的可能的根本原因。
  • 建議的修復與重構方案。
  • 如有需要,提供最小重現範例或儀器化建議。

2. 引導使用者進行性能分析

說明如何使用 Instruments 收集資料:

  • 使用 Instruments 的 SwiftUI 模板(Release 建置)。
  • 重現確切的互動(滾動、導航、動畫)。
  • 擷取 SwiftUI 時間軸和 Time Profiler。
  • 匯出或截取相關時間軸與呼叫樹。

要求提供:

  • 追蹤結果匯出,或 SwiftUI 時間軸與 Time Profiler 呼叫樹的螢幕截圖。
  • 裝置/作業系統/建置配置。

3. 分析與診斷

優先檢查常見的 SwiftUI 效能殺手:

  • 由廣泛狀態變更導致的視圖無效風暴。
  • 列表中不穩定的身份(ID 混亂、每次渲染都使用 UUID())。
  • body 中的繁重工作(格式化、排序、圖片解碼)。
  • 佈局抖動(深層堆疊、GeometryReader、偏好鏈)。
  • 未經降採樣或縮放的大型圖片。
  • 過度動畫的層級結構(對大型視圖樹使用隱式動畫)。

總結發現,並附上來自追蹤/日誌的證據。

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、掉幀、記憶體峰值),如果提供的話。

輸出

提供:

  • 簡短的指標表格(如有前/後數據)。
  • 首要問題(按影響程度排序)。
  • 建議的修復方案與估計工作量。

參考資料

當使用者提供時,將 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

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.

相關技能