home / skills / hoangnguyen0403 / agent-skills-standard / performance

performance skill

/skills/ios/performance

This skill helps optimize iOS performance by guiding memory management, instrumentation usage, and multi-threading practices for responsive apps.

npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill performance

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

Files (2)
SKILL.md
1.6 KB
---
name: iOS Performance
description: Standards for Instruments, Memory Management, and Optimization.
metadata:
  labels: [ios, performance, instruments, leaks]
  triggers:
    files: ['**/*.swift']
    keywords: [Instruments, Allocations, Leaks, dequeueReusableCell]
---

# iOS Performance Standards

## **Priority: P0**

## Implementation Guidelines

### Diagnostic Tools

- **Instruments**: Regularly use **Allocations** and **Leaks** to detect memory issues.
- **Time Profiler**: Identify heavy CPU tasks and Main Thread stalls.
- **Network instrument**: Analyze request payload sizes and frequency.

### Optimization

- **Table/Collection Views**: Always use `dequeueReusableCell` and keep `cellForRowAt` logic lightweight.
- **Image Caching**: Use `SDWebImage` or `Kingfisher` for remote assets to prevent redundant fetching and main-thread decoding. (Note: `AsyncImage` lacks built-in caching; prioritize third-party for lists).
- **Background threads**: Offload expensive work (parsing, encryption) from the Main thread using GCD or Tasks.

### Diagnostics

- **Compiler Warnings**: Enable `SWIFT_TREAT_WARNINGS_AS_ERRORS` in Release builds.
- **Static Analyzer**: Use Xcode's "Analyze" (Product > Analyze) to find logic errors.

## Anti-Patterns

- **CPU work on Main Thread**: `**No parsing/processing on Main**: Use background thread.`
- **Force Cache Flushes**: `**No redundant cache clears**: Let the system handle low-memory warnings via AppDelegate.`
- **Retain Cycles**: `**Check for cycles in Instruments**: Use the Leaks instrument frequently.`

## References

- [Profiling & Optimization](references/implementation.md)

Overview

This skill codifies iOS performance standards for Instruments, memory management, and runtime optimization. It provides practical diagnostics, optimization patterns, and anti-patterns to keep iOS apps responsive and memory-efficient. The guidance focuses on reproducible tooling, safe threading, and cache strategies for production builds.

How this skill works

The skill prescribes tools and checks to run regularly: Instruments (Allocations, Leaks), Time Profiler, and Network instrumentation to surface memory leaks, CPU hotspots, and network inefficiencies. It defines actionable coding practices—lightweight table/collection cell code, image caching via established libraries, and backgrounding expensive work—to eliminate common causes of UI jank and memory pressure. It also enforces compile-time hygiene and static analysis for release builds.

When to use it

  • During feature development that affects lists, images, or heavy parsing.
  • Before release to audit memory, CPU, and network profiles.
  • When investigating user reports of freezes, crashes, or high battery usage.
  • While refactoring code that touches threading, caching, or networking.
  • When configuring CI to enforce build-time warnings and static analysis.

Best practices

  • Run Instruments regularly: Allocations and Leaks for memory, Time Profiler for CPU, Network for payload analysis.
  • Make cellForRowAt minimal and use dequeueReusableCell to avoid layout and allocation spikes.
  • Use trusted image caching libraries (SDWebImage, Kingfisher) for remote assets; avoid relying on AsyncImage for lists.
  • Offload parsing, decoding, encryption, and heavy work off the Main Thread via GCD/Tasks to keep the UI responsive.
  • Enable SWIFT_TREAT_WARNINGS_AS_ERRORS for Release and run Xcode’s Analyze to catch logic issues early.

Example use cases

  • Profiling a list screen that stutters when scrolling to identify heavy layout or decoding on the main thread.
  • Fixing a memory leak discovered via the Leaks instrument by locating retain cycles in closures and delegates.
  • Reducing network overhead by profiling request sizes and switching to compressed payloads after Network instrument findings.
  • Improving app startup time by moving expensive initialization into background tasks.
  • Enforcing a CI gate that fails on compiler warnings and runs static analysis before merging.

FAQ

How often should I run Instruments?

Run core Instruments (Allocations, Leaks, Time Profiler) weekly during development and before each release, and immediately when users report performance issues.

Is AsyncImage acceptable for production lists?

Avoid AsyncImage for high‑traffic lists because it lacks built-in caching and may cause redundant downloads and main-thread decoding; prefer SDWebImage or Kingfisher.

What’s the quickest way to find retain cycles?

Use the Leaks instrument and the Allocations timeline to inspect object lifetimes, and audit closures/delegate references for strong captures; prefer weak/unowned where appropriate.