johnthagen / min-sized-rust

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

Add note about monomorphisation #24

Open marcospb19 opened 3 years ago

marcospb19 commented 3 years ago

Generics with static dispatch can lead to duplicated functions, and if they are big enough, it will make for a large piece of the final binary.

Alternatives are dynamic dispatch with dyn, or just removing generic code.

Is seems very important for minimizing binary size.

(If you don't want to change the function API, and the function is being generic over something like AsRef<Path>, you can also create helper function which resolves path.as_ref() and calls a non-generic function that receives &Path instead.)

johnthagen commented 3 years ago

This is a good idea.

Ideally, what I'd like is if we could gather some references to some quantitative research on the relationship between monomophisation and binary size.

If anyone in the community would be willing to look for/watch for such articles and post them to this issue, that would be great!

frencojobs commented 3 years ago

@jonhoo did a video about monomorphisation just today. It is really well explained, should consider it as one of the reference contents.

johnthagen commented 3 years ago

@jonhoo did a video about monomorphisation just today. It is really well explained, should consider it as one of the reference contents.

@frencojobs Thanks for this link, @jonhoo's content is great. Since it's a little more general, I think it's out of scope for min-sized-rust, but I'm glad it's linked in this issue for more people to find to learn from.

Artoria2e5 commented 3 years ago

It might also be useful to link to https://github.com/llogiq/momo, a proc_macro that makes monomorphisation (Into, AsRef, AsMut) helper functions for you.

twiggy seems to provide a functionality for analyzing monomorph code size bloat. https://rustwasm.github.io/twiggy/concepts/generic-functions-and-monomorphization.html (You could do that with hand-made tools downstream of cargo bloat output too.)

johnthagen commented 3 years ago

@Artoria2e5 Thanks for this. Both of those tools are great additions. I've added them in #31

johnthagen commented 9 months ago

A reference related to this topic was added in c4be196