johnthagen / min-sized-rust

🦀 How to minimize Rust binary size 📦
MIT License
8.19k stars 210 forks source link

Identical code folding #38

Open Kobzol opened 2 years ago

Kobzol commented 2 years ago

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:

$ RUSTFLAGS="-Clink-args=-fuse-ld=lld -Clink-args=-Wl,--icf=all" cargo build

I measured the binary size change for the following program:

fn foo() {
    let mut a: Vec<u8> = vec![1, 2, 3];
    a.pop();

    let mut b: Vec<u32> = vec![1, 2, 3];
    b.pop();

    let mut c: Vec<i32> = vec![1, 2, 3];
    c.pop();
}

fn main() {
    foo();
}

Here are binary sizes are after running strip on them:

Linker Mode Binary size (B) ICF (Identical Code Folding)
gold debug 342696 No
gold debug 330408 Yes
gold release 322216 No
gold release 318120 Yes
lld debug 330968 No
lld debug 321840 Yes
lld release 310616 No
lld release 306848 Yes
johnthagen commented 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? 🤔

Kobzol commented 2 years ago

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 :)