SourceKit has always been CPU and RAM hungry, slow and prone to crashes.
It gets (in my experience) a little better with Xcode 9.
One big problem is that many expressions in Swift have a large number of overloads. For type inference to work, all of them have to be tested. This is also the reason why compile times for Swift code are often a little bit longer.
Once SourceKit begins to work on such expressions, everything else has to wait.
You can help SourceKit by avoiding long expressions especially when using binary operators and chains of map
, flatMap
and filter
operations on collections and sequences, as the time complexity for resolving the return type on such expressions is exponential.
You can try to reduce long type inference times by declaring the types of variables (let a: X = expr
instead of let a = expr
). In my experience, this also helps in closures for chains map
, filter
and flatMap
chains ({ param -> Result in ...}
instead of { param in }
).
You can use the -Xfrontend -debug-time-function-bodies
flags in the Other Swift Flags build settings to get compile times for every function in your build report in Xcode, which can help you identify expressions which take long to process by the compiler and SourceKit. Detailed instructions can be found in this blog post.
Other than that I don't know of any other solutions.