4 ms·
Anyone can learn how to write generic code, the trick is knowing the price of the particular implementation of generics and when to pay it. https://planetscale
by Comevius 4y ago
Anyone can learn how to write generic code, the trick is knowing the price of the particular implementation of generics and when to pay it.
https://planetscale.com/blog/generics-can-make-your-go-code-slower https://planetscale.com/blog/generics-can-make-your-go-code-...
- Traubenfuchs 4y agoI guarantee you that less than 1% of users of generic languages have the knowledge to argue about this. Java devs are in a special position since all had to find out the hard way you can't acquire generic parameters at runtime due to type erasure. In Java generics are just syntactic sugar, but this gives them the lowest (none?) runtime overhead.
- addaon 4y agoType erasure definitely does not give the lowest runtime overhead most of the time, although (a) you can always find special cases and (b) it's great for code size, which sometimes is performance-determining at the system level due to caches. The huge thing that type erasure gives up compared to per-type instantiation is the ability to do per-type inlining and the further optimizations that enables.
- erik_seaberg 4y agoYeah, monomorphization of a method is probably cheaper and faster than boxing every argument and result. I expect value types to help a lot in JVM footprint.