iOS and SwiftUI performance with AI agents: Instruments, hangs, hitches and fixes
Give the agent Apple's performance plan (measure, one change, re-measure) and an Instruments path: the SwiftUI template for long view bodies and frequent updates, hang and hitch thresholds from Apple's responsiveness docs, and a memgraph or MetricKit payload for leaks. Skills review the code; MCP servers drive xctrace and read the traces.
Last updated:
This page is about getting a coding agent to measure iOS and SwiftUI runtime performance, read Instruments or MetricKit evidence, and change one thing at a time. Compile-time / SPM build speed is a different job (Xcode Build Optimization).
What Apple says: plan, then Instruments
Apple's "Improving your app's performance" frames the work as a cycle: gather symptoms, measure, plan one change, implement, observe whether it helped. Skipping measurement is how agents "optimize" code that was never hot.
Useful thresholds from the responsiveness docs:
| Symptom | Apple's rough threshold |
|---|---|
| Hang (discrete tap/key → update) | under about 100 ms is rarely noticeable |
| Continuous interaction / animation work on the main thread | under one display refresh (8 or 17 ms) |
| Hitch rate (Organizer Hitches metric) | ≤ 10 ms/s good; ≤ 25 warning; ≤ 50 critical; > 50 immediate attention |
Apple also ships the Thread Performance Checker in Xcode to catch priority inversions and non-UI work on the main thread during development, with issues in the Issue navigator (and Generate for an intelligence fix).
Rules worth adding to AGENTS.md or CLAUDE.md:
Performance: do not refactor for speed without a measurement. Prefer Instruments (SwiftUI, Time Profiler, Allocations, Leaks) or MetricKit payloads over guessing. One change per iteration. Keep non-UI work off the main thread. After a fix, re-record the same scenario and compare.
Step 1: classify the symptom
Ask the agent to pick one bucket before it edits code (aligned with Apple's hang/hitch/launch/memory docs and with how the performance skills are split):
Slow launch — splash / Dock bounce lasts too long; risk of the iOS watchdog.
Hang — tap or key, then a pause before the UI updates.
Hitch / jank — scrolling or animation stutters (short interruptions in motion).
Memory — growth, jetsam, retain cycles,
EXC_RESOURCE/MEMORYwarnings.SwiftUI update storms — high CPU while "nothing is happening", or bodies that take too long.
If the agent cannot classify, have it ask for a reproduction and whether the issue appears in Debug or Release, on device or simulator.
Step 2: profile SwiftUI the way Apple documents
From "Understanding and improving SwiftUI performance":
In Xcode, Product > Profile.
Choose the SwiftUI template.
Start a deferred recording, interact with the feature under test, then stop.
The SwiftUI track's Update Groups lane marks view body calculations: orange above 500 µs, red above 1000 µs. Long View Body Updates covers long-running hosted UIKit/AppKit views. The Hitches timeline reports updates that missed the next display frame.
What the agent should do with that evidence: name the view and dependency that caused the update, then apply a common SwiftUI fix (narrow observation, stable identity, move heavy work out of body) — not a speculative rewrite of the whole screen.
Step 3: let the agent drive xctrace and memgraphs
Agents cannot usefully "see" Instruments.app by default. MCP servers wrap xcrun xctrace and related tools:
Record a named template against a simulator or device.
Export summaries (launch, CPU hot spots, allocations, leaks, network).
Ingest a
.memgraphor MetricKit.mxdiagnosticpayload.
Honest limits (from the tool READMEs and from Apple's CLI surface): sample-level Time Profiler detail depends on what xctrace symbolicates and exports; some servers document macOS / simulator recording gaps. Treat them as Instruments companions, and keep Product > Profile available when the MCP output is thin.
Step 4: fix one cause, then re-measure
Prompt pattern that keeps the agent honest:
Classify this as hang, hitch, launch, memory or SwiftUI update. Record the SwiftUI (or Time Profiler / Leaks) template for the reproduction. Quote the hottest update group or stack. Propose a single change with Apple's threshold in mind. Apply only that change, re-record, and report before/after.
For retain cycles, prefer a memgraph path (memorydetective) with a classified pattern and a verify step, not a blanket [weak self] pass.
iOS / SwiftUI performance tools for agents compared
| Tool / skill | What it does (from README) | Agents | Install (from README) | License | ★ (28 Oct) | Last push |
|---|---|---|---|---|---|---|
| iOS Performance Agent Skills (Livsy90) | Bundle of skills: launch, profiling, perceived performance, SwiftUI, concurrency, Swift runtime | Claude Code, Cursor, Codex, Gemini | npx skills add Livsy90/iOS-Performance-Agent-Skills --all | MIT | 119 | 12 Jul 2026 ⚠ |
| SwiftUI Performance Audit (Dimillian) | Codex skill: code-first audit of invalidation, identity churn, layout thrash and heavy render work, then profiling intake | Codex | Copy swiftui-performance-audit into $CODEX_HOME/skills | MIT | 4014 (repo) | 29 Mar 2026 ⚠ |
| InstrumentsMCP (nemanjavlahovic) | MCP server wrapping Instruments: record traces, drive the simulator, return structured profiling data | Claude Code, Cursor, Windsurf, any MCP | npx -y instrumentsmcp@latest | MIT | 19 | 11 Sep 2026 ⚠ |
| Xcode Instruments Trace Analyzer (jamesrochabrun) | Local MCP over xcrun xctrace: record/analyze traces, Time Profiler regressions, memory, network, energy, allocations/leaks, comparisons | Claude Code, Codex | claude mcp add --transport stdio --scope user xctrace-analyzer -- npx -y @xctrace-analyzer/mcp-server@latest | MIT | 44 | 15 May 2026 ⚠ |
| memorydetective (carloshpdoc) | MCP + CLI for retain-cycle hunting from .memgraph files, hangs, traces and MetricKit payloads (42 MCP tools documented in the README) | Claude Code, Cursor, any MCP | npm install -g memorydetective | Apache-2.0 | 14 | 25 May 2026 ⚠ |
| apple-instruments-mcp (dogo) | Python MCP over xctrace: record or analyze .trace bundles, launch/memory/leaks/CPU/network summaries, device and template listing via uvx | Claude Code, Cursor, Codex, Windsurf, any MCP | claude mcp add apple-instruments -- uvx apple-instruments-mcp | MIT | 3 | 17 Jul 2026 ⚠ |
| memorydetective plugin (carloshpdoc) | Claude Code marketplace plugin that auto-registers memorydetective, adds /perf-investigate and leak/hang/jank/launch/MetricKit slash commands | Claude Code | /plugin marketplace add carloshpdoc/memorydetective-plugin, /plugin install memorydetective@memorydetective-plugin | Apache-2.0 | 1 | 18 May 2026 ⚠ |
⚠ = no push for more than 4 weeks. Dimillian's skill lives in a large Skills repo, so the star count is for the whole repository. Nearby but out of scope here: Xcode Build Optimization for compile/SPM time.
Which one to pick:
Code review first (SwiftUI jank): SwiftUI Performance Audit (Codex) or the
swiftui-performanceskill inside Livsy90's bundle.Hands on Instruments from the agent: InstrumentsMCP or apple-instruments-mcp; Trace Analyzer if you already use its
claude mcp addline.Leaks / MetricKit / hang investigation in Claude Code: memorydetective, or the memorydetective plugin for slash commands.
Broad iOS performance coaching: install Livsy90's
--allbundle once, then add one MCP server.
A prompt that keeps the agent honest
Setting up the agent itself: Claude Code for iOS development, Codex for iOS development or Cursor rules for SwiftUI.
Our scrolling feed hitch rate is above 50 ms/s on an iPhone in Release. Use the SwiftUI Instruments template (Product > Profile) or an Instruments MCP server. Find the Update Groups over 1000 µs and the views that invalidate too often. Propose one change (narrow observation or stable identity). Do not rewrite the screen. Re-record the same scroll and report before/after hitch evidence.
FAQ
- Can Claude Code or Cursor profile an iOS app with Instruments?
- Yes. MCP servers such as InstrumentsMCP, Xcode Instruments Trace Analyzer and apple-instruments-mcp wrap xcrun xctrace so the agent can record traces, list templates and return structured summaries. They are companions to Instruments.app, not full replacements: sample-level Time Profiler detail still depends on what xctrace can export.
- What Instruments template should an agent use for SwiftUI jank?
- Apple's Understanding and improving SwiftUI performance page says: Product > Profile, choose the SwiftUI template, start a deferred recording, interact with the feature, then stop. The SwiftUI track shows Update Groups (orange above 500 microseconds, red above 1000) and related lanes for long view body updates.
- When is a delay a hang versus a hitch?
- Per Apple: a hang is a noticeable pause after a discrete interaction (tap, key); delays under about 100 ms are rarely noticeable. A hitch is a short interruption during continuous motion such as scrolling or animation. The Hitches metric in Xcode Organizer measures hitch rate in milliseconds of pause per second; at or below 10 ms/s is good, above 50 ms/s warrants immediate attention.
- How should an agent approach a slow launch?
- Follow Reducing your app's launch time: separate OS preparation from the app's own pre-UI work and from post-first-frame content loading. Cold, warm and prewarmed launches differ. On iOS the watchdog can terminate an app that takes too long. Prefer measuring with Instruments or MetricKit before moving SDK setup off the critical path.
- Do I need both a performance skill and an Instruments MCP server?
- They do different jobs. Skills such as iOS Performance Agent Skills and SwiftUI Performance Audit tell the agent what to inspect in code (invalidation, observation, launch). MCP servers give the agent hands on xctrace, memgraphs and MetricKit payloads. Install one skill and one MCP server that matches your client, then require a before/after measurement for every change.