在 Debian 上“优化 Rust”通常分几个层面:系统/编译环境优化、Cargo 编译优化、运行时/发布优化、以及系统级调优。下面按常见需求给你一套实用方案。
Debian 自带的 rustc 通常较旧:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
使用 nightly(如需更多优化特性):
rustup toolchain install nightly
rustup default nightly
sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev clang llvm
sudo apt install mold
在 ~/.cargo/config.toml:
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
cargo build --release
Cargo.toml 示例:
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
strip = true
说明:
opt-level = 3:最大优化lto = true:跨 crate 优化codegen-units = 1:更小更快strip = true:去掉调试符号panic = "abort":减少运行时开销(不能 catch panic)[profile.release]
rustflags = ["-C", "target-cpu=native"]
或在 ~/.cargo/config.toml:
[build]
rustflags = ["-C", "target-cpu=native"]
⚠️ 这样编译的二进制不能在其他机器运行
cargo install sccache
[build]
rustc-wrapper = "sccache"
export CARGO_BUILD_JOBS=$(nproc)
jemalloc(大内存服务):[dependencies]
tikv-jemallocator = "0.5"
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
clonerayon 做并行perf / flamegraph 分析热点Cargo.toml
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
strip = true
~/.cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = [
"-C", "link-arg=-fuse-ld=mold",
"-C", "target-cpu=native"
]
如果你能告诉我:
我可以给你更精确的 Debian + Rust 优化方案。