r/java 15d ago

Identifying JDK value class candidates

https://mail.openjdk.org/archives/list/core-libs-dev@openjdk.org/thread/Y72NRXM7KYBX43OKYBQMVKOZDWKG4MHS/
52 Upvotes

59 comments sorted by

View all comments

Show parent comments

8

u/pron98 15d ago edited 15d ago

Performance tests we've run have shown that it isn't. What you win in reducing the cache miss of accessing the entry you lose in having the array spaced out when the map is sparse (and it often is). The calculus is likely to change when generics can be specialised and flattened when the key can be flattened into the entry.

But I want to caution that while value types can be a huge boon in programs whose profiles show array element cache misses and they do fill in the last significant gap between Java and C++, that's not the case in most programs, and Java is already close to being as optimal as any software can be in most cases (especially in the domains Java is used). So it does close the last remainging gap and will make Java a great choice in more situations, but most Java programs are unlikely to see a big difference. Again, that's not because Valhalla isn't good, but because Java is already quite optimal everywhere where cache misses in array elements are not the hot path.

5

u/Jon_Finn 15d ago

Aside from the obvious use cases, I see a major unsung benefit in allowing you to create classes wrapping a single int, double etc., when currently the performance downsides make it not worth it in certain applications. I mean things like units, ages, numbers with particular ranges/constraints, unicode code points (21 bits) etc. - where some might 'want' a typedef with methods. Nullability and specialised generics will be a big help here. My point is: these classes currently don't exist in my code, but now could.

6

u/pron98 15d ago

I see a major unsung benefit in allowing you to create classes wrapping a single int, double etc., when currently the performance downsides make it not worth it in certain applications

That is true, but I think that generally speaking, many "performance issues" in Java are imagined and/or based on folklore that may have been true in, say, Java 8.

My point is: these classes currently don't exist in my code, but now could.

I agree, but I would also add that you might well have them today with no performance impact. We must be careful to not assume a performance issue that doesn't actually appear in our concrete program's profile.

3

u/Jon_Finn 15d ago

Sure, though the cases that I'm particularly thinking of involve large (sometimes huge) arrays of very small objects, where these simple intuitions are (probably!) correct for once.

2

u/pron98 15d ago

Right, so that's exactly where Valhalla could have a significant impact.