x

Engineering4 min read

How we made cold builds 20× faster

PR
Priya Raman

Staff Engineer

Every engineering team eventually notices the same thing: the slowest part of shipping isn't writing code, it's waiting for it. Last year our own median build sat at a little over three minutes. Today it's nine seconds. This post walks through how we got there, including the parts that didn't work.

By a cold build we mean one that starts on a fresh machine with nothing on disk. That's the normal case in CI, and it's where most caching strategies fall apart. A laptop remembers yesterday's work. A new runner doesn't.

Where the time actually went

Before optimising anything, we instrumented everything. We traced 40,000 builds across 900 projects and broke each one into phases: clone, restore, install, compile, test and upload. Three findings shaped the rest of the project.

  • Install and compile accounted for 81% of wall-clock time.
  • Fewer than 4% of files changed between consecutive builds on the same branch.
  • Cache restores were sequential, so large caches were slower than no cache at all.

The second number stung. We were redoing almost all of the work on every push, even though almost none of the inputs had changed. Faster machines would have made that waste cheaper, but it would still have been waste.

Why one cache per branch wasn't enough

Our old cache was a single compressed archive per branch. At the end of a build we packed up the dependency folder and the compiler output, then unpacked it all at the start of the next one. It was easy to understand, and it had two serious problems.

First, it was all or nothing. Changing one dependency invalidated the entire archive. Second, nothing was shared. If a teammate had already built the same package on another branch, you still built it again. A new branch, the moment when fast feedback matters most, always started with nothing.

A content-addressed cache

We replaced the archive with a content-addressed store. Every build output is keyed by a hash of its inputs: source files, resolved dependencies, compiler version, build command and the environment variables the task declares. Identical work done on any branch, by any teammate, or in CI is reused instead of repeated.

# shipr.toml
[build.cache]
mode = "content-addressed"
share = "team"   # reuse outputs across branches and CI
restore = "speculative"

The hard part is deciding what counts as an input. Leave one out and you serve a stale result. Include too many and nothing ever matches. Timestamps and absolute paths were the worst offenders: they differ on every runner but rarely affect the output. We strip both before hashing.

We also made undeclared inputs visible. If a task reads an environment variable that isn't in its configuration, the build inspector flags it. A cache that's fast but occasionally wrong is worse than no cache, because people stop trusting every result it gives them.

Speculative restores

The new store gave us far more hits, but we still waited for dependency resolution to finish before downloading anything. The network sat idle while the CPU worked, and then the CPU sat idle while the network caught up.

Now Shipr predicts which artifacts a build will need from the lockfile diff and starts streaming them in parallel. When the prediction is right, which is 96% of the time, the restore is already done by the time the compiler asks for it. When it's wrong, we discard the download. A wrong guess costs some bandwidth, but it can't change the result, because every artifact is still checked against its hash before use.

The fastest build step is the one that already happened on somebody else's machine.

What didn't work

Some reasonable ideas didn't survive contact with real projects:

  • Bigger runners. Doubling the CPU count helped compilation, but did little for installs, which mostly wait on the network and disk.
  • Maximum compression. Smaller archives downloaded faster, then lost all of that time being unpacked.
  • Caching every task. For very short tasks, looking up and downloading a result took longer than running the task.

The last one led to a simple rule: if restoring a task takes longer than running it, Shipr runs it. That decision uses the task's own history, so nobody has to tune a threshold by hand.

Results

Median build time dropped from 3m 12s to 9s, and p95 from 7m to 48s. Cache hit rate across all teams now sits at 94%. The p95 improvement matters most to us: a slow build at the wrong moment is what makes someone switch tasks and lose their train of thought.

There's still work to do. A change to a package that everything depends on will always rebuild a lot of code, and the first build of a brand-new project has nothing to reuse.

Try it on your project

Remote caching is available on Pro and Team. Add the configuration above, push twice, and compare the two runs in the build inspector. If the second run still rebuilds something you didn't touch, start with the inputs listed for that task. An unexpected value there is usually the cause.