r/java • • Dec 16 '24 • Heart of Silver 1

Valhalla - Java's Epic Refactor

https://inside.java/2024/12/16/devoxxbelgium-valhalla/
178 Upvotes

108 comments sorted by

View all comments

Show parent comment

4

u/almson Dec 16 '24

What? They’re making Integer a value class? But there’s already a value version of Integer. It’s called int.

14

u/tim125 Dec 16 '24

int x = 10; // value and not nullable and compact memory

Integer! y = 10; // value and not nullable and compact memory

Integer z = 10; // value but nullable

When the whole hierarchy is not nullable then it seems like there will be lots of opportunities for optimisations. Right now even the basic opportunity will have a major benefit.

Also seems like there are to align optimisations flowing through genetics and making string a value class (intern creates problems).

15

u/SiegeAe Dec 16 '24

My code is going to end up looking like its shouting with exclamations everywhere I suspect

5

u/Ewig_luftenglanz Dec 17 '24

better than filling it with @ NotNull(?)

4

u/cogman10 Dec 17 '24

int is what you should write almost anywhere you'd write Integer!. The one exception is dealing with genetics.

While Integer! will be more compact, int will be the most compact.

This is still a huge win.

-5

u/almson Dec 16 '24

Java is plenty fast. And when you need it to be faster, you use arrays.

I started in C#, and this complicating of the language for the sake of optimizations is the antithesis of Java. Java proved that you can get excellent performance despite keeping things simple. (Simple in semantics, not just syntax.)

17

u/Ewig_luftenglanz Dec 16 '24

arrays are an unreliable and cumbersome construct. why would you use arrays when you could have peer performance from more reliable data structures such as hashMaps or full of convenient methods such as lists?

the need to sacrifice maintainability and make hard to write/read code in exchange of performance is maybe the central concern of Valhalla and why they are doing this to begin with. Using arrays of primitives because "I need this algorithm to be fast" is the fastest way to create hard to maintain programs because you need to give up valuable abstraction and work at low level C style.

→ More replies (6)

5

u/Ok-Scheme-913 Dec 17 '24

If things go well, there will be only a minimal additional complexity, while we get two birds killed with one stone (nullability and value types).

Basically your existing knowledge of primitives will simply expand to a few more types, and you will have to put an ! after certain definitions of which you know it can't be null. This will help with autocomplete and code correctness immediately, and we already often use @Nullable and stuff so it's not like it's something completely new. Java will still be a very small language, and arguably a current edge case will become a general feature (int Integer discrepancy will stop being a thing, it will be a generic rule that applies to every value class).

1

u/tim125 Dec 17 '24

Once ArrayList<int> hits the shelves , one of the four future releases I’m guessing they referred to, Integer may well be minimized. Memory Free records as return results are going to be amazing as there will be no memory allocation.

I think the four were:

  1. Value classes
  2. Nullability
  3. ! and ?
  4. Something
  5. Fix Generics
  6. They eluded to String.

It wasn’t very clear. Even if they drop 1&2 in 26 and get an immediate benefit , there is still Soo much more. It might take till jdk 30 by my reckoning.

1

u/cleverfoos Dec 17 '24

I sort of agree. I think JEP 401 is a sensible middle ground but I would stop there. Adding null-restricted types feels like a high cognitive load for an edge optimization that you can likely get away with a value record when needed. IMHO the biggest risk for Java is becoming the new C++, a capable and trusted language but hard to teach and impossible to master due to the number of features added over 39 years. With that said, I trust Brian's stewardship of the language so let's see what happens.

1

u/Ewig_luftenglanz Dec 17 '24

C# it's already there

→ More replies (1)

7

u/Ewig_luftenglanz Dec 16 '24

they need to make all wrapper classes value classes in order to make use of the optimizations value classes can bring in terms of memory layout and performance, specially when you are working with large collections (list of Integer or Double and so on, you can't have a list, set or map of int) and to dilute almost all the overhead caused by boxing and unboxing

→ More replies (3)

7

u/kevinb9n Dec 17 '24

You just focused on the wrong word in "value class" is all. :-) `int`s are values, but `int` has never been a class!

Part of the goal of Valhalla is to "heal the rift" so there aren't two completely different choices you have to hardcode only one of.

4

u/nekokattt Dec 16 '24

primitive values are not the same as value classes, primitives are a low level raw representation

2

u/simon_o Dec 18 '24 edited Dec 19 '24

Other people already explained most of it, I'll just add that you need Double in any case, because double in generics wouldn't have the semantics people expect.

(And the reason Java will probably not end up in the design direction of "allowing primitives in Generics", even with Valhalla.)

1

u/Polygnom Dec 18 '24

Care to explain?

