Communitygithub.com

ComeOnOliver/skillshub

通过代码审查和架构分析,审计并改善 SwiftUI 应用的运行时性能。用于诊断 SwiftUI 应用中的缓慢渲染、滚动卡顿、高 CPU/内存使用率、过度视图更新或布局抖动等问题,并在代码审查不足以定位问题时,提供用户自行运行 Instruments 分析的指导。

skillshub 是什么?

这是一个专注于 SwiftUI 运行时性能审计的专业 AI 技能。它能够从端到端的角度审视 SwiftUI 视图性能,包括代码审查、性能数据收集指导、根因分析和提供具体的修复建议。该技能支持在 Cursor 等 AI 编码工具中使用,当用户提供代码时,它会自动进行“代码优先审查”,重点关注状态变更导致的视图失效风暴、列表身份不稳定、`body` 中的繁重工作、布局抖动和图片解码等常见问题。如果代码审查无法得出明确结论,它会引导用户使用 Instruments 工具进行性能分析,并指导用户如何获取和分析 SwiftUI 时间线和时间分析器数据。最后,它会基于分析结果,提供有针对性的修复方案,例如缩小状态作用域、稳定 `ForEach` 身份、预计算数据、降低图片分辨率等,并总结性能改善的前后对比。

兼容平台~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. 代码优先审查

收集:

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

重点关注:

  • 由宽泛的状态变更导致的视图失效风暴 (View invalidation storms)。
  • 列表中的身份不稳定(id 频繁变化、每次渲染生成 UUID())。
  • body 中的繁重工作(格式化、排序、图片解码)。
  • 布局抖动(深层视图栈、GeometryReader、偏好链)。
  • 未经过下采样或调整尺寸的大图片。
  • 过度动画的视图层级(对大型视图树应用隐式动画)。

提供:

  • 可能的根因及代码引用。
  • 建议的修复和重构方案。
  • 如有必要,提供一个最小化复现示例或性能数据埋点建议。

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

说明如何使用 Instruments 收集数据:

  • 使用 Instruments 中的 SwiftUI 模板(Release 构建)。
  • 复现特定的交互(滚动、导航、动画)。
  • 捕获 SwiftUI 时间线 (SwiftUI timeline) 和时间分析器 (Time Profiler) 数据。
  • 导出或截图相关的通道和调用树。

要求用户提供:

  • 性能追踪文件导出或 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、掉帧、内存峰值)。

输出

提供:

  • 简短的指标表(如有前后对比数据)。
  • 主要问题列表(按影响程度排序)。
  • 建议的修复方案及其预估工作量。

参考资料

当用户提供时,将 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.

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

相关技能