agentleFS
fal3fal3/claude-skills-collection/skills/swift-performance-optimization-skill/SKILL.md

Swift Performance Optimization Skill

Skill18 starsChanged 56 days agoMITView on GitHub

Use when investigating measured Swift or Apple-platform regressions in CPU, memory, launch, scrolling, animation hitches, image processing, energy, networking, or concurrency, or when designing performance tests and Instruments experiments. Do not use for speculative micro-optimization, ordinary refactoring, or a functional bug without performance evidence.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

16 rules. About 0 are common, 7 are distinctive, and 9 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈7

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 9

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Add a regression threshold where the workload is stable enough to automate.

    line 22
  2. Very specific:

    Use os_signpost or signposter intervals around important operations so traces answer product questions rather than only showing raw symbols.

    line 36
  3. Very specific:

    Use weak when the owner may legitimately disappear before the callback.

    line 41
  4. Very specific:

    Prefer structured async functions over storing completion closures.

    line 42
  5. Very specific:

    Never use [unowned self] in a repeating timer merely to silence a cycle.

    line 44
  6. Very specific:

    If a Foundation Timer is required, capture its owner weakly, store the timer, invalidate the old instance before starting another, and …

    line 80
  7. Very specific:

    Keep UI state on @MainActor.

    line 87
  8. Distinctive:

    Avoid spawning an unbounded task per item.

    line 88
  9. Distinctive:

    Check cancellation before expensive phases and before publishing results.

    line 89
  10. Distinctive:

    Do not trade actor safety for speed without race-focused tests and trace evidence.

    line 90
  11. Distinctive:

    Avoid accidental copy-on-write churn in hot paths, but prove it with Allocations or profiling.

    line 95
  12. Very specific:

    Keep work out of body: precompute formatting, filtering, decoding, and image processing at the correct layer.

    line 105
  13. Very specific:

    Use .equatable()/EquatableView only after measurement, and include every visible input in equality.

    line 107
  14. Very specific:

    Prefer .task(id:) for work tied to a view and input.

    line 108
  15. Distinctive:

    Use a bounded cache whose cost reflects decoded pixels.

    line 118
  16. Distinctive:

    Keep before/after traces with the scenario and build identifier.

    line 127

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Swift Performance Optimization Skill

Optimize from evidence. Preserve behavior, accessibility, data correctness, and lifecycle safety while changing performance characteristics.

Baseline

The copy-ready examples use Xcode 16, Swift 6 language mode with strict concurrency, and iOS 17. Most principles apply to older targets; verify each API against the app's actual minimum. Material from the OS 27 development cycle is beta relative to stable Xcode 26.6 and requires an explicit request, beta labeling, stable fallback, and availability gate.

Measurement workflow

  1. Define a user-visible symptom and a reproducible scenario.
  2. Record device, OS, build configuration, dataset, thermal state, and network conditions.
  3. Measure an optimized Release build on representative hardware. Simulator-only timing is not release evidence.
  4. Select the instrument that answers the hypothesis.
  5. Save a baseline trace or metric, change one variable, then repeat the same scenario.
  6. Confirm that the bottleneck moved and no memory, energy, correctness, or accessibility regression appeared.
  7. Add a regression threshold where the workload is stable enough to automate.

Useful tools include:

SymptomFirst evidence source
CPU-bound workTime Profiler; inspect heavy stacks and self time
SwiftUI update costSwiftUI instrument plus Time Profiler
Scroll or animation stallsAnimation Hitches, Core Animation, signposts
Growth or leaksAllocations, Leaks, Memory Graph, memgraphs
Slow launchApp Launch template and launch signposts
Battery or thermal issuesEnergy Log and device testing
Field regressionsMetricKit payloads and app-specific telemetry

Use os_signpost or signposter intervals around important operations so traces answer product questions rather than only showing raw symbols.

Lifetime and memory safety

  • A capture is a cycle only when the closure is retained along a path back to its owner. Do not add [weak self] mechanically to every closure.
  • Use weak when the owner may legitimately disappear before the callback. Use unowned only when the lifetime invariant is proven and documented; a wrong assumption traps.
  • Prefer structured async functions over storing completion closures.
  • Cancel owned tasks and invalidate repeating timers when the owner stops needing them and during teardown.
  • Never use [unowned self] in a repeating timer merely to silence a cycle.
import Foundation

@MainActor
final class PollingModel {
    private var task: Task<Void, Never>?
    private(set) var tickCount = 0

    func start() {
        stop()
        task = Task { [weak self] in
            while !Task.isCancelled {
                do {
                    try await Task.sleep(for: .seconds(1))
                } catch {
                    return
                }
                guard let self else { return }
                tickCount += 1
            }
        }
    }

    func stop() {
        task?.cancel()
        task = nil
    }

    deinit {
        task?.cancel()
    }
}

If a Foundation Timer is required, capture its owner weakly, store the timer, invalidate the old instance before starting another, and invalidate it on stop/deinit.

See the lifecycle-safe example.

Concurrency

  • Swift concurrency is the default for new asynchronous flows, but Dispatch and operation queues remain supported interoperability and scheduling tools.
  • Keep UI state on @MainActor. Move only verified expensive, concurrency-safe work away from it.
  • Avoid spawning an unbounded task per item. Use task groups with deliberate limits, an actor, an async sequence, or a bounded worker design.
  • Check cancellation before expensive phases and before publishing results.
  • Do not trade actor safety for speed without race-focused tests and trace evidence.

Collections and computation

  • Pick structures by operation: a Set or dictionary can replace repeated linear membership searches.
  • Avoid accidental copy-on-write churn in hot paths, but prove it with Allocations or profiling.
  • Reserve capacity only when the final scale is reasonably known.
  • Cache only expensive, repeatable results with a clear invalidation rule and a bounded memory policy.
  • Benchmark optimized code with representative data; debug-build microbenchmarks are misleading.

SwiftUI

  • Stable identity is mandatory for mutable collections. Prefer model IDs, not offsets or a new UUID computed during rendering.
  • List already realizes rows lazily. ScrollView plus LazyVStack offers different styling and interaction behavior; it is not inherently faster.
  • A body evaluation is value computation, not proof that all descendants were redrawn. Use the SwiftUI instrument to identify expensive updates.
  • Keep work out of body: precompute formatting, filtering, decoding, and image processing at the correct layer.
  • Extract views for responsibility and data-flow clarity. Do not claim extraction alone establishes an isolated rendering boundary.
  • Use .equatable()/EquatableView only after measurement, and include every visible input in equality.
  • Prefer .task(id:) for work tied to a view and input. It cancels prior work when the ID changes.

See the list identity example.

Images and reusable cells

  • Decode and downsample to the rendered pixel size rather than decoding full-resolution assets for thumbnails.
  • Validate HTTP responses, cancel obsolete requests, and check both cancellation and represented URL before assigning an image.
  • Reset image, identity, and task in prepareForReuse().
  • Lay out with constraints or layoutSubviews; a one-time frame set during initialization will not follow cell resizing.
  • Use a bounded cache whose cost reflects decoded pixels. Add collection-view prefetching only after measuring its value and cancel prefetches when appropriate.
  • Register cell classes/nibs, implement item counts, and avoid force-casting dequeued cells in copy-ready examples.

See the complete downsampling and reuse example.

Shipping checks

  • Compare percentile metrics, not only averages.
  • Set thresholds that account for device classes and natural variance.
  • Keep before/after traces with the scenario and build identifier.
  • Re-test memory warnings, background/foreground transitions, cancellation, large accessibility text, and low-power/thermal conditions.
  • State what was not measured; never market an optimization as proven from code inspection alone.

Resources

  • List performance example
  • Task lifetime example
  • Image downsampling and cell reuse
  • Performance investigation prompts

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.