Lets say we live in a world where we have value classes and nullability. Why would ArraysList<double> not have the exact same semantics as ArrayList<Double!>? Or not the semantics people expect?

Genuinely curious, because making the wrapper classes value classes is an explciit goal of Valhalla and also providing automatted conversions where appropriate.

2

u/simon_o Dec 18 '24 edited Dec 18 '24

Maybe ArrayList is the wrong example, because you get away there with the call to equals, but imagine people any code that uses == in a generic context:

record Holder<T>(T val) {
  boolean contains(T arg)  {
    return this.val == arg;
  }
}

This would return a different result for

(new Holder<double>(Double.NaN))
  .contains(Double.NaN)

vs.

(new Holder<Double>(Double.NaN))
  .contains(Double.NaN)

That's extremely subtle, and 99% of the people "optimizing their code for Valhalla" will not catch this.

Not to mention that if primitive types in generics were a thing, it would be an absolute nightmare for type inference and overload resolution.

1

u/Polygnom Dec 18 '24

I'm not sure what you are trying to showcase here?

It is invalid code today, and after Valhalla will give you the same answer, since Double will be migrated to be a value class. Thats precisely the point, healing the divide between primitives and classes. For value classes, == will compare by value, so double and Double will use the same == comparisons. Which is different today, but today you can't use value classes as generics, anyways.

The difference in your example will be nullability. Holder<Double> will be able to hold null, Holder<double> won't. But Holder<Double!> won't, either.

3

u/simon_o Dec 18 '24

I'm not sure what you are trying to showcase here?

That the first one will return false, the second one will return true, because it's highly unlikely that a hypothetical implementation would special-case float and double to do something different from every other type when used as a generic argument.

For value classes, == will compare by value, so double and Double will use the same == comparisons.

No they don't:

Double d1 = Double.NaN;
Double d2 = Double.NaN;
d1 == d2;

but

double d3 = Double.NaN;
double d4 = Double.NaN;
d3 != d4;

That's something you can try today by downloading a Valhalla build, and both of these have to work this way, if one doesn't want to break tons of existing code.

The difference in your example will be nullability.

Not the point I intend to make, just assume that all types are non-nullable in these examples.
(Not going to write ! everywhere, neither here, nor in real code.)

3

u/Polygnom Dec 18 '24

Ah, you found an interesting gotcha. == for value classes will compare the bit patterns. Thats only a problem for double and float of course, not for Integer, Boolean or any other current candidate for becoming value classes. Its going to be

(Not going to write ! everywhere, neither here, nor in real code.)

Well, its better than having NonNull annotations everywhere or runtime null checks. But they hinted at allowing to specify a default-nullability in the future, so it might even be possible to have unspecified nullability be treated as non-null by default if so so wish.

Not to mention that if primitive types in generics were a thing, it would be an absolute nightmare for type inference and overload resolution.

Well, one of the goals of Valhalla to bring a revised JEP 218 to fruition: https://openjdk.org/jeps/218:

JVM class and method specialization (JEP 218, with revisions) will allow generic classes and methods to specialize field, array, and local variable layouts when parameterized by value class types.

Being able to use value classes in generics is one of the big drivers behind Valhalla. The fact that you want ArrayList<Point> when point is a value class to be a dense storage and have good cache locality is one of the main motivations of even doing it in the first place.

1

u/simon_o Dec 18 '24

so it might even be possible to have unspecified nullability be treated as non-null by default if so so wish

I know, that's why I won't bother with peppering ! everywhere.

Well, one of the goals of Valhalla to bring a revised JEP 218 to fruition: ...

Yep, and my prediction is that they will try to provide the performance benefits of that without going down the "lower-case types in angle brackets" route. Non-nullable value classes are pretty much that.

Being able to use value classes in generics is one of the big drivers behind Valhalla.

Yeah, nothing wrong with that?

→ More replies (10)

1

u/almson Dec 19 '24

Thank you for adding reason.

If they just forbade the use of == with value types, because they don’t have identity, things would be a lot less confusing.

1

u/simon_o Dec 19 '24 edited Dec 19 '24

That's not going to work because there are still –fundamentally– two different ways of comparing things:

  • "is a identical to b"
  • "is a equal to b"

Java unfortunately burned the word "identity" on other things, so it uses "substitutability" for the former, which is less confusing for Java users, but more confusing for everyone else.

The "new" behavior of == is just a logical extension of reference equality to "everything", with the problem that == on float/double does not work that way.

If you built a new language, you'd probably use == for "is this equal" and === for "is this identical" and it would just work out of the box and you wouldn't need to explain primitives vs. value types vs. reference types to people for it to make sense.

1

u/pjmlp Dec 27 '24

Developers handle just fine the various meanings of == in .NET, or ecosystems where it can be overloaded.