News for hackers. Stuff that matters.
Re:How generics are implemented in Go 1.18 by josephg on Tuesday March 01, 2022 @03:37PM (#30519664)
The process of expanding genetic code into concrete code based on the types (monomorphizing) can also be an expensive process. At least, people in the rust world point to it a lot as a reason why some crates compile slowly. Monomorphizing multiplicatively increase how much code LLVM needs to process (and optimize).

Mind you, C/C++’s ridiculous header system is probably still a much bigger issue. Especially because C++ templates usually need to go in header files.

Re:How generics are implemented in Go 1.18 by yakubin on Tuesday March 01, 2022 @04:20PM (#30520147)
Although monomorphization takes a significant chunk of a typical Rust compilation, it pales in comparison to LLVM codegen and execution of procedural macros. So although it's academically worthy of note, optimization-wise it's not where the focus should be. Personally, I'd like to see someday a Rust compiler which is not based on LLVM. Both Zig and Jai compile much faster when using their non-LLVM backends than when using LLVM, so Rust is not the odd one here either.
Re:How generics are implemented in Go 1.18 by nicoburns on Tuesday March 01, 2022 @05:34PM (#30520975)
> Although monomorphization takes a significant chunk of a typical Rust compilation, it pales in comparison to LLVM codegen

It's not the monomorphization itself that takes a long time, it's the LLVM codegen due to all the extra code being generated from duplicated function implementations. I've heard macros slow for largely the same reason, it's not the macro execution itself that's slow, it's compiling all the generated code.

Re:How generics are implemented in Go 1.18 by yakubin on Wednesday March 02, 2022 @05:47AM (#30525921)
> I've heard macros slow for largely the same reason, it's not the macro execution itself that's slow, it's compiling all the generated code.

If that were true, the same issue would apply to hygienic macros, yet it doesn't. It's distinctly a problem with proc macros.

Re:How generics are implemented in Go 1.18 by jcelerier on Tuesday March 01, 2022 @04:55PM (#30520583)
> Mind you, C/C++’s ridiculous header system is probably still a much bigger issue. Especially because C++ templates usually need to go in header files.

IDK about that. A project I'm working on is template-heavy in a major way, entirely header-only (~15kloc of this: https://github.com/celtera/avendish/blob/main/include/avnd/c... more or less) but with a barely correctly set-up dev environment with clang and PCH (a couple lines in CMake), ninja and mold, individual rebuilds are pretty much instant ; a complete rebuild which on my machine builds 49 libraries and 38 executables takes a whopping 7 seconds, CMake included.

Re:How generics are implemented in Go 1.18 by xigoi on Tuesday March 01, 2022 @04:09PM (#30520025)
Increases compared to what? If you don't use generics and write the code for each type manually, you'll end up with exactly the same thing.
Re:How generics are implemented in Go 1.18 by zwieback on Tuesday March 01, 2022 @05:43PM (#30521053)
I often wonder about that - if I was going to write by hand I might end up extracting a simple interface, do some type coercion/promotion or some other trick to reduce the number of functions I need to write.

I read about how .Net does Generics at the VM level and was very impressed, a good compromise between C++ "macro expansion" and Java "type erasure", both of which seem extreme.

At the end of the day, though, when generics are present I stop worrying and just use List<int>, List<float>, List<char>, etc. without a second thought. The compiler, VM or runtime can deal with it although I might get punished in some way.

Re:How generics are implemented in Go 1.18 by pshc on Tuesday March 01, 2022 @05:17PM (#30520807)
Increased compile time compared to doing polymorphism with an extra layer of indirection, I’m guessing