Optimizing Rust Performance
Profile a release-like artifact first. Identify whether CPU, allocation, retained memory, contention, code size, or startup is the binding cost before changing idiomatic code.
- Read Rust performance audit.
- Scan hot paths for clones, owned-string conversion, growth reallocations, formatting, dynamic dispatch, lock contention, and allocation-hidden iterator adapters.
- Use compiler changes only through optimizing-compiler-performance.
- Keep
unsafe, SIMD, custom allocators, and target-specific features behind explicit invariants, supported-target dispatch, and a measured win. For SIMD, reach forstd::archintrinsics behindis_x86_feature_detected!/#[cfg(target_arch)],std::simdon nightly, thewidecrate for stable ergonomic portable SIMD, or the SIMD-acceleratedmemchrfamily for byte scans. Do NOT-C target-cpu=nativeor a baseline-C target-feature=+avx2in a build distributed to CPUs you do not control — it bakes in the build machine's ISA and SIGILLs on older CPUs; runtimeis_x86_feature_detected!dispatch is the portable path. Pin only to a floor a controlled target guarantees — a homogeneous fleet, a per-ISA build matrix, or a local build where build host == run host — and record why. Enforced byscripts/fleet/check/build-microarch-is-portable.mts. - Report CPU, allocations, peak RSS, binary/startup impact, and correctness results.