Understanding and Improving SwiftUI Performance
Article Summary
Airbnb's SwiftUI features were re-rendering way more than they should. The culprit? SwiftUI's hidden diffing algorithm was working against them.
Cal Stephens and Miguel Jimenez from Airbnb's iOS team discovered that common SwiftUI patterns were causing massive performance hits. Their investigation revealed how SwiftUI's reflection-based diffing works and why it often fails with real-world code.
Key Takeaways
- SwiftUI diffs views by reflection, but closures and reference types break the algorithm
- Custom @Equatable macro lets views control diffing without manual boilerplate
- Breaking large view bodies into smaller diffable pieces prevents unnecessary re-evaluation
- Custom SwiftLint rule alerts when view complexity exceeds threshold of 10
- Search screen scroll hitches reduced 15% after applying these techniques
By understanding SwiftUI's diffing behavior and creating tooling to control it, Airbnb reduced scroll hitches by 15% on their most performance-critical screens.
About This Article
SwiftUI's reflection-based diffing algorithm breaks down when views have non-Equatable properties like closures from Airbnb's unidirectional data flow library. This makes entire views non-diffable and causes unnecessary re-evaluations.
Airbnb engineers built the @Equatable macro to generate custom Equatable conformances automatically. Developers can use @SkipEquatable to exclude non-diffable properties, and the build system catches regressions before they ship.
Airbnb applied @Equatable and split large view bodies into smaller diffable pieces. On their main Search screen, scroll hitches dropped by 15% while keeping their feature architecture flexible.