How smart is the compiler about avoiding monomorphization?

⚓ Rust    📅 2026-07-04    👤 surdeus    👁️ 2      

surdeus

This has been asked before here, but that's four years old, so I'll check again to see if anything has changed.

Say I have a function that looks like this:

fn foo(id: impl Into<SimpleId>) {
    let id = id.into(); 
    // everything else uses id: SimpleId

It seems like it would be better to save space on instructions and do something like:

fn foo(id: impl Into<SimpleId>) {
    foo_inner(id.into());
}
fn foo_inner(id: SimpleId) { ... }

Now foo could also be inlined.

Is the Rust compiler smart enough to do that for me, in release/optimization mode? Or does it pay to do this manually?

If SimpleId were SomeBigThing, then the size of the moved object could come into play. But in my case both the SimpleId and the type that implements Into<SimpleId> are 8 bytes.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed