3 ms·
Java optimizes at the method level and the majority of methods when writing an app will have constant types used against them. Your method signature may look l
by cogman10 4y ago
Java optimizes at the method level and the majority of methods when writing an app will have constant types used against them.
Your method signature may look like
void foo(Iterable<Bar> b)
which would make you think of vtables and casting performance losses.
However, Java's JIT is fairly cleaver. It can tell that you always call `foo` with an `ArrayList` and it can tell that `ArrayList`'s iterator always emits a `Bar` Object. From there it can elate a lot of the type tables and virtual dispatch to just be direct dispatch. It does that at the cost of a couple of added runtime checks to make sure those assumption's aren't violated.
The impact of generics on performance has, for me, generally been pretty low. I've seen a much higher impact due to java's memory model (which is getting fixed with Valhalla).
This, btw, is how JavaScript's and indeed most high speed JITs function.
- lifthrasiir 4y agoYeah, JIT does subsume naive AOT + generics for many cases, but it is a bit unfair comparison because there is no reason for other languages to implement such optimizations (PGO is one of them). And such an inference is not free and causes usual JIT pain points.