rust-build-times について
rust-build-times は、Rust 開発者向けにコンパイルの遅さを調べ、ビルドやキャッシュ、リンカーの設定を選ぶためのスキルです。プロファイル方法と、コンパイル速度・実行性能のトレードオフを扱います。
主な機能
- 01cargo-timings によるビルド分析
- 02sccache のコンパイルキャッシュ設定
- 03開発ビルドで Cranelift を使用
- 04並列化に向けたワークスペース分割
- 05LTO とリンカー設定の調整
活用シーン
- 01コンパイルに時間のかかる crate の特定
- 02Rust ビルドへのキャッシュ導入
- 03開発用とリリース用の設定比較
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill rust-build-timesインストール後、Claude Code で/rust-build-timesを入力して読み込まれたか確認します。
詳細
Rust ビルド時間
目的
Rust のコンパイル速度を診断して改善する方法をエージェントに案内します。ビルドのプロファイリングに使う cargo-timings、キャッシュに使う sccache、開発ビルドを高速化する Cranelift コード生成バックエンド、ワークスペースのクレート分割、LTO の設定によるトレードオフ、高速リンカー(mold/lld)を取り上げます。
トリガー
- 「Rust プロジェクトのコンパイルに時間がかかりすぎます」
- 「ビルドが遅いクレートをプロファイリングするにはどうすればよいですか?」
- 「Rust 用に sccache を設定するにはどうすればよいですか?」
- 「Cranelift バックエンドとは何ですか?どのように役立ちますか?」
- 「thin LTO と fat LTO のどちらを使うべきですか?」
- 「Rust で mold リンカーを使うにはどうすればよいですか?」
ワークフロー
1. cargo-timings で診断する
# Build with timing report
cargo build --timings
# Opens build/cargo-timings/cargo-timing.html
# Shows: crate compilation timeline, parallelism, bottlenecks
# For release builds
cargo build --release --timings
# Key things to look for in the timing report:
# - Long sequential chains (no parallelism)
# - Individual crates taking > 10s (candidates for optimization)
# - Proc-macro crates blocking everything downstream
# cargo-llvm-lines — count LLVM IR lines per function (monomorphization)
cargo install cargo-llvm-lines
cargo llvm-lines --release | head -20
# Shows functions generating the most LLVM IR (template explosion)
2. sccache — Rust のコンパイルキャッシュ
# Install
cargo install sccache
# or: brew install sccache
# Configure for Rust builds
export RUSTC_WRAPPER=sccache
# Add to .cargo/config.toml (project or global)
# ~/.cargo/config.toml
[build]
rustc-wrapper = "sccache"
# Check cache stats
sccache --show-stats
# S3 backend for CI teams
export SCCACHE_BUCKET=my-rust-cache
export SCCACHE_REGION=us-east-1
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=yyy
sccache --start-server
# GitHub Actions with sccache
# - uses: mozilla-actions/[email protected]
3. Cranelift コード生成バックエンド
Cranelift は高速なコード生成バックエンドです(LLVM と比べて)。生成されるコードの速度は遅くなりますが、コンパイルは大幅に速くなります。開発ビルドに最適です。
# Install nightly (Cranelift requires nightly for now)
rustup toolchain install nightly
rustup component add rustc-codegen-cranelift-preview --toolchain nightly
# Use Cranelift for dev builds only
# .cargo/config.toml
[unstable]
codegen-backend = true
[profile.dev]
codegen-backend = "cranelift"
# Use per-build
CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift \
RUSTFLAGS="-Zunstable-options" \
cargo +nightly build
Cranelift と LLVM のトレードオフ:
- 開発ビルド:Cranelift によりコンパイルが 20~40% 高速化
- 実行時のパフォーマンス:LLVM でコンパイルしたコードのほうが高速です(Cranelift は多くの最適化を省略します)
- リリースビルド:常に LLVM を使用します
4. 並列処理のためのワークスペース分割
大きなクレートを 1 つだけ使うと、コンパイルは逐次的に進みます。小さなクレートに分割して、Cargo の並列処理を有効にします。
# Before: one giant crate
[package]
name = "monolith" # everything in one crate = sequential compile
# After: workspace with parallel crates
[workspace]
members = [
"core", # compiled in parallel
"networking", # no deps on ui → parallel with ui
"ui", # no deps on networking → parallel
"server", # depends on core + networking
"cli", # depends on core + ui
]
# Visualize dependency graph
cargo tree | head -30
cargo tree --graph | dot -Tsvg > deps.svg # visual graph
# Check how many crates compile in parallel
cargo build -j$(nproc) --timings # maximize parallelism
効果的なワークスペース分割のルール:
- 最初に循環依存を解消します
- proc-macro は独自のクレートに分離します(すべての処理をブロックするため)
- 頻繁に変更されるコードは分離します(無効化の範囲を小さくできます)
5. LTO の設定
LTO は実行時のパフォーマンスを向上させますが、リンク時間が長くなります。
# Cargo.toml profile configuration
[profile.release]
lto = "thin" # thin LTO: good performance, much faster than "fat"
codegen-units = 1 # needed for best optimization (but disables parallelism)
[profile.release-fast]
inherits = "release"
lto = "fat" # full LTO: maximum performance, very slow link
[profile.dev]
lto = "off" # never use LTO in dev (compilation speed)
codegen-units = 16 # maximize parallel codegen in dev
LTO の比較:
| 設定 | リンク時間 | 実行時パフォーマンス | 使用する場面 |
|---|---|---|---|
lto = false | 高速 | 基準 | 開発ビルド |
lto = "thin" | 中程度 | +5~15% | ほとんどのリリースビルド |
lto = "fat" | 低速 | +15~30% | 最大限のパフォーマンス |
codegen-units = 1 | 最も低速 | 最高 | リリース用に LTO と併用 |
6. 高速リンカー
大規模な Rust プロジェクトでは、リンカーがボトルネックになることがよくあります。
# mold — fastest general-purpose linker (Linux)
sudo apt-get install mold
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
# Or use cargo-zigbuild (uses zig cc as linker)
cargo install cargo-zigbuild
cargo zigbuild --release
# lld — LLVM's linker (faster than GNU ld, available everywhere)
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
# On macOS: zld or the default lld
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld"]
リンカーの速度比較(大規模プロジェクトでの一般的な目安):
- GNU ld:基準
- lld:約 2 倍高速
- mold:約 5~10 倍高速
- gold:約 1.5 倍高速
7. その他のすぐにできる改善
# Reduce debug info level (faster but less debuggable)
# Cargo.toml
[profile.dev]
debug = 1 # 0=off, 1=line tables, 2=full (default)
# debug=1 saves 20-40% on debug build time
# Split debug info (reduces linker input)
[profile.dev]
split-debuginfo = "unpacked" # macOS: equivalent of gsplit-dwarf
# Disable incremental compilation (sometimes faster for full rebuilds)
CARGO_INCREMENTAL=0 cargo build
# Reduce proc-macro compile time (pin heavy proc-macro deps)
# Heavy proc-macros: serde, tokio, axum — keep versions stable
関連スキル
- Cargo のワークスペースとプロファイルの設定には
skills/rust/cargo-workflowsを使用します - C/C++ における同等のビルド高速化には
skills/build-systems/build-accelerationを使用します - デバッグ情報のサイズと split-dwarf のトレードオフについては
skills/debuggers/dwarf-debug-formatを使用します - LTO の内部動作については
skills/binaries/linkers-ltoを使用します
コメント
Markdown 対応