Skip to content

Enable Link-Time Optimization (LTO) and codegen-units = 1 for Release builds #60

@zamazan4ik

Description

@zamazan4ik

Hi!

I noticed that in the Cargo.toml file Link-Time Optimization (LTO) for the project is not enabled. I suggest switching it on since it will reduce the binary size (always a good thing to have) and will likely improve CPU performance a bit due to more aggressive compiler optimizations. Additionally, codegen-units = 1 (CU1) option can help too in a similar to LTO way, so I recommend to enable it as well.

I recommend enabling LTO only for Release builds so developers experience won't be affected by the increased build time. Actually, I can propose to use flags directly from this ripgrep profile (like stripping and other things).

Basically, it can be enabled with the following lines to the root Cargo.toml file:

[profile.release]
codegen-units = 1
lto = true # FatLTO - the most aggressive LTO version
<possible other options like strip = true>

I have made quick tests (AMD Ryzen 9 5900x, Fedora 43, Rust 1.95, cargo build -r --bins build command, without stripping, ) - here are the results:

Profile / Binary name protoc-gen-buffa protoc-gen-buffa-packaging
Current Release 3.9 Mib 706 Kib
Release + FatLTO + CU1 3.1 Mib 540 Kib

Even if the win is not that big - the proposed change is just a three-liner to the Cargo.toml file - so why not? :) LTO was even mentioned in a PR. I haven't done performance benches yet but expect FatLTO + CU1 the most performant configuration too.

Clean build time:

  • Current Release profile: 10s
  • Release + FatLTO + CU1: 23s

Build time increase shouldn't be an issue since we enable it only for the Release profile - in this case, we would not affect the development lifecycle. This change optimizes both cargo install-ed and manually downloaded prebuilt binaries from the GitHub Releases page.

The same thing can/should be applied to the https://github.com/anthropics/connect-rust repo. Enabling FatLTO + CU1 for protoc-gen-connect-rust improves the binary size from 3.1 Mib to 1.9 Mib (clean build time increase from 10s to 19s). If it's needed, I can create a dedicated issue in the corresponding repo too.

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions