Axiom swiftui performance

From ComeOnOliver/skillshub · View interactive version

Axiom swiftui performance is an AI agent skill from the GitHub repository ComeOnOliver/skillshub. Use when UI is slow, scrolling lags, animations stutter, or when asking 'why is my SwiftUI view slow', 'how do I optimize List performance', 'my app drops frames', 'view body is called too often', 'List is laggy' - SwiftUI performance optimization with Instruments 26 and WWDC 2025 patterns

How to use this skill

Skill file

SwiftUI Performance Optimization

When to Use This Skill

Use when:

Example Prompts

These are real questions developers ask that this skill is designed to answer:

1. "My app has janky scrolling and animations are stuttering. How do I figure out if SwiftUI is the cause?"

→ The skill shows how to use the new SwiftUI Instrument in Instruments 26 to identify if SwiftUI is the bottleneck vs other layers

2. "I'm using the new SwiftUI Instrument and I see orange/red bars showing long updates. How do I know what's causing them?"

→ The skill covers the Cause & Effect Graph patterns that show data flow through your app and which state changes trigger expensive updates

3. "Some views are updating way too often even though their data hasn't changed. How do I find which views are the problem?"

→ The skill demonstrates unnecessary update detection and Identity troubleshooting with the visual timeline

4. "I have large data structures and complex view hierarchies. How do I optimize them for SwiftUI performance?"

→ The skill covers performance patterns: breaking down view hierarchies, minimizing body complexity, and using the @Sendable optimization checklist

5. "We have a performance deadline and I need to understand what's slow in SwiftUI. What are the critical metrics?"

→ The skill provides the decision tree for prioritizing optimizations and understands pressure scenarios with professional guidance for trade-offs


Overview

Core Principle: Ensure your view bodies update quickly and only when needed to achieve great SwiftUI performance.

NEW in WWDC 2025: Next-generation SwiftUI instrument in Instruments 26 provides comprehensive performance analysis with:

Key Performance Problems:

  1. Long View Body Updates — View bodies taking too long to run
  2. Unnecessary View Updates — Views updating when data hasn't actually changed

iOS 26 Framework Performance Improvements

"Performance improvements to the framework benefit apps across all of Apple's platforms, from our app to yours." — WWDC 2025-256

SwiftUI in iOS 26 includes major performance wins that benefit all apps automatically. These improvements work alongside the new profiling tools to make SwiftUI faster out of the box.

List Performance (macOS Focus)

Massive gains for large lists
List(trips) { trip in // 100k+ items
    TripRow(trip: trip)
}
// iOS 26: Loads 6x faster, updates 16x faster on macOS
// All platforms benefit from performance improvements
Impact on your app

Scrolling Performance

Reduced dropped frames during high-speed scrolling

SwiftUI has improved scheduling of user interface updates on iOS and macOS. This improves responsiveness and lets SwiftUI do even more work to prepare for upcoming frames. All in all, it reduces the chance of your app dropping a frame while scrolling quickly at high frame rates.

Key improvements
  1. Better frame scheduling — SwiftUI gets more time to prepare for upcoming frames
  2. Improved responsiveness — UI updates scheduled more efficiently
  3. Fewer dropped frames — Especially during quick scrolling at 120Hz (ProMotion)
When you'll notice

Nested ScrollViews with Lazy Stacks

Photo carousels and multi-axis scrolling now properly optimize
ScrollView(.horizontal) {
    LazyHStack {
        ForEach(photoSets) { photoSet in
            ScrollView(.vertical) {
                LazyVStack {
                    ForEach(photoSet.photos) { photo in
                        PhotoView(photo: photo)
                    }
                }
            }
        }
    }
}
// iOS 26: Nested scrollviews now properly delay loading with lazy stacks
// Great for photo carousels, Netflix-style layouts, multi-axis content

Before iOS 26 Nested ScrollViews didn't properly delay loading lazy stack content, causing all nested content to load immediately.

After iOS 26 Lazy stacks inside nested ScrollViews now delay loading until content is about to appear, matching the behavior of single-level ScrollViews.

Use cases

