Przejdź do treści
Biblioteka skilli
Programowanie · Analiza

Swift Performance Optimization Skill

/Swift Performance Optimization Skill

Bada zmierzone regresje wydajności aplikacji Apple: procesor, pamięć, start, przewijanie, animacje i sieć, z eksperymentami w Instruments.

Licencja
MIT
Ostatnia zmiana repozytorium
17.09.2026

Ocena i bezpieczeństwo

90/ 100Klasa: dobry
Format15 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
7
Skrypty
3
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 17.09.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co sprawdzić przed instalacją
  • 3 skrypty w paczce. Przejrzyj je przed instalacją, bo Claude może je uruchomić.

Skrypty: examples/example_image_loading.swift, examples/example_list_optimization.swift, examples/example_memory_management.swift

Co robi i kiedy

Bada zmierzone regresje wydajności aplikacji Apple: procesor, pamięć, start, przewijanie, animacje i sieć, z eksperymentami w Instruments.

Oryginalny opis (EN)

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.

Jak działa

  1. Baseline
  2. Measurement workflow
  3. Lifetime and memory safety
  4. Concurrency
  5. Collections and computation
  6. SwiftUI
  7. Images and reusable cells
  8. Shipping checks
  9. Resources
Sekcje z nagłówków SKILL.md, w oryginale · 133 linii, 961 słów
Podgląd SKILL.md
---
name: Swift Performance Optimization Skill
description: 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.
---

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

| Symptom | First evidence source |
|---|---|
| CPU-bound work | Time Profiler; inspect heavy stacks and self time |
| SwiftUI update cost | SwiftUI instrument plus Time Profiler |
| Scroll or animation stalls | Animation Hitches, Core Animation, signposts |
| Growth or leaks | Allocations, Leaks, Memory Graph, memgraphs |
| Slow launch | App Launch template and launch signposts |
| Battery or thermal issues | Energy Log and device testing |
| Field regressions | MetricKit 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.

```swift
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](examples/example_memory_management.swift).

## 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](examples/example_list_optimization.swift).

## 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](examples/example_image_loading.swift).

## 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](examples/example_list_optimization.swift)
- [Task lifetime example](examples/example_memory_management.swift)
- [Image downsampling and cell reuse](examples/example_image_loading.swift)
- [Performance investigation prompts](examples/prompts.md)
Pliki w paczce (7)
  • README.md 1 KB
  • SKILL.md 7 KB
  • agents/openai.yaml 1 KB
  • examples/example_image_loading.swift 6 KB
  • examples/example_list_optimization.swift 1 KB
  • examples/example_memory_management.swift 1 KB
  • examples/prompts.md 1 KB

Wersje

W bazie nie ma innych skilli o nazwie Swift Performance Optimization Skill.

Migawka bazy z 13.11.2026 · wersja paczki 5cffe3e4684f (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100swift-performance-optimization
    Profiluje wolne ekrany SwiftUI, optymalizuje listy, odciąża wątek główny i poprawia pamięć oraz procesor przy użyciu Instruments.
  • 91performance-profiling
    Prowadzi profilowanie aplikacji iOS: procesor, pamięć, sieć i zużycie energii, z narzędziami Instruments i strategiami optymalizacji.
  • 100swiftui-performance-audit
    Diagnozuje wydajność SwiftUI na podstawie kodu i dowodów: renderowanie, przewijanie, aktualizacje, CPU, pamięć, obrazy, układ i animacje, przed pomiarem w Instruments.
  • 100swift-performance-pro
    Diagnozuje wydajność aplikacji Swift: koszt renderowania SwiftUI, cykle silnych referencji, czas startu i pomiary w Instruments.
  • 90swift-core
    Pomaga wdrażać podstawy Swift 6, czyli współbieżność, architekturę, testowanie, internacjonalizację i wydajność, na każdej platformie Apple.
  • 100performance-profiling
    Prowadzi profilowanie aplikacji na platformy Apple z Instruments, diagnostyką Xcode i MetricKit: zawieszenia, CPU, wycieki pamięci, start aplikacji, bateria i os_signpost.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.