Since being created, the Java pitch was "don't worry about low-level stuff like value/reference classes, a Sufficiently Smart Compiler will automatically pick the best option given your code and runtime profiling". What changed, why suddenly they adopt C++ features they explicitly excluded? https://wiki.c2.com/?SufficientlySmartCompiler
This was pretty much always wrong. Nobody writing performance critical code in Java trusts the compiler to magically figure things out, and the sort of code you arrive at if you want Java to go fast is generally unidiomatic.
Value Classes Still Need Compiler Sympathy
41–50 of 51 posts
Re: Value Classes Still Need Compiler Sympathy
#42Re: Value Classes Still Need Compiler Sympathy
#43I guess I follow the technical reason that the interface doesn’t override the method so there can’t be a specialized version of it. But that’s only visible through reflection right? Not in the direct language semantics?
Re: Value Classes Still Need Compiler Sympathy
#44Earlier quoted context omitted.
Apparently linear search now beats hashmap if you have less than TWO HUNDRED elements. Crazy!
But when you compare native integers, not something more complicated, right?
Re: Value Classes Still Need Compiler Sympathy
#45Earlier quoted context omitted.
So now the result of `new HashMap ()` should be backed by an array for the first 200 elements or so? Potentially the size depending on the L1 size etc.
Not in Java, because a linear search map in Java means traversing over 100 (on average) pointers.
Does this also mean we need to worry about word aligning our fields?? What about strings?
Re: Value Classes Still Need Compiler Sympathy
#46Earlier quoted context omitted.
> So O(n) could be faster than O(log n) depending on the size of the data structure and interactions with RAM pre-fetch, etc. This has always been the case. The RAM effects only changed at which point the O(n) stops being faster than the O(log n) solution.
Apparently linear search now beats hashmap if you have less than TWO HUNDRED elements. Crazy!
Re: Value Classes Still Need Compiler Sympathy
#47Earlier quoted context omitted.
Yes, this was the idiom in the 90s, but Escape Analysis has not proven to be powerful enough to optimize away identity. And looking at all the edge-cases and possible data-races via tearing, declaring something as a value must be an explicit design decision that cannot be inferred by a compiler or optimizer alone.
Note that tearing is NOT a risk for value classes in Java - the JIT compiler is not allowed to optimize the layout of any value class that can tear on the current architecture (so, for any value class larger than 64 bits on x86-64).
But whether your data’s integrity can be broken by such must be decided by the class author.
Re: Value Classes Still Need Compiler Sympathy
#48I’m not sure I fully understand why the compiler can’t derive the specialized apply method in this case. I guess I follow the technical reason that the interface doesn’t override the method so there can’t be a specialized version of it. But that’s only visible through reflection right? Not in the direct language semantics?
I think that your misunderstanding comes from the fact that you base your understanding in Java semantics, but we really need to deal with the JVM's semantics. Your suggestion only makes sense when:
a) All of the typing information is preserved across a whole build b) The build is entirely static, no new classes can be loaded
Both of these are at odds with how the JVM functions. First: We lose type parameters on compilation, so we only see that Frobber extends Fun, the type parameters are lost and its usages are converted to Object.
Second: The JVM loads code by lazily loading classfiles. All classfiles are independent, and more can be added to a system.
Re: Value Classes Still Need Compiler Sympathy
#49Earlier quoted context omitted.
Note that tearing is NOT a risk for value classes in Java - the JIT compiler is not allowed to optimize the layout of any value class that can tear on the current architecture (so, for any value class larger than 64 bits on x86-64).
The tradeoff being that tearing is what enables flattening. So if you truly want the best performance (cache lines etc.) you must enable it. But whether your data’s integrity can be broken by such must be decided by the class author.
Yes, exactly.
> So if you truly want the best performance (cache lines etc.) you must enable it.
Someone was saying that this is planned as an option in some future version of Java. As it stands, this is just not possible, and to me this suggests that the performance gains from Project Valhalla in this Java will be minimal.
Re: Value Classes Still Need Compiler Sympathy
#50I’m not sure I fully understand why the compiler can’t derive the specialized apply method in this case. I guess I follow the technical reason that the interface doesn’t override the method so there can’t be a specialized version of it. But that’s only visible through reflection right? Not in the direct language semantics?
Hi, the author of the blog post here. I think that your misunderstanding comes from the fact that you base your understanding in Java semantics, but we really need to deal with the JVM's semantics. Your suggestion only makes sense when: a) All of the typing information is preserved across a whole build b) The build is entirely static, no new classes can be loaded Both of these are at odds with how the JVM functions.…
Would including the specialized method in the derived interface bytecode preemptively take us all the way to C++ templates and ruin everything that’s good about Java? Or is it a matter of type erasure being consistently applied and if it were ignored selectively it would make everything more confusing?
Like if we had final class Foo implements Func then we’d presumably need to supply a typed implementation of apply, so the fact that we can get away with the generic one in a specialized interface feels surprising, even though I can see why it’s technically correct.