3 ms·
A single plugin case is a kinda optimistic wishful thinking. Sure, this case happens. Sometimes. But in real code you often have plenty of things like iterator
by pkolaczk 4y ago
A single plugin case is a kinda optimistic wishful thinking. Sure, this case happens. Sometimes.
But in real code you often have plenty of things like iterators or lambdas, and you'll have many of types of those. So the calls to them will be megamorphic and no JVM magic can do anything about it.
While in C++ world you'd use templates or in Rust you'd use traits, which are essentially zero cost, guaranteed.
- benibela 4y ago>While in C++ world you'd use templates or in Rust you'd use traits, which are essentially zero cost, guaranteed. Templates create more code If the code becomes too large to fit in the cache, it becomes very slow
- pkolaczk 4y agoSomehow I never noticed it happening in practice. In all the cases where cache was the problem, it was caused by data, not code. CPUs prefetch the code into cache quite well.
- NavinF 4y agoAre you talking about type erasure and generics? If so I agree, but that’s unrelated to devirtualization
- pkolaczk 4y agoThey are kinda related in a way that Java implementation of generics does not help with devirtualization, while C++ templates / Rust traits do help by not needing virtual calls from the start. Consider the pre- Java 1.5 sort method: Collections.sort(List list, Comparator comparator); If you load more than one Comparator type, then the calls to comparator are megamorphic and devirtualization won't happen unless the whole sort is inlined. In languages like C++, you'd make it a template and the compiler would always know the target type, so no need for virtual.