Understanding and Improving SwiftUI Performance
Airbnb adopted SwiftUI in 2022, which improved engineer productivity but introduced new performance challenges. To address these issues, Airbnb created new tooling to identify and validate performance-critical code patterns. The company's feature architecture uses declarative UI patterns and unidirectional data flow systems, which simplified the adoption of SwiftUI. However, SwiftUI features using this architecture didn't perform as well as expected, and understanding SwiftUI's performance characteristics is crucial for building performant features. SwiftUI's view diffing algorithm, which determines when a view's body needs to be re-evaluated, is often overlooked and not officially documented. The algorithm compares each of the view's stored properties, but common code patterns can confound it, leading to unnecessary view body evaluations. To address this, Airbnb created a new @Equatable macro that generates Equatable conformances for views, allowing engineers to selectively decide which properties should be compared when diffing. This approach ensures that views are diffable and prevents regressions from creeping in later. Additionally, Airbnb implemented a custom SwiftLint rule to help engineers identify when a view body is too complex and needs to be refactored into smaller, diffable pieces. By breaking down views into smaller pieces, SwiftUI can efficiently update only the parts of the view that actually changed, maintaining performance as features grow more complex.