Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the amount of files imported in a bridging header affect compile times?

Tags:

ios

swift

I have a theory, but I don't know how to test it. We have a fairly large iOS project of about 200 Swift files and 240 obj-C files (and an equal amount of header files). We're still on Swift 1.2, which means that quite regularly, the entire project gets rebuilt.

I've noticed that each .swift file takes about 4-6 seconds to compile; in other projects this is at most 2.

Now, I've noticed that in the build output, warnings generated in header files get repeated for every .swift file, which makes me believe the swift compiler will re-parse all headers included in the bridging header. Since we have ~160 import statements in the bridging header, this kinda adds up.

So, basic questions:

  • Does the size of our bridging header impact build times?
  • Is there any way to optimize this, so it parses the headers only once?
  • Does Swift 2 have this same issue?
  • Any other tricks to optimize this? Besides rewriting everything in Swift, that's kinda too labor-intensive a project for us to undertake at this time.
like image 333
cthulhu Avatar asked Dec 10 '15 10:12

cthulhu


2 Answers

Does the size of our bridging header impact build times?

Absolutely. The more files included in your bridging header, the more time it takes the compiler to parse them. This is what a Precompiled Header attempted to fix. PCH files have been phased out in favor of Modules.

Is there any way to optimize this, so it parses the headers only once?

To be honest I don't know, it depends on your source files and dependencies.

Does Swift 2 have this same issue?

Yes, but compiler optimization is much better in the newer versions of Xcode and Swift. Again, stressing Modules instead of Precompiled Header files here. I should note that it is possible to pass a pch file directly into clang, but that's rarely a good idea.

If you can, I'd experiment with using a pch header in the hybrid project. I'd also consider creating precompiled libraries or static frameworks to prevent the constant rebuilding of classes. There's a great WWDC video from 2013 which introduces modules, I highly recommend watching it.

References:

  • Modules and Precompiled Headers

  • WWDC 2014 Session 404 - Advances in Objective-C

  • Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?

like image 182
JAL Avatar answered Oct 30 '22 22:10

JAL


I can only talk from the experience I have at my previous workplace, meaning some things might have changed. Also, I'm not sure if this helps your specific case, since you mix Objective C and Swift which I have never done, but the theory is still sound.

In short, yes, the size of the bridging header affects compile times and you're correct that it parses it once for every file/inclusion.

The correct way to optimise this seemed to be to split the project up into modules (also called "frameworks" at some point) because each module is compiled individually and thus not recompiled if nothing has changed.

like image 24
kb. Avatar answered Oct 30 '22 22:10

kb.