← Recipes

阅读中文版本

Tune a Rust Release Profile for Throughput

Enable LTO and codegen tuning in the release profile to improve runtime throughput at the cost of build time.

~30 minperformancerustbuild

Task

Configure the Cargo release profile with LTO and opt-level tuning for a throughput-sensitive binary.

When to use this

A compute-heavy Rust service is slower in release than expected because default settings miss cross-crate optimisations.

Prerequisites

  • A Rust workspace with Cargo
  • A benchmark or workload
  • A build environment

Steps

1. Set a tuned release profile.

[profile.release]
lto = "fat"
codegen-units = 1
opt-level = 3
panic = "abort"

Expected output: The compiler links across crates and emits fewer, better-optimised codegen units.

2. Benchmark before and after.

cargo bench --profile release

Expected output: Throughput improves while build time increases, quantifying the trade-off.

Troubleshooting

Problem

Build time too high

Solution

Use lto = "thin" for most of the benefit with less build penalty.

Problem

panic=abort breaks unwinding

Solution

Do not use abort if code relies on catch_unwind or unwinding.

Limitations

  • Longer builds slow iteration.
  • panic=abort changes error semantics.
  • Profile choice must match the workload.

What this recipe does

Configure the Cargo release profile with LTO and opt-level tuning for a throughput-sensitive binary.

A compute-heavy Rust service is slower in release than expected because default settings miss cross-crate optimisations.

Prerequisites

  • A Rust workspace with Cargo
  • A benchmark or workload
  • A build environment

Procedure

  1. Set a tuned release profile.
[profile.release]
lto = "fat"
codegen-units = 1
opt-level = 3
panic = "abort"

Expected result: The compiler links across crates and emits fewer, better-optimised codegen units.

  1. Benchmark before and after.
cargo bench --profile release

Expected result: Throughput improves while build time increases, quantifying the trade-off.

How to confirm it worked

Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows the benchmark shows improved throughput and the chosen panic strategy matches the code requirements.

Troubleshooting

Build time too high — Use lto = “thin” for most of the benefit with less build penalty.

panic=abort breaks unwinding — Do not use abort if code relies on catch_unwind or unwinding.

Limitations

  • Longer builds slow iteration.
  • panic=abort changes error semantics.
  • Profile choice must match the workload.