TExcelTAD
Home › Build Performance & Artifacts: ccache, sccache, Distributed Builds, Artifact Repositories › How ccache Works: Direct, Preprocessor & Depend Mode

How ccache Works: Direct, Preprocessor & Depend Mode

CMake's ccache toolkit lesson showed the basics: set CMAKE_CXX_COMPILER_LAUNCHER=ccache and clean rebuilds turn into cache hits. To make ccache hit reliably in a team and in CI, you need to know how it decides that two compiles are "the same". This lesson looks inside.

The idea: a hash of everything that matters

For each compile ccache computes a hash of the inputs that can affect the object file: the compiler (identity and version), the relevant command-line options, the source and the headers it reads, and a few environment details. That hash is the key; the stored object file (plus the compiler's stderr and dependency file) is the value. The question is how to hash "the source and the headers" cheaply, and ccache has three ways.

Mode How it finds the headers Cost of a lookup Default
Direct mode a manifest from an earlier compile lists the headers and their hashes; ccache hashes the source and those headers directly cheapest: no preprocessor run on
Preprocessor mode runs the preprocessor (-E) and hashes its output one preprocessor run used when direct mode misses
Depend mode uses the dependency file the compiler writes (-MD) instead of the preprocessor cheap, but no preprocessor fallback off (depend_mode = true)

On a lookup, ccache tries direct mode first. If that misses (no manifest yet, or a header changed), it falls back to preprocessor mode. After a real compile, it stores the result under both keys and updates the manifest.

Watching the counters

The session below compiles one file several times and prints the three counters that matter after each step. Zeroing the statistics first (ccache -z) makes every count exact:

bash · try it yourself
mkdir -p modes && cd modes
cat > shape.cpp <<'SRC'
#include <string>
// Area of a square
int area(int side) { return side * side + int(std::string("ok").size()) - 2; }
SRC
st() { ccache --print-stats | awk -F'\t' '$1 ~ /^(direct_cache_hit|preprocessed_cache_hit|cache_miss)$/ { printf "%s=%s ", $1, $2 } END { print "" }'; }
c() { ccache g++ -O2 -c shape.cpp -o shape.o; }
ccache -z > /dev/null
c; echo "1. first compile:          $(st)"
c; echo "2. same again:             $(st)"
sed -i 's|// Area of a square|// Area of a square (side * side)|' shape.cpp
c; echo "3. comment edited:         $(st)"
c; echo "4. same again:             $(st)"
sed -i 's|side \* side +|side * side * 1 +|' shape.cpp
c; echo "5. code edited:            $(st)"
1. first compile:          cache_miss=1 direct_cache_hit=0 preprocessed_cache_hit=0 
2. same again:             cache_miss=1 direct_cache_hit=1 preprocessed_cache_hit=0 
3. comment edited:         cache_miss=1 direct_cache_hit=1 preprocessed_cache_hit=1 
4. same again:             cache_miss=1 direct_cache_hit=2 preprocessed_cache_hit=1 
5. code edited:            cache_miss=2 direct_cache_hit=2 preprocessed_cache_hit=1 

Step by step (the counters accumulate):

  1. Nothing cached: a miss, the real compiler runs, and the result and a manifest are stored.
  2. Same source, same headers: a direct hit. No preprocessor, no compiler.
  3. Only a comment changed. The source hash differs, so direct mode misses; but the preprocessor strips comments, so the preprocessed output is identical: a preprocessed hit. The object is reused, and the manifest learns the new source hash.
  4. Now direct mode knows this version too: a direct hit.
  5. Real code change: a miss, as it should be.

Depend mode

Running the preprocessor is the expensive part of a direct-mode miss. Depend mode skips it: ccache relies on the dependency file that the compiler writes anyway with -MD (which CMake's Ninja and Makefile generators always pass). The cost is that there's no preprocessor fallback, so the comment-only edit above becomes a miss:

bash · try it yourself
mkdir -p dep && cd dep
printf '#include <string>\n// v1\nint f() { return int(std::string("x").size()); }\n' > f.cpp
st() { ccache --print-stats | awk -F'\t' '$1 ~ /^(direct_cache_hit|preprocessed_cache_hit|cache_miss)$/ { printf "%s=%s ", $1, $2 } END { print "" }'; }
export CCACHE_DEPEND=1
c() { ccache g++ -c f.cpp -o f.o -MD -MF f.d; }
ccache -z > /dev/null
c; echo "first compile:   $(st)"
c; echo "same again:      $(st)"
sed -i 's|// v1|// v2|' f.cpp
c; echo "comment edited:  $(st)"
echo "--- without -MD, depend mode can't be used; ccache falls back to the normal modes:"
ccache -z > /dev/null
ccache g++ -c f.cpp -o f2.o; ccache g++ -c f.cpp -o f2.o; echo "                 $(st)"
first compile:   cache_miss=1 direct_cache_hit=0 preprocessed_cache_hit=0 
same again:      cache_miss=1 direct_cache_hit=1 preprocessed_cache_hit=0 
comment edited:  cache_miss=2 direct_cache_hit=1 preprocessed_cache_hit=0 
--- without -MD, depend mode can't be used; ccache falls back to the normal modes:
                 cache_miss=1 direct_cache_hit=1 preprocessed_cache_hit=0 

Depend mode is a good choice when the preprocessor is slow (big include trees, network file systems) and misses are usually real code changes anyway. It's also what makes ccache efficient with MSVC (/showIncludes).

What goes into the hash (and what doesn't)

In the hash Not in the hash
compiler identity (see compiler_check) the output file name (-o)
options that affect the result (-O2, -D…, -std=…, -I… paths) -MD/-MF paths and similar bookkeeping
the source file and every header it reads timestamps of files (unless __DATE__/__TIME__ are used)
the current directory, when -g is used (hash_dir) comments, in preprocessor mode
absolute paths, unless base_dir rewrites them the environment, apart from a few known variables

The last row of the first column is why two checkouts of the same code at different paths usually don't share hits: the next lesson, base_dir & hash_dir, fixes that.

Reading the statistics

  • ccache -s (--show-stats): human summary; add -v for details (cache directory, files, reads and writes).
  • ccache --print-stats: every counter as name<TAB>value, ideal for scripts and CI logs (--format=json too).
  • ccache -z: zero the counters, e.g. at the start of a CI job, so the end-of-job numbers describe that job only.
  • ccache -sv also shows counters for uncacheable calls, such as links (called_for_link) or unsupported options. The next lessons cover those.

Test yourself

Only a comment in the .cpp changed. In default mode, which counter goes up?

  1. direct_cache_hit
  2. preprocessed_cache_hit
  3. cache_miss
  4. None

What does depend mode use instead of running the preprocessor?

  1. The .ninja_log
  2. The dependency file the compiler writes with -MD
  3. A PCH
  4. Git hashes

Why zero the statistics with ccache -z at the start of a CI job?

  1. It empties the cache
  2. So the counters at the end describe this job only
  3. It's required for hits
  4. It resets the configuration