- Supercharge Your llama.cpp Builds with ccache: A Complete Guide
Supercharge Your llama.cpp Builds with ccache: A Complete Guide
If you’re frequently rebuilding llama.cpp (for testing new commits, CUDA optimizations, or debugging), ccache can dramatically reduce compilation time—sometimes by 5-10x on repeated builds. Here’s how to optimize your workflow.
Why ccache is a Game-Changer for llama.cpp
- Caches object files: Avoids recompiling unchanged code.
- Works with CUDA/NVCC: Also accelerates GPU-enabled builds (
-DGGML_CUDA=ON). - Massive time savings: First build is normal, but subsequent builds skip redundant work.
Benchmark Example
| Build Type | Time (Without ccache) | Time (With ccache) |
|---|---|---|
| Clean Build | ~5-10 minutes | ~5-10 minutes (first run) |
| Rebuild (no changes) | ~5-10 minutes | ~10-30 seconds |
| Rebuild (small change) | ~5-10 min | ~1-2 minutes |
Step 1: Install ccache
Linux (Debian/Ubuntu)
bash
sudo apt update && sudo apt install ccache
Linux (Fedora/RHEL)
bash
sudo dnf install ccache
macOS (Homebrew)
bash
brew install ccache
Windows (MSYS2)
bash
pacman -S ccache
Step 2: Configure ccache for llama.cpp
Modify your build_llama.cpp() function to use ccache:
Option 1: Automatic (via CMAKE_CXX_COMPILER_LAUNCHER)
```bash build_llama.cpp() { cd /mnt/nvme0n1/LLM/git/llama.cpp git pull && git log -1
# Clean build with ccache enabled
cmake -B build -DGGML_CUDA=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
# Build with all CPU cores
cmake --build build --config Release -j$(nproc)
# Install (optional)
cd build && sudo make install
} ```
Option 2: Manual (via CC and CXX overrides)
```bash export CC=“/usr/lib/ccache/gcc” export CXX=“/usr/lib/ccache/g++”
build_llama.cpp() { cd /mnt/nvme0n1/LLM/git/llama.cpp git pull && git log -1 cmake -B build -DGGML_CUDA=ON cmake –build build –config Release -j$(nproc) cd build && sudo make install } ```
Step 3: Verify ccache is Working
Check cache hits/misses:
bash
ccache -s
Example output:
Cacheable calls: 500 / 700 (71.43%)
Hits: 400 / 500 (80.00%)
Misses: 100
Uncacheable calls: 200
Advanced Tips
Increase Cache Size (Default: 5GB)
bash
ccache --set-config=max_size=10G # Set to 10GB
Clear Cache (If Builds Get Weird)
bash
ccache -C
Preload Common Objects (For Even Faster First Build)
If you frequently switch branches, precompile common files:
bash
ccache --recache
Final Optimized Build Script
```bash build_llama.cpp() { cd /mnt/nvme0n1/LLM/git/llama.cpp git pull && git log -1
# Use ccache + all CPU cores
cmake -B build -DGGML_CUDA=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
cmake --build build --config Release -j$(nproc)
# Install (optional)
cd build && sudo make install
# Show ccache stats
ccache -s
} ```
Now your llama.cpp rebuilds will be blazing fast! 🚀