CentOS 上 Rust 优化配置清单
一 工具链与环境准备
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shrustup default stablerustup component add rust-src lld[build] rustflags = ["-C", "target-cpu=native"]rustc --version、cargo --version 正常输出即可。二 编译期优化
[profile.release] opt-level = 3(最高速度优化)lto = true(链接时优化,跨模块内联与去冗余)codegen-units = 1(减少代码生成单元,提升优化质量,代价是编译更慢)panic = "abort"(生产环境减少展开与回溯开销)cargo build --releaseRUSTFLAGS="-C opt-level=3" cargo build --releaseopt-level 设为 “z”,并开启 strip = true 剥离符号。三 运行时与内存管理
jemallocator = "0.3";代码:#[global_allocator] static GLOBAL: Jemalloc = Jemalloc;mimalloc = "0.1";代码:#[global_allocator] static GLOBAL: MiMalloc = MiMalloc;Vec::with_capacity、String::with_capacity;减少拷贝与扩容抖动。四 并发与异步模型
numbers.par_iter().sum()五 系统与持续分析
ulimit -n 65535(或更高,视业务与系统限制而定)。sysctl -w net.core.somaxconn=2048、sysctl -w net.ipv4.tcp_max_syn_backlog=2048,缓解高并发连接建立瓶颈。vm.max_map_count(如 262144)。sudo perf record -F 99 -ag -- ./target/release/app;perf report 查看热点。cargo install flamegraph;flamegraph.pl perf.data > flamegraph.svg;也可结合 RUSTFLAGS="-C target-cpu=native" cargo flamegraph。