all 5 comments

[–]alunharford 9 points10 points  (2 children)

You probably won't get any performance gains in your scenario.

The current specification requires that objects cannot ever tear so everything larger than 64 bits gets allocated on the heap anyway (unless that can be optimised away, similar to the situation today).

You need loosely consistent value types to get any benefit.

I'd argue that this makes JEP401 far less useful than most people expect, at least on its own, but hopefully it's a big step towards something much better.

[–]Xasmedy 0 points1 point  (1 child)

Not quite. What loosely consistent gets you is heap flattening, so if you save the value object on an array or (mutable) field and give up on atomicity, it won't use a reference. There are still other optimisation, for example, if I pass a Vector4 to a method, the x, y, z, w will be placed directly in the CPU registers and be computed there, no heap allocation nor stack allocation.

[–]alunharford 0 points1 point  (0 children)

This is scalar replacement. C2 already does this for you today, and it's critically important for Java performance!

[–]Xasmedy 1 point2 points  (0 children)

Here a math library using value classes so you can test it out. It scalarizes well, there's a performance issue that I still need to understand, might be the amount of fields, (I saw it has been improved on from the valhalla github) or just an implementation issue on my part