Open Kobzol opened 2 years ago
It would be interesting to see how this interacts with -Z build-std=std,panic_abort
as then the linker might also be able to fold identical code in std
as well? 🤔
I tried it, but it didn't change the binary size. I suppose that's expected, since the program links statically to libstd
, the code of libstd
is already inside the binary when ICF runs, so it's applied to it even without build-std
. It's done on the linker/binary level, so it won't do much for libstd
unless we link to it as a dynamic library I guess :)
Since Rust can't do polymorphization properly yet, using generics generates a lot of duplicated functions because of monomorphization. These functions take space in the binary, even though they have completely the same instructions.
Some linkers (
gold
,lld
) can deduplicate these identical functions using Identical Code Folding, and thus reduce binary size (and potentially also improve usage of the i-cache).You can specify this linker option using a linker flag, for example like this:
I measured the binary size change for the following program:
Here are binary sizes are after running
strip
on them: