home / skills / pproenca / dot-skills / swift-optimise

swift-optimise skill

/skills/.experimental/swift-optimise

This skill helps you optimize Swift and SwiftUI performance, migrate to Swift 6 concurrency, implement async/await, and boost render and animation efficiency.

npx playbooks add skill pproenca/dot-skills --skill swift-optimise

Review the files below or copy the command above to add this skill to your agents.

Files (23)
SKILL.md
4.4 KB
---
name: swift-optimise
description: Swift 6 and SwiftUI performance optimization, modern concurrency patterns, and animation performance. Covers async/await migration, Sendable/actor isolation, Swift 6 strict concurrency, view decomposition, render performance, Canvas/TimelineView, and fluid animations. This skill should be used when optimizing Swift/SwiftUI performance, migrating to Swift 6 concurrency, implementing async/await patterns, improving scroll performance, profiling render issues with Instruments, or building performant animations.
---

# Apple Swift/SwiftUI Performance Optimization Best Practices

Comprehensive guide for Swift and SwiftUI performance optimization. Contains 19 rules across 3 categories covering modern concurrency, render performance, and animation performance. Targets iOS 17+ with @Observable and Swift 6 strict concurrency.

## 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

## Rule Categories by Priority

| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Concurrency & Async | CRITICAL | `conc-` |
| 2 | Render & Scroll Performance | HIGH | `perf-` |
| 3 | Animation Performance | MEDIUM | `anim-` |

## Quick Reference

### 1. Concurrency & Async (CRITICAL)

- [`conc-combine-to-async`](references/conc-combine-to-async.md) - Replace Combine publishers with async/await
- [`conc-mainactor-isolation`](references/conc-mainactor-isolation.md) - Use @MainActor instead of DispatchQueue.main
- [`conc-swift6-sendable`](references/conc-swift6-sendable.md) - Adopt Sendable and Swift 6 strict concurrency
- [`conc-task-id-pattern`](references/conc-task-id-pattern.md) - Use .task(id:) for reactive data loading
- [`conc-actor-for-shared-state`](references/conc-actor-for-shared-state.md) - Replace lock-based shared state with actors
- [`conc-asyncsequence-streams`](references/conc-asyncsequence-streams.md) - Replace NotificationCenter observers with AsyncSequence

### 2. Render & Scroll Performance (HIGH)

- [`perf-view-decomposition`](references/perf-view-decomposition.md) - Decompose views to limit state invalidation blast radius
- [`perf-instruments-profiling`](references/perf-instruments-profiling.md) - Profile with SwiftUI Instruments before optimizing
- [`perf-lazy-containers`](references/perf-lazy-containers.md) - Use lazy containers for large collections
- [`perf-canvas-timeline`](references/perf-canvas-timeline.md) - Use Canvas and TimelineView for high-performance rendering
- [`perf-drawinggroup`](references/perf-drawinggroup.md) - Use drawingGroup for complex graphics
- [`perf-equatable-views`](references/perf-equatable-views.md) - Add Equatable conformance to prevent spurious redraws
- [`perf-task-modifier`](references/perf-task-modifier.md) - Use .task modifier instead of .onAppear for async work
- [`perf-async-image`](references/perf-async-image.md) - Use AsyncImage with caching strategy for remote images

### 3. Animation Performance (MEDIUM)

- [`anim-spring`](references/anim-spring.md) - Use spring animations as default
- [`anim-matchedgeometry`](references/anim-matchedgeometry.md) - Use matchedGeometryEffect for shared transitions
- [`anim-gesture-driven`](references/anim-gesture-driven.md) - Make animations gesture-driven
- [`anim-with-animation`](references/anim-with-animation.md) - Use withAnimation for state-driven transitions
- [`anim-transition-effects`](references/anim-transition-effects.md) - Apply transition effects for view insertion and removal

## How to Use

Read individual reference files for detailed explanations and code examples:

- [Section definitions](references/_sections.md) - Category structure and impact levels
- [Rule template](assets/templates/_template.md) - Template for adding new rules

## Reference Files

| File | Description |
|------|-------------|
| [references/_sections.md](references/_sections.md) | Category definitions and ordering |
| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |

Overview

This skill helps engineers optimize Swift 6 and SwiftUI apps for concurrency, render throughput, and animation fluidity. It focuses on migrating to async/await, adopting Sendable/actor isolation, decomposing views to reduce redraw blast radius, and improving scroll and animation performance. Use it to diagnose performance regressions and apply modern Swift concurrency and rendering patterns for iOS 17+.

How this skill works

The skill inspects common performance pain points: improper use of DispatchQueue/Combine, missing Sendable conformance, large SwiftUI view redraws, and costly drawing or animation patterns. It recommends concrete replacements and patterns—async/await for reactive flows, actors for shared state, .task(id:) for lifecycle-aware loading, lazy containers for lists, Canvas/TimelineView for custom rendering, and spring-based or gesture-driven animations. The guidance pairs profiling-first advice (SwiftUI Instruments) with code-level fixes to verify impact.

When to use it

  • Migrating codebases from Combine/DispatchQueue to async/await and Swift 6 concurrency
  • Resolving data races or compiler warnings related to Sendable and actor isolation
  • Reducing jank in scrolling lists or large collection views
  • Profiling and fixing unexpected SwiftUI view invalidations and redraws
  • Optimizing high-frequency rendering (Canvas, TimelineView) or complex drawing
  • Smoothing animations and transitions to meet frame-rate budgets

Best practices

  • Profile first with SwiftUI Instruments to find the true bottleneck before changing code
  • Migrate publishers and callbacks to async/await and use .task(id:) for reactive loads
  • Encapsulate shared mutable state in actors and mark UI-affecting code @MainActor
  • Decompose large views into smaller Equatable/isolated subviews to limit invalidation
  • Prefer lazy stacks/grids for long lists and use AsyncImage with caching for remote media
  • Use Canvas/TimelineView or drawingGroup for expensive rendering and spring animations for fluid motion

Example use cases

  • Converting NotificationCenter observers and Combine pipelines to AsyncSequence and async/await
  • Replacing a lock-based shared cache with an actor to eliminate race conditions
  • Improving list scroll performance by decomposing rows and switching to LazyVStack
  • Using Canvas and TimelineView to render a 60fps dashboard without allocating per-frame
  • Refactoring modal transitions to matchedGeometryEffect and gesture-driven spring animations

FAQ

Do I always need to mark types Sendable for Swift 6?

Aim to make shared mutable types Sendable; immutable value types usually are fine. Use sendable wrappers or actors for nontrivial shared state.

When should I use .task vs .onAppear for async work?

.task(id:) ties work to view identity and cancellation; prefer it for lifecycle-bound async work. Use .onAppear only for simple, synchronous setup.