Skip to content

Consider enabling more aggressive optimizations for the Dist profile: Fat LTO and codegen-units = 1 #136

@zamazan4ik

Description

@zamazan4ik

Hi!

I see that the project already uses ThinLTO in the Dist profile in the root Cargo.toml file. However, ThinLTO is usually less efficient from the perspective of performed optimizations than Fat (aka Full) LTO, and cargo-dist/dist defaults are not the most optimal by default - specifically binary size optimizations. Additionally, I suggest enabling codegen-units = 1 (CU1) too. Enabling more advanced optimizations allows us to reduce the binary size further without hurting any other part of the binary. In the embedded world we aim to care a bit more about binary size :)

Basically, it can be enabled with the following change in the Cargo.toml file:

[profile.dist]
inherits = "release"
codegen-units = 1
lto = true

I have made quick local tests (AMD Ryzen 9 5900x, Fedora 43, Rust 1.94, the latest version of this project at the moment, cargo build --profile dist command, no stripping) - the results are below.

  • ThinLTO (current Dist profile): 1.1 Mib, clean build time: 3.4s
  • FatLTO + CU1: 830 Kib, clean build time: 4.7s

The win is pretty small but if it's for free so why not? :) Since the Dist profile is used only for release binaries, this build time increase shouldn't be a problem for the project (especially having such a low build time for now anyway). In exchange to build time, we will deliver a more optimized binary to users by default.

We can also apply the optimized Cargo settings to the Release profile, so cargo install will also deliver by default smaller binaries. But it's another topic for discussion - it doesn't block the Dist profile optimization proposal above. However, it would be nice to do it in one PR (ofc if you agree with the proposal).

I am ready to create a PR with the changes, if maintainers are okay with the proposed changes.

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions