Any good practices around Swift code organization for faster incremental builds

I know that the Swift incremental compilation is not perfect and mostly conservative in determining what it needs to do. E.g. sometimes it recompile more files than required and also sometime loads more definitions than required from ObjC or swift modules. What I find unusual is, no documentation around what developers can do to help compiler to have most accurate approximation. Apart from doing obvious things like making declarations private/fileprivate what we can do

  • one class/struct/enum per file?
  • minimal possible ObjcC<->Swift interface?
  • diagnosis using -debug-time-expression-type-checking, -debug-time-function-bodies and fixing expressions, method bodies (mostly by rewriting expressions and providing type information explicitly)

e.g. code samples, like why sometime compiler become too conservative to marks file dirty

Is there a documentation around this that I can refer to?

2 Likes

Changing structure of types (adding/renaming/removing methods, fields) in files which also contain operator functions (function + etc.) leads to rebuild of every file that uses these operators on any type.

We talked about some of this in "Building Faster in Xcode" at last year's WWDC, but part of the reason there isn't a clear set of recommendations is because there's not much in the way of one-size-fits-all solutions, and a lot of the changes you could make have tradeoffs.

There's a bit more information on how the dependency analysis works in the Swift repo under docs/DependencyAnalysis.rst, but that's more for understanding than for action.

1 Like