Sunday, April 22, 2012

How to decrease build times / speed up compile time in XCode?


What strategies can be used in general to decrease build times for any XCode project? I'm mostly interested in XCode specific strategies.



I'm doing iPhone development using XCode, and my project is slowly getting bigger and bigger. I find the compile / link phases are starting to take more time than I'd like.



Currently, I'm:





  • Using Static Libraries to make it so most of my code doesn't need to be compiled everytime I clean and build my main project





  • Have removed most resources from my application, and test with a hard coded file system path in the iPhone simulator whenever possible so my resources don't have to constantly be packaged as I make changes to them.





I've noticed that the "Checking Dependencies" phase seems to take longer than I'd like. Any tips to decrease that as well would be appreciated!


Source: Tips4all

7 comments:

  1. Often, the largest thing you can do is to control your inclusion of header files.

    Including "extra" header files in source code dramatically slows down the compilation. This also tends to increase the time required for dependency checking.

    Also, using forward declaration instead of having headers include other headers can dramatically reduce the number of dependencies, and help all of your timings.

    ReplyDelete
  2. Personally I switched compiler to LLVM-Clang for my Mac development projects and have seen a dramatic decrease in build times. There's also the LLVM-GCC compiler but I'm not sure this would help with build times, still that's something you can try too if LLVM-Clang does not work for iPhone app compilation.

    I'm not 100% sure LLVM is supported for development on the iPhone but I think I remember reading in a news feed that it is. That's not an optimization you can implement in your code but it's worth the try!

    ReplyDelete
  3. Easy answer: add another machine running XCode on your local network. XCode incorporates distcc to do distributed compiles. It can even use Bonjour to find other build hosts, which simplifies the process of configuring this greatly. For large builds, distributing can get you a speed increase that is nearly linearly proportional to the number of build machines (2 machines takes half the time, three takes a third and so on).

    To see how to set this up, you can consult this development doc. It also features other useful build time improvement strategies, such as using precompiled headers and predictive builds.

    ReplyDelete
  4. You mentioned using static libs for your most-often used files to prevent compilation. You can accomplish something similar by putting headers to your code that it's frequently used but not in your static libs in the precompiled header. At least they'll only be compiled once.

    Care must be taken to avoid issues if you have multiple compilation types across your project (e.g. Obj-C, Obj-C++, C++).

    ReplyDelete
  5. The number of threads Xcode will use to perform tasks defaults to the same number of cores your CPU has. For example, a Mac with an Intel Core i7 has two cores, so by default Xcode will use a maximum of two threads. Since compile times are often I/O-bound rather than CPU-bound, increasing the number of threads Xcode uses can provide a significant performance boost for compiles.

    Try configuring Xcode to use 3, 4 or 8 threads and see which one provides the best performance for your use case.

    You can set the number of processes Xcode uses from Terminal as follows:

    defaults write com.apple.Xcode PBXNumberOfParallelBuildSubtasks 4

    Please see Xcode User Defaults for more information.

    ReplyDelete
  6. Hey there, I would recommend you to optimize your project's physical structure. There's some good reading about this ( at least in the C++ world ) , but I do objective-C and the same principles often apply.

    Here's a great article about project's physical structure optimization, which tends to improve compile times
    Games From Within: Physical Structure Part 1

    Good luck :)

    ReplyDelete
  7. If you're not using 8GB of RAM, upgrade now.

    I just upgraded my macbook pro from 4GB to 8GB. My project build time went from 2:10 to 0:45. I was floored by the improvement. It also makes web browsing for research snappier and general Xcode performance when indexing, etc.

    ReplyDelete