SwiftUI Performance Instrument Enhancements

New lanes in Instruments 26

The SwiftUI instrument now includes dedicated lanes for:

  1. Long View Body Updates — Identify expensive body computations
  2. Platform View Updates — Track UIKit/AppKit bridging performance (Long Representable Updates)
  3. Other Long Updates — All other types of long SwiftUI work

These lanes are covered in detail in the next section.

Performance Improvement Summary

Automatic wins (recompile only)

No code changes required — rebuild with iOS 26 SDK to get these improvements.

Cross-reference SwiftUI 26 Features (swiftui-26-ref skill) — Comprehensive guide to all iOS 26 SwiftUI changes


The SwiftUI Instrument (Instruments 26)

Getting Started

Requirements:

Launch:

  1. Open project in Xcode
  2. Press Command-I to profile
  3. Choose SwiftUI template from template chooser
  4. Click Record button

Template Contents

The SwiftUI template includes three instruments:

  1. SwiftUI Instrument (NEW) — Identifies performance issues in SwiftUI code
  2. Time Profiler — Shows CPU work samples over time
  3. Hangs and Hitches — Tracks app responsiveness

SwiftUI Instrument Track Lanes

Lane 1: Update Groups
Lane 2: Long View Body Updates
Lane 3: Long Representable Updates
Lane 4: Other Long Updates

Color-Coding System

Updates shown in orange and red based on likelihood to cause hitches:

Note: Whether updates actually result in hitches depends on device conditions, but red updates are the highest priority.


Understanding the Render Loop

Normal Frame Rendering

Frame 1:
├─ Handle events (touches, key presses)
├─ Update UI (run view bodies)
│  └─ Complete before frame deadline ✅
├─ Hand off to system
└─ System renders → Visible on screen

Frame 2:
├─ Handle events
├─ Update UI
│  └─ Complete before frame deadline ✅
├─ Hand off to system
└─ System renders → Visible on screen

Result: Smooth, fluid animations

Frame with Hitch (Long View Body)

Frame 1:
├─ Handle events
├─ Update UI
│  └─ ONE VIEW BODY TOO SLOW
│  └─ Runs past frame deadline ❌
├─ Miss deadline
└─ Previous frame stays visible (HITCH)

Frame 2: (Delayed)
├─ Handle events (delayed by 1 frame)
├─ Update UI
├─ Hand off to system
└─ System renders → Finally visible

Result: Previous frame visible for 2+ frames = animation stutter

Frame with Hitch (Too Many Updates)

Frame 1:
├─ Handle events
├─ Update UI
│  ├─ Update 1 (fast)
│  ├─ Update 2 (fast)
│  ├─ Update 3 (fast)
│  ├─ ... (100 more fast updates)
│  └─ Total time exceeds deadline ❌
├─ Miss deadline
└─ Previous frame stays visible (HITCH)

Result: Many small updates add up to miss deadline

Key Insight: View body runtime matters because missing frame deadlines causes hitches, making animations less fluid.

Reference:


Problem 1: Long View Body Updates

Identifying Long Updates

  1. Record trace in Instruments with SwiftUI template
  2. Look at Long View Body Updates lane — any orange/red bars?
  3. Expand SwiftUI track to see subtracks
  4. Select View Body Updates subtrack
  5. Filter to long updates:

Analyzing with Time Profiler

Workflow:

  1. Find long update in Long View Body Updates summary
  2. Hover over view name → Click arrow → "Show Updates"
  3. Right-click on long update → "Set Inspection Range and Zoom"
  4. Switch to Time Profiler instrument track

What you see:

Finding the bottleneck:

  1. Option-click to expand main thread call stack
  2. Command-F to search for your view name (e.g., "LandmarkListItemView")
  3. Identify expensive operations in time column

Common Expensive Operations

Formatter Creation (Very Expensive)

❌ WRONG - Creating formatters in view body:

struct LandmarkListItemView: View {
    let landmark: Landmark
    @State private var userLocation: CLLocation

    var distance: String {
        // ❌ Creating formatters every time body runs
        let numberFormatter = NumberFormatter()
        numberFormatter.maximumFractionDigits = 1

        let measurementFormatter = MeasurementFormatter()
        measurementFormatter.numberFormatter = numberFormatter

        let meters = userLocation.distance(from: landmark.location)
        let measurement = Measurement(value: meters, unit: UnitLength.meters)
        return measurementFormatter.string(from: measurement)
    }

    var body: some View {
        HStack {
            Text(landmark.name)
            Text(distance) // Calls expensive distance property
        }
    }
}

Why it's slow:

✅ CORRECT - Cache formatters centrally:

@Observable
class LocationFinder {
    private let formatter: MeasurementFormatter
    private let landmarks: [Landmark]
    private var distanceCache: [Landmark.ID: String] = [:]

    init(landmarks: [Landmark]) {
        self.landmarks = landmarks

        // Create formatters ONCE during initialization
        let numberFormatter = NumberFormatter()
        numberFormatter.maximumFractionDigits = 1

        self.formatter = MeasurementFormatter()
        self.formatter.numberFormatter = numberFormatter

        updateDistances()
    }

    func didUpdateLocations(_ locations: [CLLocation]) {
        guard let location = locations.last else { return }
        updateDistances(from: location)
    }

    private func updateDistances(from location: CLLocation? = nil) {
        guard let location else { return }

        for landmark in landmarks {
            let meters = location.distance(from: landmark.location)
            let measurement = Measurement(value: meters, unit: UnitLength.meters)
            distanceCache[landmark.id] = formatter.string(from: measurement)
        }
    }

    func distanceString(for landmarkID: Landmark.ID) -> String {
        distanceCache[landmarkID] ?? "Unknown"
    }
}

struct LandmarkListItemView: View {
    let landmark: Landmark
    @Environment(LocationFinder.self) private var locationFinder

    var body: some View {
        HStack {
            Text(landmark.name)
            Text(locationFinder.distanceString(for: landmark.id)) // ✅ Fast lookup
        }
    }
}

Benefits:

Other Expensive Operations

Complex Calculations:

// ❌ Don't calculate in view body
var body: some View {
    let result = expensiveAlgorithm(data) // Complex math, sorting, etc.
    Text("\(result)")
}

// ✅ Calculate in model, cache result
@Observable
class ViewModel {
    private(set) var result: Int = 0

    func updateData(_ data: [Int]) {
        result = expensiveAlgorithm(data) // Calculate once
    }
}

Network/File I/O:

// ❌ NEVER do I/O in view body
var body: some View {
    let data = try? Data(contentsOf: fileURL) // ❌ Synchronous I/O
    // ...
}

// ✅ Load asynchronously, store in state
@State private var data: Data?

var body: some View {
    // Just read state
}
.task {
    data = try? await loadData() // Async loading
}

Image Processing:

// ❌ Don't process images in view body
var body: some View {
    let thumbnail = image.resized(to: CGSize(width: 100, height: 100))
    Image(uiImage: thumbnail)
}

// ✅ Process images in background, cache
.task {
    await processThumbnails()
}

Verifying the Fix

After implementing fix:

  1. Record new trace in Instruments
  2. Check Long View Body Updates summary
  3. Verify your view is gone from the list (or significantly reduced)

Note: Updates at app launch may still be long (building initial view hierarchy) — this is normal and won't cause hitches during scrolling.


Problem 2: Unnecessary View Updates

Why Unnecessary Updates Matter

Even if individual updates are fast, too many updates add up:

100 fast updates × 2ms each = 200ms total
→ Misses 16.67ms frame deadline
→ Hitch

Identifying Unnecessary Updates

Scenario: Tapping a favorite button on one item updates ALL items in a list.

Expected: Only the tapped item updates.

Actual: All visible items update.

How to find:

  1. Record trace with user interaction in mind
  2. Highlight relevant portion of timeline
  3. Expand hierarchy in detail pane
  4. Count updates — more than expected?

Understanding SwiftUI's Data Model

SwiftUI uses AttributeGraph to define dependencies and avoid re-running views unnecessarily.

