Linux下Rust编译提速实用指南
一 高性价比动作优先做
export RUSTC_WRAPPER=sccache;CI 可配合 GitHub Action mozilla-actions/sccache-action 与 Swatinem/rust-cache。本地可定期执行 cargo cache --autoclean 清理无用缓存。.cargo/config.toml 配置:[target.x86_64-unknown-linux-gnu]linker = "clang"rustflags = ["-C", "link-arg=-fuse-ld=lld"](如已安装 Mold,可改为 -fuse-ld=mold)。cargo build -j $(nproc --ignore=2);在 .cargo/config.toml 设置 [build] jobs = 16(数值≈CPU 核数)。增量构建在 stable 已默认开启,建议显式写为 incremental = true 防止被覆盖。default-features = false 并仅启用需要的功能,能显著减少编译单元与链接压力。[profile.dev] opt-level = 0、debug = 1(仅行表,比 full 快约30%)、codegen-units = 16、lto = false,避免把 opt-level=3/lto=true 误用到开发配置。cargo +nightly build。二 配置示例可直接复制
# ~/.bashrc 或当前 shell
export RUSTC_WRAPPER=sccache
# 构建时保留 2 核给编辑器/终端
alias cb='cargo build -j $(nproc --ignore=2)'
alias ct='cargo test -j $(nproc --ignore=2)'
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
# 如已安装 mold:将上一行替换为 "-C", "link-arg=-fuse-ld=mold"
[profile.dev]
opt-level = 0
debug = 1 # 仅行表,更快
codegen-units = 16
lto = false
incremental = true
# .cargo/config.toml
[unstable]
codegen-backend = true
[profile.dev]
codegen-backend = "cranelift"
三 CI 场景一键复制
name: Fast Rust CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo + sccache
uses: mozilla-actions/sccache-action@v0.0.6
with:
version: "v0.8.2"
- name: Rust cache
uses: Swatinem/rust-cache@v2
- name: Build
run: cargo build --all-features
env:
RUSTC_WRAPPER: sccache
CARGO_INCREMENTAL: 1
SCCACHE_GHA_ENABLED: true
- name: Test
run: cargo nextest run --all-features # 通常比默认 test 快 ~60%
四 系统层面与 I/O 优化
target 放到内存盘(Tmpfs):I/O 密集项目全量构建常见可降 20–30%。示例:mkdir -p /mnt/fast
sudo mount -t tmpfs -o size=8G tmpfs /mnt/fast
CARGO_TARGET_DIR=/mnt/fast cargo build
sudo sysctl -w vm.max_map_count=262144。ulimit -n 查看/调高。五 定位瓶颈与常见误区
cargo install flamegraph 后执行 cargo flamegraph,定位编译期/运行期热点函数。perf:perf record --call-graph dwarf ./target/release/your_bin && perf report。opt-level=3/lto=true 放到 dev,会显著拖慢链接与迭代。build.rs(尤其含 C/C++ 子流程),能预编译就预编译为静态库。