Recently, I was tasked with answering the following question (actually two questions, but we’ll get to the second one at the end):
going to compile faster than this:
Restated more verbosely: in this era of modules, is it faster to import only the individual files you need from an module umbrella header, or does it make no difference, so you can rely on the simplicity of always importing the entire module in all your files?
I had always assumed the latter, but now I was being asked to prove it.
To do that, I made a new GitHub project, Import-Ant. Inside of it, you’ll find five Xcode projects: four test projects and a test builder project.
You may ask: why bother with a builder project? What do you need to build to conduct these sorts of tests?
Turns out, about 40,800 files.
I didn’t want differences between the two techniques listed above to get lost in the noise of a normal build, so I decided that my Ant framework (the thing to be imported) would have 100 header files — and a corresponding 100 source files — and my Hill iOS app (the thing doing the importing) would have quite a few more — 5,000 source files, each of whom would import one Ant header file.
To avoid having to make either those 200 header/source files, or those 10,000 header/source files, by hand, I wrote some code to do it for me, which resulted in the Builder project. There’s the
AntBuilder class to make the Ant framework files, and the
HillBuilder class to make the Hill app files.
Currently, there are four test projects that the Builder project will make files for:
- 01 Import By Module
- 02 Import Individually
- 03 Import by Framework
- 04 Import by File
The first two test projects address the problem described at the beginning of this post.
The second two test projects go more old school, converting the new module syntax back to straight-up C import syntax. Individual files:
versus the umbrella header:
So instead of just one Ant and Hill project pair, there are four of them.
To test build times, I would reboot each time, open all four projects, wait for them to finish indexing, and then build one of them. After writing down that build time, I would clean that project’s build folder, then go back and start the cycle over again….
I built for Debug to keep it simple and I used the default Simulator target that came up when opening the project, either the iPhone 8 or the iPhone 8 Plus.
This command-line invocation helped:
defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
It makes Xcode show the most recent build time in its user interface, like so:
Here are average times:
01 Import By Module: 124.796s
02 Import Individually: 121.823s
03 Import by Framework: 126.342s
04 Import by File: 122.121s
The differences were between 0% and 4%, which I don’t find to be all that significant, for two reasons.
For one, I only built each project 3 times, and each test series had its own outliers. I suspect if I’d had the patience to build them 10 times, the differences would have smoothed out more. I’ve also since realized Xcode may take up significant amounts of CPU time even after its UI indicates that indexing has finished, lending more randomness to the proceedings.
For two, I actually built the first two projects 3X each separately before building all four projects for this post, and in that case, 01 Import By Module was faster than 02 Import Individually by 2%.
If you’re not convinced, you can certainly run them for yourself.
But for me, I think this proves there isn’t a significant penalty for using full module imports instead of trying to pick out individual module files to import.
The second question was whether this syntax influenced which files would be rebuilt if an Ant framework header was modified. Now, every individual Ant class is used by 50 Hill classes. If only, say, Ant000.h was modified, and only 50 Hill source files referenced it directly, would only those source files be rebuilt?
Turns out no. In all four test cases, two of which involved only references to specific Ant headers, the entirety of the Hill project was rebuilt if even only one Ant header was modified. Rebuild the module (in this case, the Ant framework), and everything that relies on any part of that module is also rebuilt by the current version of Xcode.
Sound right? I consider myself far from an expert in this area, so if anyone has any more information, feel free to leave a comment or ping me on Twitter. Thanks!
One thought on “Building a Better Ant Hill”
this data makes sense to me, in that i had always assumed that referring to a sub-module would require importing the whole module to find the submodule, and that there would be no performance gain.
i still like to make liberal use of submodule references to be able to look a the top of a file and get an idea of it’s compilation complexity and architectural complexity.
in other words, if i look at the set of imports, and i see a lot of imports, i know that the compiler will need to pull in a lot of things to compile this specific file. i also know that if i want to re-factor it, there may be gains in certain areas, and not in others.
it also helps with cleanup. if i have a large file with a large compilation complexity, and it has an @import Ant; , i don’t know whether removing my call to what i know to be in Ant000 will mean that i can fully delete the @import Ant . however, if there is a single reference to only @import Ant.Ant000 , then i have a much better idea that my removal of references to Ant000 will mean that i can remove that import clause altogether.
and … since you have proven that the the compilation cost is the same either way … then using the tools in a way to gauge compilation complexity and architectural complexity still feels like a win to me.
Comments are closed.