Help My LocalDate Isn't Flattened

https://news.ycombinator.com/rss Hits: 2
Summary

Help! My LocalDate Isn’t Flattened Project Valhalla promises to flatten objects in the Java virtual machine. Of course, it is much better to have an array of fifty million values than fifty million object references, each with a header and the value. Just before giving a presentation about that at the JFall conference, I saw an article at inside.java that promised such flattening with an array of LocalDate objects. But I could not reproduce it. The culprit? Serialization, of course. Read on for the gory details! Project Valhalla Eleven years in the making, Project Valhalla promises: “Codes like a class, runs like an int.” And it is really getting there. You define a value class or value record, such as public value record Point(double x, double y) { public Point moveBy(double dx, double dy) { return new Point(x + dx, y + dy); } } The JVM will try to represent it as a 16 byte value instead of a reference to a heap object with a header + 16 byte. Right now, that is not easy to observe. In my conference presentation, I had to use an internal API to create an array that could be flattened: Point[] path = (Point[]) ValueClass.newNullRestrictedNonAtomicArray(Point.class, NPOINTS, new Point(0, 0)); At some point in the future, you'll be able to write something like var path = new Point![NPOINTS]{ new Point(0, 0) } // Syntax may change The morning before the presentation, I stumbled upon an article by Dan Smith that made it all so easy. He makes a set of LocalDate, calls toArray, and processes the values. With Valhalla, the trivial benchmark is significantly faster. No internal API required. But it only worked with the “early access” release posted at https://jdk.java.net/valhalla/. When I built the Valhalla JDK from the Git repo, there was no improvement. I was very confused. The whole point of my presentation was “Trust, but verify”. So, following my own sage advice, I verified with jcmd DateTest GC.class_histogram | head And indeed, with the EA build, we can see flatteni...

First seen: 2025-11-17 05:57

Last seen: 2025-11-17 07:57