min-sized-rust icon indicating copy to clipboard operation
min-sized-rust copied to clipboard

Add note about monomorphisation

Open marcospb19 opened this issue 3 years ago • 6 comments

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

marcospb19 avatar Apr 17 '21 16:04 marcospb19

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!

johnthagen avatar Apr 29 '21 17:04 johnthagen

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

frencojobs avatar May 01 '21 11:05 frencojobs

@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.

johnthagen avatar May 08 '21 12:05 johnthagen

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

Artoria2e5 avatar Aug 06 '21 06:08 Artoria2e5

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

johnthagen avatar Aug 17 '21 11:08 johnthagen

A reference related to this topic was added in c4be196

johnthagen avatar Dec 30 '23 15:12 johnthagen