Attributes & Dependencies
struct OnOffView: View {
    @State private var isOn: Bool = false

    var body: some View {
        Text(isOn ? "On" : "Off")
    }
}

What SwiftUI creates:

  1. View attribute — Stores view struct (recreated frequently)
  2. State storage — Keeps isOn value (persists entire view lifetime)
  3. Signal attribute — Tracks when state changes
  4. View body attribute — Depends on state signal
  5. Text attributes — Depend on view body

When state changes:

  1. Create transaction (scheduled change for next frame)
  2. Mark signal attribute as outdated
  3. Walk dependency chain, marking dependent attributes as outdated (just set flag - fast)
  4. Before rendering, update all outdated attributes
  5. View body runs again, producing new Text struct
  6. Continue updates until all needed attributes updated
  7. Render frame

The Cause & Effect Graph

Purpose: Visualize what marked your view body as outdated.

Example graph:

[Gesture] → [State Change] → [View Body Update]
               ↓
         [Other View Bodies]

Node types:

Selecting nodes:

Accessing graph:

  1. Detail pane → Expand hierarchy to find view
  2. Hover over view name → Click arrow
  3. Choose "Show Cause & Effect Graph"

Example: Favorites List Problem

Problem:

@Observable
class ModelData {
    var favoritesCollection: Collection // Contains array of favorites

    func isFavorite(_ landmark: Landmark) -> Bool {
        favoritesCollection.landmarks.contains(landmark) // ❌ Depends on whole array
    }
}

struct LandmarkListItemView: View {
    let landmark: Landmark
    @Environment(ModelData.self) private var modelData

    var body: some View {
        HStack {
            Text(landmark.name)
            Button {
                modelData.toggleFavorite(landmark) // Modifies array
            } label: {
                Image(systemName: modelData.isFavorite(landmark) ? "heart.fill" : "heart")
            }
        }
    }
}

What happens:

  1. Each view calls isFavorite(), accessing favoritesCollection.landmarks array
  2. @Observable creates dependency: Each view depends on entire array
  3. Tapping button calls toggleFavorite(), modifying array
  4. All views marked as outdated (array changed)
  5. All view bodies run (even though only one changed)

Cause & Effect Graph shows:

[Gesture] → [favoritesCollection.landmarks array change] → [All LandmarkListItemViews update]

✅ Solution — Granular Dependencies:

@Observable
class LandmarkViewModel {
    var isFavorite: Bool = false

    func toggleFavorite() {
        isFavorite.toggle()
    }
}

@Observable
class ModelData {
    private(set) var viewModels: [Landmark.ID: LandmarkViewModel] = [:]

    init(landmarks: [Landmark]) {
        for landmark in landmarks {
            viewModels[landmark.id] = LandmarkViewModel()
        }
    }

    func viewModel(for landmarkID: Landmark.ID) -> LandmarkViewModel? {
        viewModels[landmarkID]
    }
}

struct LandmarkListItemView: View {
    let landmark: Landmark
    @Environment(ModelData.self) private var modelData

    var body: some View {
        if let viewModel = modelData.viewModel(for: landmark.id) {
            HStack {
                Text(landmark.name)
                Button {
                    viewModel.toggleFavorite() // ✅ Only modifies this view model
                } label: {
                    Image(systemName: viewModel.isFavorite ? "heart.fill" : "heart")
                }
            }
        }
    }
}

Result:

Cause & Effect Graph shows:

[Gesture] → [Single LandmarkViewModel change] → [Single LandmarkListItemView update]

Environment Updates

How Environment Works

struct EnvironmentValues {
    // Dictionary-like value type
    var colorScheme: ColorScheme
    var locale: Locale
    // ... many more values
}

Each view has dependency on entire EnvironmentValues struct via @Environment property wrapper.

What Happens on Environment Change

  1. Any environment value changes (e.g., dark mode enabled)
  2. All views with @Environment dependency notified
  3. Each view checks if the specific value it reads changed
  4. If value changed → View body runs
  5. If value didn't change → SwiftUI skips running view body (already up-to-date)

