swift-optimise
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseApple Swift/SwiftUI Performance Optimization Best Practices
Apple Swift/SwiftUI性能优化最佳实践
Comprehensive guide for Swift and SwiftUI performance optimization. Contains 19 rules across 3 categories covering modern concurrency, render performance, and animation performance. Targets iOS 26 / Swift 6.2 with @Observable and Swift 6 strict concurrency.
这是一份关于Swift和SwiftUI性能优化的综合指南,包含3个类别共19条规则,覆盖现代并发、渲染性能和动画性能三大领域。针对iOS 26 / Swift 6.2版本,适配@Observable特性及Swift 6严格并发模式。
Clinic Architecture Contract (iOS 26 / Swift 6.2)
诊所架构约定(iOS 26 / Swift 6.2)
All guidance in this skill assumes the clinic modular MVVM-C architecture:
- Feature modules import +
Domainonly (neverDesignSystem, never sibling features)Data - App target is the convergence point and owns , concrete coordinators, and Route Shell wiring
DependencyContainer - stays pure Swift and defines models plus repository,
Domain,*Coordinating, andErrorRoutingcontractsAppError - owns SwiftData/network/sync/retry/background I/O and implements Domain protocols
Data - Read/write flow defaults to stale-while-revalidate reads and optimistic queued writes
- ViewModels call repository protocols directly (no default use-case/interactor layer)
本指南中的所有内容均基于诊所模块化MVVM-C架构:
- Feature模块仅导入+
Domain(绝不导入DesignSystem或同级Feature模块)Data - App目标作为汇聚点,拥有、具体协调器及路由壳层的配置
DependencyContainer - 层为纯Swift实现,定义模型以及repository、
Domain、*Coordinating和ErrorRouting协议AppError - 层负责SwiftData/网络/同步/重试/后台I/O操作,并实现Domain层定义的协议
Data - 默认读写流程采用 stale-while-revalidate 读取策略和乐观队列写入策略
- ViewModel直接调用repository协议(默认不使用用例/交互器层)
When to Apply
适用场景
Reference these guidelines when:
- Migrating to Swift 6 strict concurrency (Sendable, actor isolation)
- Replacing Combine publishers with async/await
- Implementing @MainActor isolation and actor-based concurrency
- Decomposing views to reduce state invalidation blast radius
- Optimizing scroll and render performance with lazy containers
- Using Canvas/TimelineView for high-performance rendering
- Profiling with SwiftUI Instruments before optimizing
- Building performant spring animations and transitions
在以下场景中可参考本指南:
- 迁移至Swift 6严格并发模式(Sendable、actor隔离)
- 使用async/await替代Combine发布者
- 实现@MainActor隔离及基于actor的并发机制
- 拆分视图以缩小状态失效影响范围
- 使用懒加载容器优化滚动与渲染性能
- 采用Canvas/TimelineView实现高性能渲染
- 优化前使用SwiftUI Instruments进行性能分析
- 构建高性能弹簧动画与转场效果
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Concurrency & Async | CRITICAL | |
| 2 | Render & Scroll Performance | HIGH | |
| 3 | Animation Performance | MEDIUM | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 并发与异步 | 关键 | |
| 2 | 渲染与滚动性能 | 高 | |
| 3 | 动画性能 | 中 | |
Quick Reference
快速参考
1. Concurrency & Async (CRITICAL)
1. 并发与异步(关键)
- - Replace Combine publishers with async/await
conc-combine-to-async - - Use @MainActor instead of DispatchQueue.main
conc-mainactor-isolation - - Adopt Sendable and Swift 6 strict concurrency
conc-swift6-sendable - - Use .task(id:) for reactive data loading
conc-task-id-pattern - - Replace lock-based shared state with actors
conc-actor-for-shared-state - - Replace NotificationCenter observers with AsyncSequence
conc-asyncsequence-streams
- - 使用async/await替代Combine发布者
conc-combine-to-async - - 使用@MainActor替代DispatchQueue.main
conc-mainactor-isolation - - 适配Sendable及Swift 6严格并发模式
conc-swift6-sendable - - 使用.task(id:)实现响应式数据加载
conc-task-id-pattern - - 用actor替代基于锁的共享状态管理
conc-actor-for-shared-state - - 使用AsyncSequence替代NotificationCenter观察者
conc-asyncsequence-streams
2. Render & Scroll Performance (HIGH)
2. 渲染与滚动性能(高)
- - Decompose views to limit state invalidation blast radius
perf-view-decomposition - - Profile with SwiftUI Instruments before optimizing
perf-instruments-profiling - - Use lazy containers for large collections
perf-lazy-containers - - Use Canvas and TimelineView for high-performance rendering
perf-canvas-timeline - - Use drawingGroup for complex graphics
perf-drawinggroup - - Add Equatable conformance to prevent spurious redraws
perf-equatable-views - - Use .task modifier instead of .onAppear for async work
perf-task-modifier - - Use AsyncImage with caching strategy for remote images
perf-async-image
- - 拆分视图以缩小状态失效影响范围
perf-view-decomposition - - 优化前使用SwiftUI Instruments进行性能分析
perf-instruments-profiling - - 对大型集合使用懒加载容器
perf-lazy-containers - - 采用Canvas和TimelineView实现高性能渲染
perf-canvas-timeline - - 使用drawingGroup处理复杂图形
perf-drawinggroup - - 添加Equatable一致性以避免不必要的重绘
perf-equatable-views - - 使用.task修饰符替代.onAppear执行异步任务
perf-task-modifier - - 结合缓存策略使用AsyncImage加载远程图片
perf-async-image
3. Animation Performance (MEDIUM)
3. 动画性能(中)
- - Use spring animations as default
anim-spring - - Use matchedGeometryEffect for shared transitions
anim-matchedgeometry - - Make animations gesture-driven
anim-gesture-driven - - Use withAnimation for state-driven transitions
anim-with-animation - - Apply transition effects for view insertion and removal
anim-transition-effects
- - 优先使用弹簧动画
anim-spring - - 使用matchedGeometryEffect实现共享转场
anim-matchedgeometry - - 实现手势驱动的动画
anim-gesture-driven - - 使用withAnimation处理状态驱动的转场
anim-with-animation - - 为视图的插入和移除应用转场效果
anim-transition-effects
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
如需详细说明及代码示例,请阅读各参考文件:
- 章节定义 - 类别结构与影响级别说明
- 规则模板 - 新增规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新增规则模板 |