Salta ai contenuti

sccache - Cheatsheet della Cache di Compilazione Condivisa

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

MethodCommand
Cargocargo install sccache --locked
macOS (Homebrew)brew install sccache
Binarydownload from GitHub Releases
Verifysccache --version

Using It with Rust

# One-off
RUSTC_WRAPPER=sccache cargo build

# Persistent (in ~/.cargo/config.toml)
[build]
rustc-wrapper = "sccache"
VariablePurpose
RUSTC_WRAPPER=sccacheInstrada rustc attraverso sccache
SCCACHE_DIRDirectory cache locale
SCCACHE_CACHE_SIZEDimensione 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

CommandShows
sccache --show-statsHit, miss, dimensione cache, errori
sccache --zero-statsReset dei contatori prima di un build di test
sccache --stop-serverFerma il server di background
sccache --start-serverAvvia 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
BackendEnv prefix
Local diskSCCACHE_DIR
S3 / S3-compatibleSCCACHE_BUCKET, SCCACHE_ENDPOINT
RedisSCCACHE_REDIS
GCSSCCACHE_GCS_*
AzureSCCACHE_AZURE_*
GitHub ActionsSCCACHE_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

CauseFix
Absolute paths in outputUsa --remap-path-prefix
Changing RUSTFLAGSMantieni i flag stabili tra le esecuzioni
Incremental compilationDisabilitalo (CARGO_INCREMENTAL=0) per l”uso della cache
Proc macros / build scriptsNon sempre memorizzabile
Different compiler versionsLe chiavi di cache includono il compilatore

sccache vs Alternatives

AspectsccacheccacheBazel/Buck
LanguagesRust, C/C++, CUDAC/C++Molti (sistema di build completo)
Shared cacheBackend cloud integratiPrincipalmente localeCache remota/esecuzione
SetupVariabile env di wrapperWrapperAdotta un nuovo sistema di build
Best forAccelerare i build esistentiCache locale C/C++Grandi monorepo

Si integra bene con mold — sccache riduce il tempo di compilazione, mold riduce il tempo di link.

Resources