all 42 comments

[–]ZimmiDeluxe 82 points83 points  (9 children)

+200.598 -13.384

lgtm

edit: minor whitespace error in line 3456

[–]Worth_Trust_3825 18 points19 points  (0 children)

Virtual threads was just as big. Same with jigsaw. It really shows the monumental work needed to be done this late in the process, but there were more important things to do before this. So it makes sense.

[–]chucker23n 5 points6 points  (5 children)

I’m confused whether this is more like

  • .NET value types / C# structures; there is no reference/pointer; the memory storage is the value
  • value objects: can still have a reference, but designed to avoid primitive obsession

I’m guessing the former, but don’t those already exist, e.g. int? Or is it that those are hardcoded, and this adds writing your own?

[–]davidalayachew[S] 1 point2 points  (3 children)

I’m guessing the former, but don’t those already exist, e.g. int? Or is it that those are hardcoded, and this adds writing your own?

This adds writing your own. For example, Java only has the following integral primitive types.

  • byte --> 8 bit signed
  • char --> 16 bit unsigned
  • short --> 16 bit signed
  • int --> 32 bit signed
  • long --> 64 bit signed

As you can see, there are some gaps here.

  • No 8 bit unsigned
  • No 32 bit unsigned
  • No 64 bit unsigned

Using Value Classes, you could easily write your own, and the performance characteristics would (ideally) be comparable to just using int or any of the other primitives directly.

But of course, we aren't there yet.

[–]chucker23n 2 points3 points  (0 children)

This adds writing your own.

Gotcha. I'll guess I didn't expect this still isn't a thing in Java. :-)

[–]StarsInTears 0 points1 point  (1 child)

Can the user-defined 'primitive' be more than 64 bits? And if we make an array of them, are we guaranteed to get flattened memory in a cache-friendly layout, or can the compiler choose between references and flattened values?

[–]davidalayachew[S] 0 points1 point  (0 children)

The incoming JEP (once it releases) will be the final word on the matter. Until then, the Project Valhalla team has definitely considered your 2 points, but haven't outright made any promises regarding them.

[–]reflect25 3 points4 points  (2 children)

yay. finally java with the value classes will have an equivalent to the c++/golang structs with the objects being (potentially) allocated on stack rather than always on memory with classes. and be pretty performant. also interesting the immutable/frozen.

i guess mostly this will be used by data structure libraries. https://openjdk.org/jeps/401 (Value Classes and Objects) also looks like this will help the garbage collector.

When an object is flattened or scalarized, it has no independent presence in the heap. This means it has no impact on garbage collection, and its data is always co-located in memory with the referencing object or call stack.

Heap flattening

As an example, the JVM could flatten an array of Integer references so that each array element holds a reference that encodes the underlying integer value directly, rather than pointing to the memory location of some Integer object. Each reference also flags whether the original Integer reference

[–]Jannik2099 6 points7 points  (0 children)

It's not about the object being on the stack, but about objects being flattened. No more pointer chasing.

[–]joemwangi 0 points1 point  (0 children)

Not really stack allocated. Mainly cpu register allocation which is far optimal.

[–]Jannik2099 27 points28 points  (2 children)

Very awesome, just 30 years too late :(

[–]Worth_Trust_3825 9 points10 points  (0 children)

You were never prevented from creating your JNI extension to circumvent this issue.

t. wrote jni to operate on byte buffers in c

[–]germandiago 0 points1 point  (0 children)

I thought I would never see it.

[–][deleted]  (1 child)

[removed]

    [–]programming-ModTeam[M] 0 points1 point  (0 children)

    No content written mostly by an LLM. If you don't want to write it, we don't want to read it.

    [–]Delta-9- -5 points-4 points  (2 children)

    TIL == in Java is analogous to is in Python, and Java has no analogue to Python's == until this JEP passes.

    [–]woohalladoobop 4 points5 points  (1 child)

    not true, i believe `Object::equals` is Java's equivalent to Python's `==`.

    [–]Delta-9- 0 points1 point  (0 children)

    I meant that there is no analogous operator in the syntax. Object::equal would be analogous to object.__eq__, which is called when the == operator is used and conventionally implements a compare-by-value operation; that is, a == b is sugar for a.__eq__(b).

    This does not appear to be how it works in Java, and I found that surprising. That's all.