Ubuntu 上优化 Rust 性能的可落地清单
一 编译期优化
[profile.release]
opt-level = 3
lto = "thin" # 平衡性能与编译时长
codegen-units = 1 # 全量优化时提升质量(会增时长)
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
# 或 mold: "-C", "link-arg=-fuse-ld=mold"
[build]
jobs = 16
incremental = true
# 本地/CI
export RUSTC_WRAPPER=sccache
cargo install cargo-cache && cargo cache --autoclean
cargo install cargo-machete && cargo machete
# 仅 Nightly,调试构建更快(运行时略降 5–10%)
rustup toolchain install nightly
rustup component add rustc-codegen-cranelift --toolchain nightly
cargo +nightly build
# dev 配置示例
[profile.dev]
opt-level = 0
debug = 1 # line-tables-only 更快
codegen-units = 16
lto = false
以上做法在 Ubuntu 上对构建时长与运行时性能均有显著收益,尤其是更快的链接器与 sccache 缓存。
二 运行时与代码层优化
cargo install criterion flamegraph
cargo bench
sudo perf record -g target/release/your_app
perf report
cargo flamegraph --bin your_app
use rayon::prelude::*;
let s: i32 = (0..1_000_000).into_par_iter().sum();
// Tokio 异步示例(按需选择运行时)
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1024];
loop {
let n = socket.read(&mut buf).await?;
if n == 0 { return Ok::<_, anyhow::Error>(()); }
socket.write_all(&buf[..n]).await?;
}
});
}
}
# 本机优化(开发/测试)
[profile.dev]
rustflags = ["-C", "target-cpu=native"]
# 通用分发(生产)
[profile.dist]
inherits = "release"
rustflags = ["-C", "target-cpu=x86-64-v3"]
以上手段能稳定提升运行时性能,并通过基准与火焰图确保优化“有的放矢”。
三 系统与运行环境调优
四 场景化配置模板
[profile.dev]
opt-level = 0
debug = 1
incremental = true
codegen-units = 16
lto = false
[build]
jobs = 12
incremental = true
[profile.bench]
inherits = "release"
opt-level = 3
lto = true
debug = true
[profile.release]
opt-level = 3
lto = "thin"
codegen-units = 1
panic = "abort" # 可选:减小体积与展开开销
strip = true # 可选:发布时去除符号
[profile.min-size]
inherits = "release"
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
strip = "symbols"
[profile.dist]
inherits = "release"
rustflags = ["-C", "target-cpu=x86-64-v3"]
以上模板覆盖开发、测试、预发布、生产、体积优先与通用分发等典型场景,可按项目特点微调。