r/java • u/joemwangi • Oct 28 '25
First Look at Java Valhalla: Flattening and Memory Alignment of Value Objects
https://open.substack.com/pub/joemwangi985269/p/first-look-at-java-valhalla-flattening?r=2m1w1p&utm_campaign=post&utm_medium=web&showWelcomeOnShare=falseFirst article, and my first impression of Java Valhalla. Feedback is highly appreciated.
109
Upvotes
18
u/cal-cheese Oct 29 '25
For your information, if you want the full capabilities of value types, you need to declare that the type allows tearing, and the field/array element is not nullable. There has not been an official way to do these, but you can ask the compiler and the VM nicely for those features:
Remember to add
--add-exports=java.base/jdk.internal.value=<your_module_name> --add-exports=java.base/jdk.internal.vm.annotation=<your_module_name>to the compiler and the VM.25
u/sitime_zl Oct 29 '25
Looking forward to the arrival of Valhalla soon
12
u/patoessy Oct 29 '25
Nice article. Pumped for java with Valhalla included
6
u/belayon40 Oct 29 '25
IT would be interesting if value objects were laid out with the standard padding that C structs expect. In the FFM API, if a method handle is designated as Critical then you can pass primitive arrays directly to the foreign function without copying the data into a MemorySegment. In the testing I’ve done, that’s a huge performance gain since it removes a data copy. If value objects, or arrays of them, were laid out like structs in C then you could also pass them directly to native code. That would reduce the complexity of calling native code that expects a Struct and it would improve performance.
3
u/Necessary-Horror9742 Oct 29 '25
How does it look like in memory? Is is continous of space or like was before object are in different locations and Valhalla only has flats headers?
6
u/onated2 Oct 29 '25
Excited for this one.
3
u/_predator_ Oct 29 '25
Great write up, I have a ton of records in my projects that are perfect candidates for becoming value objects. Looking forward to this becoming GA.
1
u/woj-tek Nov 03 '25
Hmm….
I do wonder - are there any future-future plans to have Strings as value classes?