Swift Performance Optimization
Language-level performance patterns for memory efficiency, runtime speed, and zero-cost abstractions. Profile first, optimize later — use this skill after Instruments identifies Swift code as the bottleneck.
When to Use
Use this skill when:
- Time Profiler shows Swift code as hotspot
- Excessive memory allocations or retain/release traffic
- Implementing performance-critical algorithms
- Writing framework code with performance requirements
- Optimizing tight loops or frequently called methods
Do NOT use for
- First-step optimization (use performance-profiling first)
- SwiftUI performance (use swiftui-performance)
- Premature optimization
Example Prompts
- "Profiler shows excessive copying — how do I eliminate it?"
- "Retain/release overhead in Time Profiler — how to reduce?"
- "Generic code is slow in hot path"
- "Actor overhead causing UI jank"
What This Skill Provides
Four Principles of Swift Performance
From WWDC 2024-10217 — the organizing mental model for all optimizations:
- Function Calls – Static vs dynamic dispatch, generic specialization
- Memory Allocation – Stack vs heap, when each occurs
- Memory Layout – Contiguous vs pointer-chasing, cache lines
- Value Copying – COW triggers, defensive copies, ARC traffic
Core Topics
- Noncopyable Types – Swift 6
~Copyablefor types that should never be copied - Copy-on-Write – Use
isKnownUniquelyReferenced(),reserveCapacity(), and avoid defensive copies - Value vs Reference – Structs under 64 bytes are fast; larger need indirect storage
- ARC Optimization –
unownedis ~2x faster thanweak; closure capture costs (escaping vs non-escaping) - Generics – Use
someoveranyfor static dispatch - Collection Performance –
ContiguousArrayis ~15% faster thanArray; InlineArray for fixed sizes - Concurrency – Actor hops cost ~100μs; batch calls
- Memory Layout – Struct padding, exclusivity checks, cache-friendly data structures
- Span Types – Safe zero-copy memory access with OutputSpan for initialization
Each topic ships with before/after code patterns, measured costs, and a code-review checklist — those live in the skill itself.
Related
- performance-profiling – Use this first to identify bottlenecks
- swift-concurrency – Correctness-focused concurrency
- swiftui-performance – SwiftUI-specific optimization
Resources
WWDC: 2025-312, 2024-10217, 2024-10170, 2021-10216, 2016-416