TExcelTAD

Model: Hit Rates & What a Cache Saves

ccache's counters give you a hit rate. To turn that into "minutes saved per day" or "is a remote cache worth it?", you need a small model. This lesson builds three: the time of a build at a given hit rate, the effect of network latency on remote hits, and how one header edit destroys the hit rate.

1. Build time vs hit rate

For N compiles, a miss costs the full compile time t_miss plus a little cache overhead (hashing, storing), and a hit costs only t_hit (hash + copy the object). With P parallel jobs:

T(h) ≈ N · (h · t_hit + (1 − h) · t_miss) / P  +  link
python · try it yourself
def build_time(n, hit_rate, t_miss=2.0, t_hit=0.05, overhead=0.02, jobs=8, link=6.0):
    per_compile = hit_rate * t_hit + (1 - hit_rate) * (t_miss + overhead)
    return n * per_compile / jobs + link

n = 1200
no_cache = n * 2.0 / 8 + 6.0
print(f"without ccache: {no_cache:.1f} s")
print(f"{'hit rate':>8} {'build (s)':>10} {'vs no cache':>12}")
for h in [0.0, 0.5, 0.8, 0.9, 0.95, 0.99, 1.0]:
    t = build_time(n, h)
    print(f"{h:>8.0%} {t:>10.1f} {no_cache / t:>11.1f}x")
without ccache: 306.0 s
hit rate  build (s)  vs no cache
      0%      309.0         1.0x
     50%      161.2         1.9x
     80%       72.6         4.2x
     90%       43.0         7.1x
     95%       28.3        10.8x
     99%       16.5        18.6x
    100%       13.5        22.7x

Two things to notice:

  • A cache with a 0% hit rate is slightly slower than no cache (the overhead), which is why a misconfigured cache that never hits is worse than none.
  • The curve is steep at the top: going from 90% to 99% hits more than halves the build. Each percentage point near the top is worth chasing, which is what base_dir, compiler_check = content and sloppiness settings are for.

2. Remote hits aren't free

A remote hit means a network round trip plus downloading the object. If that costs more than compiling, the remote cache slows down builds. The model compares a compile with a remote fetch for different object sizes and links:

python · try it yourself
def fetch_seconds(size_kb, latency_ms, mbit_per_s):
    return latency_ms / 1000 + (size_kb * 8 / 1000) / mbit_per_s

compile_s = {"small TU": 0.3, "typical TU": 2.0, "template-heavy TU": 12.0}
size_kb = {"small TU": 40, "typical TU": 400, "template-heavy TU": 3000}
links = {"same rack (1 ms, 1000 Mbit/s)": (1, 1000), "office VPN (40 ms, 50 Mbit/s)": (40, 50),
         "home DSL (60 ms, 10 Mbit/s)": (60, 10)}
for link, (lat, bw) in links.items():
    print(link)
    for tu, c in compile_s.items():
        f = fetch_seconds(size_kb[tu], lat, bw)
        verdict = "remote hit faster" if f < c else "compiling is faster"
        print(f"  {tu:<18} compile {c:>5.1f}s  fetch {f:>5.2f}s  -> {verdict}")
same rack (1 ms, 1000 Mbit/s)
  small TU           compile   0.3s  fetch  0.00s  -> remote hit faster
  typical TU         compile   2.0s  fetch  0.00s  -> remote hit faster
  template-heavy TU  compile  12.0s  fetch  0.03s  -> remote hit faster
office VPN (40 ms, 50 Mbit/s)
  small TU           compile   0.3s  fetch  0.05s  -> remote hit faster
  typical TU         compile   2.0s  fetch  0.10s  -> remote hit faster
  template-heavy TU  compile  12.0s  fetch  0.52s  -> remote hit faster
home DSL (60 ms, 10 Mbit/s)
  small TU           compile   0.3s  fetch  0.09s  -> remote hit faster
  typical TU         compile   2.0s  fetch  0.38s  -> remote hit faster
  template-heavy TU  compile  12.0s  fetch  2.46s  -> remote hit faster

For C++ objects, remote hits almost always win, even over slow links, because compiles are so expensive. The picture changes for very cheap compiles (C files, small generated sources) on high-latency links, and when many parallel jobs share one slow uplink: then bandwidth, not latency, is the limit. ccache keeps a local cache in front of the remote one, so each object crosses the network at most once per machine.

3. How an edit erodes the hit rate

A CI job building commit n+1 with a cache warmed by commit n misses every TU whose inputs changed. Editing a .cpp file invalidates one TU; editing a header invalidates every TU that includes it. Using include fan-outs like the ones from Header Cost:

python · try it yourself
tus = 800
fan_out = {"src/net/socket.cpp": 1, "net/socket.hpp": 35, "core/log.hpp": 420, "core/common.hpp": 760}
commits = [
    ("fix a typo in socket.cpp", ["src/net/socket.cpp"]),
    ("change socket.hpp", ["net/socket.hpp"]),
    ("add a log level", ["core/log.hpp"]),
    ("touch common.hpp", ["core/common.hpp"]),
]
for msg, files in commits:
    invalid = min(tus, sum(fan_out[f] for f in files))
    hit = (tus - invalid) / tus
    print(f"{msg:<26} misses {invalid:>4}  hit rate {hit:>6.1%}")
fix a typo in socket.cpp   misses    1  hit rate  99.9%
change socket.hpp          misses   35  hit rate  95.6%
add a log level            misses  420  hit rate  47.5%
touch common.hpp           misses  760  hit rate   5.0%

The cache can't save you from your include graph. A commit that touches an "everything" header turns the next CI build into a near-clean build, cache or not. That's why header hygiene (forward declarations) and caching are complementary: one keeps fan-out small, the other reuses everything outside it.

Using the models

Plug in your own numbers:

  • N, t_miss and link from a clean build and .ninja_log;
  • the hit rate from ccache --print-stats at the end of CI jobs ((direct + preprocessed hits) / (hits + misses));
  • fan-outs from ninja -t deps.

Then decide with arithmetic, not anecdotes: a bigger max_size, a remote cache, or splitting a header.

Test yourself

In the model, why is a 0% hit rate slower than no cache?

  1. Links are cached
  2. Every compile pays the cache overhead (hashing, storing) without any benefit
  3. ccache disables parallelism
  4. It isn't; they are equal

Why do remote cache hits usually beat compiling C++ even over a VPN?

  1. Objects are tiny
  2. A typical C++ compile takes seconds, while fetching a few hundred KB takes a fraction of that
  3. VPNs are fast
  4. ccache compresses objects to zero

A commit changes a header included by 760 of 800 TUs. What hit rate can the next build expect?

  1. About 95%
  2. About 50%
  3. About 5%
  4. 100%, because of base_dir