关于 rust-build-times
rust-build-times 是面向 Rust 开发者的编译提速指导技能,涵盖构建分析、缓存及编译和链接配置,帮助定位耗时环节并权衡速度与运行性能。
主要功能
- 01用 cargo-timings 分析构建
- 02配置 sccache 编译缓存
- 03使用 Cranelift 加快开发构建
- 04拆分工作区以增加并行度
- 05调整 LTO 与链接器设置
适用场景
- 01查找耗时较长的 crate
- 02为 Rust 构建启用缓存
- 03比较开发与发布构建配置
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill rust-build-times安装后在 Claude Code 中输入/rust-build-times确认已加载。
详细介绍
Rust 构建时间
用途
指导 Agent 诊断并提升 Rust 编译速度:使用 cargo-timings 分析构建性能、使用 sccache 进行缓存、使用 Cranelift 代码生成后端加快开发构建、拆分工作区 crate、权衡 LTO 配置,以及使用快速链接器(mold/lld)。
触发条件
- “我的 Rust 项目编译时间太长”
- “如何分析哪些 crate 构建缓慢?”
- “如何为 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. 拆分工作区以实现并行构建
单个大型 crate 会按顺序编译。将其拆分为较小的 crate,以启用 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
有效拆分工作区的规则:
- 首先消除循环依赖
- 将过程宏拆分到独立的 crate 中(它们会阻塞所有构建)
- 将经常变更的代码隔离开来(减少失效)
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
相关技能
- 使用
skills/rust/cargo-workflows配置 Cargo 工作区和构建配置 - 使用
skills/build-systems/build-acceleration了解 C/C++ 对应的构建加速方法 - 使用
skills/debuggers/dwarf-debug-format了解调试信息大小和 split-dwarf 的权衡 - 使用
skills/binaries/linkers-lto了解 LTO 内部机制
评论
支持 Markdown