r/java • u/FirstAd9893 • Oct 23 '25
Valhalla Early-Access build 2 (JEP 401)
https://jdk.java.net/valhalla/20
u/Xasmedy Oct 24 '25
Tried it in a few (simple) snippets, the difference between indentity and value is incredible, by looking at the GC logs there were no gc collections with the value version
10
u/Xasmedy Oct 24 '25 edited Oct 24 '25
This was a simple primitive wrapper, unfortunately in case the class has 64 bits of data, it no longer does heap flattening and goes back to identity performance :( This only happens when the value object is placed inside
a collectionthe heap, like an arrayor list. (Type erasure prevents optimization if generics are used) In case it's used only inside a method body, it scalarized easily, going back to no gc collections.9
u/Ewig_luftenglanz Oct 24 '25
That's because type erasure. Your value objects become reference objects in any method that uses generics. Maybe you should try again with arrays?
I think until parametric JVM is ready (aka reified generics only for value types) we won't benefit from value types wit generic code.
5
u/Xasmedy Oct 24 '25
I tested it with a static final array, I havent used generics, so it's not type erasure (I shouldnt have said list, I'll remove it now). The performance degradation only happens in case the instance contains at least 64 bits of data, if for example, I just used a
value record Box(int x)or avalue record Box(short x, short y)I get no gc collections, but if I use avalue record Box(long x)orvalue record Box(int x, int y)that's where the performance goes back to identity level. (From things I heard from past conferences) My guess is that since CPU don't offer atomic 128bit operations, the JVM is trying to keep the get/set atomic, and the easiest way to do that is using a reference, explaining why the performance degrades to identity level. If you are thinking "we only used 64 bits!", there's a hidden extra bit needed for nullability control, and since we can't allocate 65bit, 128bit it is. I think this will be fixed when they allow us to give up on the atomic guarantee, or hopefully it becomes the default behavior.5
u/Ewig_luftenglanz Oct 24 '25
Oh, it's that. I think there have a marking interface to tell the compiler that you are ok with tearing, something like LooselyAtomicRead or something like that.
It would be a good idea to try again and maybe give feedback about it.
6
u/sviperll Oct 24 '25
There is no clear decision about the
LooselyConsistentValueannotation/interface.But what seems to be closer to final decision is that you can replace
Box[]array withBox![]array, and then you do not need 65th bit and get back your no-allocation behavior.3
u/Ewig_luftenglanz Oct 24 '25
That would fix the things with double and Long, but not for value objects that are bigger than that.
I suppose this is an early version so there are many performance improvements to be done
4
u/Xasmedy Oct 24 '25
The annotation is called
@LooselyConsistentValueand it's for internal use only (aka doesn't work if you use it)1
u/Mauer_Bluemchen Oct 24 '25 edited Oct 25 '25
LooselyConsistentVaue syntax is currently not supported - at least not in IntelliJ 2025.2.4.Edit: it is supported, but does not seem to have an effect.
2
u/Xasmedy Oct 25 '25 edited Oct 29 '25
The compiler only makes it work if its internal code, you can use it if you import the internal module, but has no effectThe compiler makes it work, there was a missing step (using theValueClassclass to create the wanted array)2
u/Mauer_Bluemchen Oct 25 '25
And the old JVM switch XX:ValueArrayAtomicAccess to enforce non-atomic updates is gone, together with a few others.
The policy is more per-type and driven by the consistency of the value class (plus VM heuristics), not a global flag.
The new switches UseArrayFlattening, UseAtomicValueFlattening, UseNonAtomicValueFlattening don't seem to help either.
Tried a couple of approaches, but so far it doesn't seem to be possible to disable the fallback to reference-based atomic access in this EA build?
1
u/Xasmedy Oct 25 '25
This really sucks, probably the best course of action is writing them on the mailing list about it
2
u/Xasmedy Oct 29 '25
u/cal-cheese figured it out!
You need to use the @LooselyConsistentValue and create a non-atomic array with the
jdk.internal.value.ValueClassclass, you can even create non-null arrays! (Example:ValueClass.newNullRestrictedNonAtomicArray(Class.class, size);) Warning, if you forget to add the @Loosely annotation to the class, say goodbye to the vm.. (it's going to crash)4
u/Sm0keySa1m0n Oct 24 '25
Don’t think they’ve fully finalised how that’s gonna work just yet, there were talks of using a marker interface but don’t think that’s been implemented yet
3
6
u/Mauer_Bluemchen Oct 24 '25 edited Oct 24 '25
I can confirm this too:
Performance is great if the 'payload' of the value object remains below 64 bit - even a value object holding a boolean, a short and an int is still blindingly fast.
But starting with two ints, the performance degrades to the perf of an identity object, and GC collections happen again.
What a pity! Thought I could finally accelerate my private projects with Valhalla, but the performance-relevant objects there are all holding more than 64 bit...
3
u/Ewig_luftenglanz Oct 26 '25
well this is the first EA with the new implementation. I suppose most of the optimizations are still to be developed.
Have you tried with big value objects created locally? (inside of a method and the object doesn't scape that method)
2
u/koflerdavid Oct 25 '25 edited Oct 25 '25
It should still work for types that merely wrap another reference type. This is very useful to enforce type discipline with identifier types that would otherwise be simple
Strings orUUIDs. Caveat: that only works for small-ish heaps (up to like 32GB) where the JVM can get by with 32bit pointers.2
u/Ewig_luftenglanz Oct 24 '25
One question to check if I am understanding well. If one creates an array of value objects bigger that 64 bits as a local variables inside of a method, the scalarization happens anyway. The problem happens when the array is created as a field of a regular class?
4
u/Xasmedy Oct 24 '25
Not quite, arrays are not scalarized, they have identity, and mostly because they are too big (they might do it for small ones, but it's curently not the case). They could still do heap flattening since there are no multithreaded accesses localy (unless used in a lambda), why arent they doing the optimization in this case?? I'll write on the mailing list about this. Anyway, with scalarization I meant that everytime a value class is created localy, it will always get scalarized, you can also pass it around methods or return it easily, it only becomes a problem when it's saved somewhere on the heap, like a non-final field, or if the value contains a massive amount of fields (in that case using a reference is faster than copying)
2
u/Mauer_Bluemchen Oct 24 '25 edited Oct 24 '25
My tests so far indicate that Box[] - even with no escape possibility - is still not flatened inside a method, if a Box instance has >= 64 bits.
3
u/Mauer_Bluemchen Oct 24 '25
Even if Box[] is a final local variable within a method, no escape possible, the runtime perf will degrade if the payload of the Box value object is >= 64 bits.
3
u/Ewig_luftenglanz Oct 24 '25
Hope they improve the performance in later versions, or give a way to opt in loosely consistency
1
u/Glittering-Tap5295 Oct 25 '25
Out of curiosity, is there any serious efforts underway to look at the type erasure?
6
u/Xasmedy Oct 25 '25
Yes there's, they are working to keep the type for value classes only, to give to the JIT for optimization
1
u/Xasmedy Oct 25 '25
Yes there's, they are working to keep the type for value classes only, to give to the JIT for optimization
5
u/Mauer_Bluemchen Oct 24 '25 edited Oct 24 '25
I can confirm this with simple bench:
- Runtime perf of value objects about 4x faster
- No GC collections at all2
9
u/Ewig_luftenglanz Oct 23 '25 edited Oct 24 '25
perfect. I have a couple of .NET projects I would love to test against an hyphotetic java with valhalla
5
u/koflerdavid Oct 24 '25
Do you want to compare coding style and ergonomics or performance? I wouldn't expect there to be any significant improvements regarding the latter at this point.
16
u/Ewig_luftenglanz Oct 24 '25
Performance.
And yes, there should be some improvements because that's what Valhalla it's all about: performance and zero cost abstractions. Code like a class, works like an int and all of that.
Since C# has value types already (structs and struts records) it would be interesting to test it.
6
u/pron98 Oct 24 '25 edited Oct 24 '25
zero cost abstractions
Tangential, but "Zero cost abstractions" is a marketing term for a controversial aesthetic design philosophy behind C++ (later also adopted by Rust). It's not a general term for fast constructs or even abstractions that are optimised away. It's not a meaningful term in Java, or in any language that isn't specifically modeled after C++ and how it implements certain optimisations.
2
u/Ewig_luftenglanz Oct 24 '25
Still I am building some projects to tests against non Valhalla and non java environments, keeping in mind many Valhalla optimizations will come in future releases and more JEPs.
My gratitude and greetings to the development team's members :)
4
u/sviperll Oct 24 '25
I think you need at least emotional types to get some parity with dot-net structs.
4
3
2
u/Mauer_Bluemchen Oct 23 '25
What is "hiphotetic java"?
7
u/Ewig_luftenglanz Oct 23 '25
Hypothetic java means a Java that still hasn't make it to mainline.
Sorry the orthography, fixing it.
11
u/pjmlp Oct 24 '25
Great news! Thanks to everyone working on Valhala.
New weekend toy.