Why Swift’s Type Checker Is So Slow
2 days ago
- Swift's bidirectional type inference can cause extremely long compile times, especially with complex expressions.
- The constraint solver explores many type and operator combinations, leading to exponential complexity.
- A common example: concatenating strings with integers using overloaded '+' operators can cause a 42-second compile with an unclear error.
- Explicitly converting types (e.g., `String(channel)`) drastically reduces compile time (0.19 seconds).
- Even valid expressions can be slow; adding `+ 1` to a simple expression changed compile time from 6.2 seconds to 0.7 seconds.
- Use temporary variables to break slow expressions into multiple statements and enable compile-time warnings.
- The Swift team acknowledges exponential worst-case performance as a known issue requiring a different type checker approach.