Hacker News new | ask | show | jobs
by pbadenski 1985 days ago
Thank you for mentioning monomorphization - I wasn't aware of this concept.

Would you maybe if there's any source discussing how this is solved in different languages and performance consequences of it?

2 comments

Not really answering your questions, but regarding monomorphization, you can see a comparison of code size and compilation speed between printf, std::format and C++ iostream. The last one is monomophization, I believe.

https://github.com/fmtlib/fmt#compile-time-and-code-bloat

I'm not aware of a high-quality source discussing this. I'd be interested in seeing one. Off the top of my head:

* In many languages, monomorphization is impractical. Eg, in C it requires macros or external code generators. Java and Go don't even have macros. So it's rarely done. Instead, stuff uses type erasure, dynamic dispatch, more heap allocations, and tables like the ones described in that dave.cheney.net link. Maybe some JITs or even some ahead-of-time compilers do monomorphization internally in some cases, but I'm speculating rather than speaking from knowledge.

* In C++ and Rust, monomorphization is easy. You don't have to do it, but it's easy, so many people do. The containers in the standard library are monomorphized. This code can perform very well in any particular case, but in aggregate it's large enough that I have doubts about whether it's always worth it for the whole program.