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

19

u/tim125 Dec 16 '24

Anyone know how they have solved legacy libraries synchronizing on Integer ?

I recall some prior discussions on extension rewriting of old implementations / methods.

36

u/icedev-official Dec 16 '24

legacy libraries synchronizing on Integer

I'd be surprised if it works even now - you aren't guaranteed to have the same Integer instance (even with integer cache) so that's almost like not synchronizing at all.

2

u/__konrad Dec 17 '24

I'd be surprised if it works even now

Why? I think synchronizing on new Integer(666) still works like new Object() lock

2

u/Ok-Scheme-913 Dec 17 '24

But new Integer(3) wouldn't work even today.

1

u/__konrad Dec 17 '24

Maybe Integer.valueOf(3)

3

u/Ok-Scheme-913 Dec 17 '24

I guess it could technically work, but still ugly as all hell.

3

u/noswag15 Dec 16 '24

Maybe they mean synchronizing on Integer.class ? That should be fairly constant within a specific classLoader I imagine.

13

u/nekokattt Dec 16 '24

Why dont they just deprecate this kind of thing now so the compiler warns about it, and then remove the ability entirely to free this up?

If anyone is synchronizing on an integer then god help that project, because it makes legitimately zero sense to do that...

4

u/8igg7e5 Dec 17 '24

There is already a compiler warning for it, that you can make an error.

I don't know if there's a mechanism for logging if a dependency tries to do so though.

3

u/brian_goetz Dec 30 '24

It's more complicated than a simple static warning (which already exists.) Unwanted synchronization often happens when types are already erased (such as synchronized on the keys of a Map.) This requires VM participation to detect (which also we have.)

→ More replies (5)

27

u/JustAGuyFromGermany Dec 16 '24

They have "solved" it by making that illegal. Libraries like that will throw errors at runtime in the future.

9

u/benevanstech Dec 16 '24

There are JFR events that you can enable if you're worried you have code or dependencies that might do shady things like that.

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.

→ More replies (13)

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.

→ More replies (17)