2.0 KiB
2.0 KiB
Workflow: Optimize iOS App Performance
<required_reading> Read NOW:
- references/performance.md
- references/cli-observability.md </required_reading>
Ask:
- What feels slow?
- Startup? Scrolling? Specific action?
- When did it start?
Step 2: Measure
CPU Profiling:
xcrun xctrace record \
--template 'Time Profiler' \
--device 'iPhone 16' \
--attach AppName \
--output profile.trace
Memory:
xcrun xctrace record --template 'Allocations' ...
Launch time:
# Add DYLD_PRINT_STATISTICS=1 to scheme environment
Step 3: Identify Bottlenecks
Look for:
- Functions with high "self time"
- Main thread blocking
- Repeated expensive operations
- Large allocations
SwiftUI re-renders:
var body: some View {
let _ = Self._printChanges()
// ...
}
Step 4: Common Optimizations
Main Thread
// Bad
let data = expensiveWork() // blocks UI
// Good
let data = await Task.detached { expensiveWork() }.value
SwiftUI
// Bad - rebuilds everything
struct BigView: View {
@State var a, b, c, d, e
}
// Good - isolated state
struct BigView: View {
var body: some View {
SubViewA() // has own @State
SubViewB() // has own @State
}
}
Lists
// Use LazyVStack for long lists
ScrollView {
LazyVStack {
ForEach(items) { ... }
}
}
Images
AsyncImage(url: url) { image in
image.resizable()
} placeholder: {
ProgressView()
}
Step 5: Measure Again
Did it improve? If not, revert.
Step 6: Performance Tests
func testScrollPerformance() {
measure(metrics: [XCTCPUMetric(), XCTMemoryMetric()]) {
// scroll simulation
}
}