5 ms·
She mentions the C approach, C++ approach and Java approach. But I think she should have added the C# approach: taking all the advantages of the Java approach,
by knocte 8y ago
She mentions the C approach, C++ approach and Java approach. But I think she should have added the C# approach: taking all the advantages of the Java approach, without the slowness of casting.
- owenjonesuk 8y agoI think the author is a woman, from the name Emily.
- knocte 8y agofixed, thanks
- marmaduke 8y agoDoesn’t that work because C# has a compiler to JIT stuff at runtime? In principle it could work in te JVM as well right?
- magicalhippo 8y agoYes, the C# approach relies on JIT. IIRC for Java they decided backwards compatibility in the JVM was more important than doing it the C# way.
- int_19h 8y agoConversely, once you remove the JIT, a bunch of stuff stops working. Look at the restrictions on generics for .NET Native.
- magicalhippo 8y agoCasting isn't slow, it's the boxing. A generics List<T> isn't much slower than the non-generic List if T is some class. But if T is say an integer, then you're paying for the implicit boxing and unboxing.
- danieldk 8y agoA generics List<T> isn't much slower than the non-generic List if T is some class. This is only true generics using erasure, such as in Java. Since Java will store Objects (pointers to objects) in both cases. But in languages that use e.g. monomorphization a List<T> stores the Ts adjacent in memory, since the size of each element is known, whereas a non-generic list has to store pointers to objects, since the object sizes are not known and may vary per element. A generic List<T> that is monomorphized has two large benefits: fewer cache misses due to locality and more opportunities to inline T method implementations.
- magicalhippo 8y agoI was assuming the parent post was referencing the Java implementation as described in the article.
- Someone 8y agoYou can only do that if you can prove that T will never have subclasses that are larger. To check that, the compiler either has to see all code that will run in the process, which means whole program compilation and no plugins, or the language needs a way to make classes that cannot be subclassed, preventing code that the compiler didn’t see from subclassing T (https://en.wikipedia.org/wiki/Class_(computer_programming)#Non-subclassable https://en.wikipedia.org/wiki/Class_(computer_programming)#N...)
- danieldk 8y agoYou can only do that if you can prove that T will never have subclasses that are larger. Only if generic type parameters are covariant, if type parameters are invariant, this is not a problem. I guess that most languages with subtypes use invariance by default. Also, many languages that do generics through monomorphization do not support subclassing (Rust, Haskell, etc.). no plugins, Well, there is always the option of boxing collection elements. (E.g. by using Box in Rust.)
- 8y ago