Using sccache to speed up compiler builds

Compiling the Swift compiler is a somewhat time consuming, especially for cold builds. This is particularly painful if

  • You're bisecting an issue over a large period of time.
  • You're switching between Xcode versions to debug issues.
  • You have multiple distinct work directories, say for working on multiple things at once.

sccache provides a mostly automatic caching experience for build artifacts. I was putting off setting it up because I thought it might be complicated. I finally set it up on my Mac yesterday and it was surprisingly painless! I figured I'd write down the steps here, so other people can easily refer to it.

$ brew install sccache
$ sccache --start-server
$ utils/build-script MY_ARGS --cmake-c-launcher $(which sccache) --cmake-cxx-launcher $(which sccache)

Given the size of artifacts generated, you might also want to bump the cache size from the default 10GB to something larger, say by putting export SCCACHE_CACHE_SIZE="50G" in your dotfile(s).

You can run some compiles to see if it is actually doing something by running sccache --show-stats. In my very limited experience so far, it isn't super amazing (I got ~40 cache hits out of ~700 compilation requests once), but that's probably because I was changing some Swift compiler headers which were affecting many things. Also, the stat tracking seems to reset between shells or something :man_shrugging:. Maybe someone more experienced with sccache knows what's up with that?

My understanding is that it is very effective for things like LLVM, which doesn't change so frequently from the Swift compiler's perspective. @codafi mentioned compiling 6 compilers at once (!) thanks to sccache (how he managed to free up sufficient storage space for that remains a mystery to me).

Overall: I got some faster builds without a lot of work, particularly without having to read/write any CMake, so 10/10 would recommend.

11 Likes

@typesanitizer my suggestion would be to add this documentation somewhere beyond just the forum. Maybe the readme? Not sure. @mishal_shah any thoughts?

Agreed. I wasn't sure where to put it, so I made a forum post for it. :slight_smile:

We already have similar topic: Build Cache Server - #2 by ddunbar

However, these's not so much updates.

These are two different things. The Build Cache Server post is about having a distributed cache working with llbuild, which is used by SwiftPM. The Swift compiler is built using CMake + Ninja or CMake + Xcodebuild, I don't think either of them uses llbuild under the hood. Moreover, sccache already works today with CMake.

llbuild is the foundation of the Xcode "new" (as opposed to "legacy") build system. So, if you are using the Xcode system to build Swift, you would be using "llbuild" most likely, since I'm sure that the xcodeprojects are probably using the "new" build system. I'm assuming once the llbuild cache system is stable enough, the Xcode team will most likely incorporate it into the server infrastructure of Xcode.

1 Like

Small update: This information is now documented in a new file docs/DevelopmentTips.md. Feel free to add more productivity tips for people hacking on the compiler there. :slight_smile:

1 Like