Cost: Even when body doesn't run, there's still cost of checking for updates.

Environment Update Nodes in Graph

Two types:

  1. External Environment — App-level changes from outside SwiftUI (color scheme, accessibility settings)
  2. EnvironmentWriter — Changes inside SwiftUI via .environment() modifier

Example:

View1 reads colorScheme:
[External Environment] → [View1 body runs] ✅

View2 reads locale (doesn't read colorScheme):
[External Environment] → [View2 body check] (body doesn't run - dimmed icon)

Same update shows as multiple nodes: Hover/click any node for same update → all highlight together.

Environment Performance Warning

⚠️ AVOID storing frequently-changing values in environment:

// ❌ DON'T DO THIS
struct ContentView: View {
    @State private var scrollOffset: CGFloat = 0

    var body: some View {
        ScrollView {
            // Content
        }
        .environment(\.scrollOffset, scrollOffset) // ❌ Updates on every scroll frame
        .onPreferenceChange(ScrollOffsetKey.self) { offset in
            scrollOffset = offset
        }
    }
}

Why it's bad:

✅ Better approach:

// Pass via parameter or @Observable model
struct ContentView: View {
    @State private var scrollViewModel = ScrollViewModel()

    var body: some View {
        ScrollView {
            ChildView(scrollViewModel: scrollViewModel) // Direct parameter
        }
    }
}

Environment is great for:


Performance Optimization Checklist

Before Profiling

During Profiling

Analyzing Long View Body Updates

Analyzing Unnecessary Updates

After Optimization


Production Pressure: When Performance Issues Hit Live

The Problem

When performance issues appear in production, you face competing pressures:

The issue: Quick fixes based on guesses fail 80% of the time and waste your deployment window.

Red Flags — Resist These Pressure Tactics

If you hear ANY of these under deadline pressure, STOP and use SwiftUI Instrument:

One SwiftUI Instrument Recording (30-Minute Protocol)

Under production pressure, one good diagnostic recording beats random fixes:

Time Budget:

Total: 25 minutes to know EXACTLY what's slow

Then:

Total time: 1 hour 15 minutes for diagnosis + fix, leaving 4+ hours for edge case testing.

Comparing Time Costs

Option A: Guess and Pray
Option B: One SwiftUI Instrument Recording

Time cost of being wrong:

Real-World Example: Tab Transition Sluggishness

Pressure scenario:

Bad approach (Option A):

Junior suggests: "Add .compositingGroup() to TabView"
You: "Sure, let's try it"
Result: Ships without profiling
Outcome: Doesn't fix issue (compositing wasn't the problem)
Next: 24 hours until next deploy window
VP update: "Users still complaining"

Good approach (Option B):

"Running one SwiftUI Instrument recording of tab transition"
[25 minutes later]
"SwiftUI Instrument shows Long View Body Updates in ProductGridView during transition.
Cause & Effect Graph shows ProductList rebuilding entire grid unnecessarily.
Applying view identity fix (`.id()`) to prevent unnecessary updates"
[30 minutes to implement and test]
"Deployed at 1.5 hours. Verified with Instruments. Tab transitions now smooth."

When to Accept the Pressure (And Still be Right)

Sometimes managers are right to push for speed. Accept the pressure IF:

Document your decision:

Slack to VP + team:

"Completed diagnostic: ProductGridView rebuilding unnecessarily during
tab transitions (confirmed in SwiftUI Instrument, Long View Body Updates).
Applied view identity fix. Verified in Instruments - transitions now 16.67ms.
Deploying now."

This shows:

If You Still Get It Wrong After Profiling

Honest admission:

"SwiftUI Instrument showed ProductGridView was the bottleneck.
Applied view identity fix, but performance didn't improve as expected.
Root cause is deeper than expected. Requiring architectural change.
Shipping animation disable (.animation(nil) on TabView) as mitigation.
Proper fix queued for next release cycle."

This is different from guessing:

Decision Framework Under Pressure

Before shipping ANY fix

| Question | Answer Yes? | Action |

|----------|-------------|--------|

| Have you run SwiftUI Instrument? | No | STOP - 25 min diagnostic |

| Do you know which view is expensive? | No | STOP - review Cause & Effect Graph |

| Can you explain in one sentence why the fix helps? | No | STOP - you're guessing |

| Have you verified the fix in Instruments? | No | STOP - test before shipping |

| Did you consider simpler explanations? | No | STOP - check documentation first |

Answer YES to all five → Ship with confidence


Common Patterns & Solutions

Pattern 1: List Item Dependencies

Problem: Updating one item updates entire list

Solution: Per-item view models with granular dependencies

// ❌ Shared dependency
@Observable
class ListViewModel {
    var items: [Item] // All views depend on whole array
}

// ✅ Granular dependencies
@Observable
class ListViewModel {
    private(set) var itemViewModels: [Item.ID: ItemViewModel]
}

@Observable
class ItemViewModel {
    var item: Item // Each view depends only on its item
}

Pattern 2: Computed Properties in View Bodies

Problem: Expensive computation runs every render

Solution: Move to model, cache result

// ❌ Compute in view
struct MyView: View {
    let data: [Int]

    var body: some View {
        Text("\(data.sorted().last ?? 0)") // Sorts every render
    }
}

// ✅ Compute in model
@Observable
class ViewModel {
    var data: [Int] {
        didSet {
            maxValue = data.max() ?? 0 // Compute once when data changes
        }
    }
    private(set) var maxValue: Int = 0
}

struct MyView: View {
    @Environment(ViewModel.self) private var viewModel

    var body: some View {
        Text("\(viewModel.maxValue)") // Just read cached value
    }
}

Pattern 3: Formatter Reuse

Problem: Creating formatters repeatedly

Solution: Create once, reuse

// ❌ Create every time
var body: some View {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    Text(formatter.string(from: date))
}

// ✅ Reuse formatter
class Formatters {
    static let shortDate: DateFormatter = {
        let f = DateFormatter()
        f.dateStyle = .short
        return f
    }()
}

var body: some View {
    Text(Formatters.shortDate.string(from: date))
}

Pattern 4: Environment for Stable Values Only

Problem: Rapidly-changing environment values

Solution: Use direct parameters or models

// ❌ Frequently changing in environment
.environment(\.scrollPosition, scrollPosition) // 60+ updates/second

// ✅ Direct parameter or model
ChildView(scrollPosition: scrollPosition)

iOS 26 Performance Improvements

Automatic improvements when building with Xcode 26 (no code changes needed):

Lists

SwiftUI Instrument


Debugging Performance Issues

Step-by-Step Process

  1. Reproduce issue — Identify specific slow interaction
  2. Profile with Instruments — SwiftUI template
  3. Check Update Groups lane — SwiftUI doing work when slow?
  4. Identify problem type:
  1. Use Time Profiler for long updates (find expensive operation)
  2. Use Cause & Effect Graph for unnecessary updates (find dependency issue)
  3. Implement fix
  4. Verify with new trace

When SwiftUI Isn't the Problem

Update Groups lane empty during performance issue?

Problem likely elsewhere:

Next steps:


Real-World Impact

Example: Landmarks App (from WWDC 2025)

Before optimization:

After optimization:

Improvements:


Resources

WWDC: 2025-306

Docs: /xcode/understanding-hitches-in-your-app, /xcode/analyzing-hangs-in-your-app, /xcode/optimizing-your-app-s-performance

Skills: axiom-swiftui-debugging-diag, axiom-swiftui-debugging, axiom-memory-debugging, axiom-xcode-debugging


Key Takeaways

  1. Fast view bodies — Keep them quick so SwiftUI has time to get UI on screen without delay
  2. Update only when needed — Design data flow to update views only when necessary
  3. Careful with environment — Don't store frequently-changing values
  4. Profile early and often — Use Instruments during development, not just when problems arise
  5. Greatest takeaway: Ensure your view bodies update quickly and only when needed to achieve great SwiftUI performance

Xcode: 26+

Platforms: iOS 26+, iPadOS 26+, macOS Tahoe+, axiom-visionOS 3+

History: See git log for changes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.