Communitygithub.com

ComeOnOliver/skillshub

從程式碼審查和架構角度審計並改善 SwiftUI 執行時期效能。適用於診斷 SwiftUI 應用程式中渲染緩慢、滾動卡頓、CPU/記憶體使用率過高、過度視圖更新或佈局震盪等問題,並在僅靠程式碼審查不足時,提供使用者自行執行 Instruments 分析的指導。

skillshub 是什麼?

這項技能「SwiftUI 效能審計」提供了一個端到端的解決方案,用於分析並改善 SwiftUI 應用程式的執行時期效能。它從「程式碼優先審查」開始,檢查常見的效能問題,例如因廣泛狀態變更導致的視圖失效風暴、列表中不穩定的身份識別、在 `body` 中執行重量級工作、佈局震盪,以及未經縮放的圖像。如果程式碼審查無法找出根本原因,該技能會引導使用者使用 Instruments 進行剖析,捕捉 SwiftUI 時間軸和時間分析器資料,然後根據證據進行分析,並提供針對性的修復建議。此技能支援 Claude Code、Copic 和其他基於命令列的 AI 代理,透過提供一個結構化的逐步工作流程(包含決策樹、常見程式碼異味範例和驗證步驟),幫助開發人員系統性地診斷和解決 SwiftUI 效能瓶頸。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/aiskillstore/marketplace/dimillian/swiftui-performance-audit

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

SwiftUI 效能審計

概述

端到端審計 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 中使用昂貴的格式化器

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.

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

A conceptual skill for building an API client in Next.js that handles JWT tokens

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相關技能