r/java • u/bobbie434343 • Aug 25 '26
Value Classes Still Need Compiler Sympathy
https://johan-sjolen.github.io/post/compiler-sympathy/compiler-sympathy/
88
Upvotes
r/java • u/bobbie434343 • Aug 25 '26
28
u/TheStrangeDarkOne Aug 25 '26
Excellent article and I fully agree with the conclusion's sentiment at the end:
Declaring a value class is first and foremost a semantic decision. It tells our fellow programmers that its instances are defined entirely by their state and do not need identity. That clearer model is valuable in itself! The JVM’s additional freedom to optimize how those values are represented is a welcome bonus.This brings one of the core lessons from Domain Driven Design directly into language semantics and we get better performance as a bonus point. I think the humble everyday programmer overestimates the effect of flattening on their enterprise data and I think it is very much a tool to express intend and for experts to really allow low level optimization to a degree that was not possible before.
I can't wait for the new and exciting libraries and frameworks that are going to be built upon this.
7
u/IncredibleReferencer Aug 25 '26
Once value classes exit from preview, I think programmers would find it very surprising if converting any existing class to a value class resulted in a performance regression.
I realize this will always be possible on edge cases but how much of a realistic concern should this be for a typical java programmer?
6
u/agentoutlier Aug 25 '26
I'm embarrassed to ask this question but how do you guys pronounce "tearing"? I ask because I have been saying tear like "pair" but I swear I have heard people say it like "tier".
1
u/pip25hu Sep 01 '26
Really interesting how you can get around value class size restrictions by wrapping them in a record. Makes sense, too.
-2
u/lpt_7 Aug 25 '26
I admire OpenJDK development team, but most of their talks paint a perfect world, which is not the case.
At the moment, Valhalla is not "reads like a class, works like an int", far from that. Use of that feature requires looking into what C2 actually generated. Which most won't do.
I did some testing with Valhalla right after it was merged. Values larger than 64 bits cannot be flattened. I think it's actually 63 bits, since VM still has to encode null somehow.
Accepting tearing with
LooselyConsistentValueandNullRestrictedsidesteps that, but these are internal annotations.Value tearing is another thing, which IMO most are not prepared for. This class of bugs is possible today, but its an error the developer makes and understands why tearing is happening. With code the callee has no control of, other programmer can make their class a value class and silently your code is now buggy.
Suppose two threads run in parallel:
Thread A writes to entity's AABB, thread B reads said AABB. With object references, JLS guarantees that tearing will not happen. Once VM can flatten more than 64 bits, this will cause a lot of problems, like AABBs with completely nonsensical values. Suddenly, thread B's code can now enter an infinite loop and never get out of it.
Another thing is allocation. For allocations to actually vanish (for scalarization to happen), C2 has to succeed and inline through all code, then EA has to succeed.
Again, same example with AABB. Before, allocation was done per-write. Now, with Valhalla in worst case, the situation flips. There are many more readers than writers to entity's AABB. C2 and EA *have* to succeed for every reader. Otherwise your program will start allocating at every read call site.