Can the Rust compiler merge identical function implementations?

In the case, I have multiples dependencies (A, B, C), each one with the dependency of a crate "lib" with distinct versions (v1, v2, v3).

My crate -> dependency A -> lib-v1
         -> dependency B -> lib-v2
         -> dependency C -> lib-v3

From what I understand, the compiler will compile each dependency using its own lib version. So the final binary will have code of all 3 libraries. In the case of all used methods are identical from in all versions. Is the compiler smarty enough to merge the identical code? Would the produced binary be the same size as if all dependencies use the same library version?

I have no specific use case, I am just curious.

1 Like

I don't know, but if it can, it would probably require turning on link-time optimizations, which are off by default.

[profile.release]
lto = true
1 Like

Rust enables an LLVM pass called MergeFunctions, and that tries to merge equivalent functions.

If you use generic functions from lib-v1/2/3, those uses are instantiated inside "My crate" and could be merged, at optimization time, even without LTO. How thin-lto interacts with any of this, I don't know.

It's worth noting that there's another big use case for this optimization: merging different instantiations of a generic function.

If lib has a generic function fn generic<T>, and then both dependency A and dependency B use lib::generic<u32>, it'll be codegen'd twice - once for each dependency. So something like this is possible even without multiple versions of a crate!

My crate -> dependency A -> lib::generic<u32>
         -> dependency B -> lib::generic<u32>
         -> dependency C -> lib::generic<u32>

Regardless of the cause of the duplicated functions, if you suspect they appear in any particular dependency graph, it should be possible to test. Tools like cargo-bloat or twiggy for wasm will display all generated functions, and if the function's big enough it will be pretty easy to spot duplicates. I'm ~75% sure I've seen generic instantiated functions that appeared duplicated before, but I could be misremembering things.