sccache - Cheatsheet della Cache di Compilazione Condivisa
sccache è una cache del compilatore (nello spirito di ccache) di Mozilla che supporta Rust, C/C++ e NVCC. Avvolge il tuo compilatore, esegue l”hash degli input e restituisce un oggetto memorizzato quando ha visto la stessa compilazione prima. La sua caratteristica distintiva è backend di archiviazione condivisi — S3, GCS, Redis, Azure — quindi un intero team e la tua flotta CI condividono una cache. Questo trasforma “CI ricompila tutto da zero ogni esecuzione” in “CI scarica ciò che ha già costruito”.
Installation
| Method | Command |
|---|
| Cargo | cargo install sccache --locked |
| macOS (Homebrew) | brew install sccache |
| Binary | download from GitHub Releases |
| Verify | sccache --version |
Using It with Rust
# One-off
RUSTC_WRAPPER=sccache cargo build
# Persistent (in ~/.cargo/config.toml)
[build]
rustc-wrapper = "sccache"
| Variable | Purpose |
|---|
RUSTC_WRAPPER=sccache | Instrada rustc attraverso sccache |
SCCACHE_DIR | Directory cache locale |
SCCACHE_CACHE_SIZE | Dimensione massima della cache locale (es. 20G) |
Using It with C/C++
# CMake
cmake -DCMAKE_C_COMPILER_LAUNCHER=sccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache ..
# Make
make CC="sccache gcc" CXX="sccache g++"
Checking Effectiveness
| Command | Shows |
|---|
sccache --show-stats | Hit, miss, dimensione cache, errori |
sccache --zero-stats | Reset dei contatori prima di un build di test |
sccache --stop-server | Ferma il server di background |
sccache --start-server | Avvia esplicitamente |
# 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
Il numero chiave è il tasso di hit della cache. Un tasso basso di solito significa che qualcosa negli input sta cambiando (flag, variabili env, percorsi assoluti).
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
Things That Break Caching
| Cause | Fix |
|---|
| Absolute paths in output | Usa --remap-path-prefix |
Changing RUSTFLAGS | Mantieni i flag stabili tra le esecuzioni |
| Incremental compilation | Disabilitalo (CARGO_INCREMENTAL=0) per l”uso della cache |
| Proc macros / build scripts | Non sempre memorizzabile |
| Different compiler versions | Le chiavi di cache includono il compilatore |
sccache vs Alternatives
| Aspect | sccache | ccache | Bazel/Buck |
|---|
| Languages | Rust, C/C++, CUDA | C/C++ | Molti (sistema di build completo) |
| Shared cache | Backend cloud integrati | Principalmente locale | Cache remota/esecuzione |
| Setup | Variabile env di wrapper | Wrapper | Adotta un nuovo sistema di build |
| Best for | Accelerare i build esistenti | Cache locale C/C++ | Grandi monorepo |
Si integra bene con mold — sccache riduce il tempo di compilazione, mold riduce il tempo di link.
Resources