Course contentsShow
← Rust
Lesson 1808 of 1,91015. Performance and Architectural EngineeringFree lesson

Effective Use of LTO and ThinLTO

Optimize build times and performance with incremental cross-module optimization.

In this lesson, you will learn how to balance build performance and binary execution speed by leveraging the incremental nature of ThinLTO.

When we move beyond standard optimization, we often reach for Link-Time Optimization (LTO). Standard LTO performs a "full-program" analysis, which requires merging all object files into a single module before optimization. While this produces the leanest, most highly inlined binaries, it creates a catastrophic bottleneck during the build phase: it is single-threaded and memory-intensive, often leading to exponential build times as your project scales.

This is where ThinLTO becomes a critical architectural tool.

How ThinLTO Works

ThinLTO introduces a two-stage approach to cross-module optimization. Instead of merging everything, it performs a quick "summary" analysis of each module. During the link phase, the linker uses these summaries to identify which functions are candidates for cross-module inlining. It then imports only the necessary code blocks into the relevant modules and optimizes them in parallel.

Configuring for Performance

You can tune these behaviors in your Cargo.toml. While lto = "fat" provides the theoretical maximum performance, it is rarely suitable for modern development workflows. Using lto = "thin" is the industry standard for production-ready Rust:

[profile.release]
lto = "thin"    # Enables incremental cross-module optimization
codegen-units = 16 # Balances parallel work with optimization potential

By keeping codegen-units at a sensible value (e.g., 16 or lower), you allow the compiler to perform enough work to keep individual modules clean, while ThinLTO ensures that across-the-crate-boundary optimizations—like inlining shared generic logic—still occur.

The architectural benefit here is stability. You gain the performance wins of global inlining without sacrificing the incremental build cycle. If you find your link times are still too high, reducing codegen-units further will increase the effectiveness of the ThinLTO summaries, though it may increase the total compilation time for that specific link phase.

Key Takeaway: ThinLTO is the pragmatic architectural choice for high-performance Rust, providing the speed of parallel compilation with the cross-module optimization benefits previously restricted to full-program LTO.