sccache - Shared Compilation Cache Cheatsheet
sccache ist ein Compiler-Cache (im Geist von ccache) von Mozilla, der Rust, C/C++ und NVCC unterstützt. Es wrappet deinen Compiler, hash die Inputs und gibt ein gecachtes Object zurück, wenn es dieselbe Kompilation vorher gesehen hat. Sein unterscheidendes Feature sind Shared Storage Backends — S3, GCS, Redis, Azure — sodass ein ganzes Team und deine CI Fleet einen Cache teilen. Das verwandelt “CI neukompiliert alles von Grund auf bei jedem Lauf” in “CI lädt herunter, was es bereits gebaut hat.”
Installation
| Methode | Befehl |
|---|
| Cargo | cargo install sccache --locked |
| macOS (Homebrew) | brew install sccache |
| Binary | download from GitHub Releases |
| Verifikation | sccache --version |
Benutzung mit Rust
# One-Off
RUSTC_WRAPPER=sccache cargo build
# Persistent (in ~/.cargo/config.toml)
[build]
rustc-wrapper = "sccache"
| Variable | Zweck |
|---|
RUSTC_WRAPPER=sccache | Route rustc durch sccache |
SCCACHE_DIR | Lokales Cache-Verzeichnis |
SCCACHE_CACHE_SIZE | Max lokale Cache-Größe (z.B. 20G) |
Benutzung mit C/C++
# CMake
cmake -DCMAKE_C_COMPILER_LAUNCHER=sccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache ..
# Make
make CC="sccache gcc" CXX="sccache g++"
Wirksamkeit prüfen
| Befehl | Zeigt |
|---|
sccache --show-stats | Hits, Misses, Cache-Größe, Errors |
sccache --zero-stats | Reset Counters vor Test-Build |
sccache --stop-server | Stop the Background Server |
sccache --start-server | Start es explizit |
# Measure a Cold vs Warm Build
sccache --zero-stats
cargo build --release
sccache --show-stats # expect misses
cargo clean && cargo build --release
sccache --show-stats # expect hits
Die Schlüsselzahl ist Cache Hit Rate. Eine niedrige Rate bedeutet normalerweise etwas in den Inputs ändert sich (Flags, Env-Vars, Absolute Paths).
Shared Cloud Backends
# S3
export SCCACHE_BUCKET=my-build-cache
export SCCACHE_REGION=us-east-1
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=...
# Redis
export SCCACHE_REDIS=redis://cache.internal:6379
# GCS
export SCCACHE_GCS_BUCKET=my-build-cache
export SCCACHE_GCS_KEY_PATH=/path/to/key.json
| Backend | Env Prefix |
|---|
| Local Disk | SCCACHE_DIR |
| S3 / S3-compatible | SCCACHE_BUCKET, SCCACHE_ENDPOINT |
| Redis | SCCACHE_REDIS |
| GCS | SCCACHE_GCS_* |
| Azure | SCCACHE_AZURE_* |
| GitHub Actions | SCCACHE_GHA_ENABLED=true |
CI Pattern
# GitHub Actions
env:
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
steps:
- uses: mozilla-actions/sccache-action@v0.0.5
- run: cargo build --release
- run: sccache --show-stats
Dinge, die Caching brechen
| Ursache | Fix |
|---|
| Absolute Paths in Output | Nutze --remap-path-prefix |
Ändernde RUSTFLAGS | Keep Flags stabil über Läufe |
| Incremental Compilation | Disable es (CARGO_INCREMENTAL=0) für Cache-Nutzung |
| Proc Macros / Build Scripts | Nicht immer cacheable |
| Unterschiedliche Compiler-Versionen | Cache Keys inkludieren den Compiler |
sccache vs Alternativen
| Aspekt | sccache | ccache | Bazel/Buck |
|---|
| Languages | Rust, C/C++, CUDA | C/C++ | Viele (vollständiges Build System) |
| Shared Cache | Cloud Backends Built-in | Meistens Lokal | Remote Cache/Execution |
| Setup | Wrapper Env Var | Wrapper | Adopt ein neues Build System |
| Am besten für | Speedup existierender Builds | C/C++ lokales Caching | Large Monorepos |
Paart gut mit mold — sccache kürzt Compile-Zeit, mold kürzt Link-Zeit.
Ressourcen