Bryan Irace wrote a great post a earlier this year, detailing how to measure the compilation time of the swift functions that you write. It’s a great technique for spot checking your slow functions, but you’ll have to remember to run it every few months, to catch any new slow functions you write.
In May, Jordan Rose added a Swift flag for emitting warnings whenever a function takes longer than some threshold to compile. You can leave this flag enabled in your project, and it will warn you whenever any slow functions are added to the project.
To add the flag:
- Navigate to your target in Xcode (not the project)
- Click the “Build Settings” tab
- Make sure that “All” settings are being shown, not “Basic”
- Scroll to (or search for) “Other Swift Flags”
- Add two flags:
-warn-long-function-bodies=100, where 100 is the number of milliseconds you’d like the warning threshold to be
- Compile your project
When you compile, you’ll notice some new warnings. They look something like:
Getter ‘uniqueID’ took 105ms to type-check (limit: 100ms)
You can click on the warning to jump to the function in question. Rewrite it to take less time to compile.
Sadly, in the commit message adding this feature, Jordan writes,
As a frontend option, this is UNSUPPORTED and may be removed without notice at any future date.
This feature is very useful, and I hope it stays around as long as Swift compile times are flaky.
Thanks to Olivier, Caleb and Sam.