r/java • • Aug 18 '26

AutoValhalla: automatically turn your plain classes and records into value classes!

Automatically turn your plain classes and records into value classes!

Add @AutoValhalla annotation to classes or records in your JDK1.5+ codebase:

@AutoValhalla
public final class Point {        // class must be final
    public final int x;           // with final instance fields
    public final int y;
    public Point(int x, int y) { 
        this.x = x; 
        this.y = y; 
    }
}

@AutoValhalla
public record Currency(String code) { }  // or a record class

When your app is run on Valhalla-enabled JVM with auto-valhalla javaagent, these classes are automatically turned into value classes.

More info: https://github.com/thunkware/auto-valhalla/tree/auto-valhalla-0.1.0

edit: there is now a maven plugin for converting at build time. https://github.com/thunkware/auto-valhalla/tree/auto-valhalla-0.2.0

40 Upvotes

54 comments sorted by

View all comments

Show parent comment

5

u/maxxedev Aug 18 '26 edited Aug 18 '26

Yes, value classes aren't a performance optimization. There will be other behavioral changes like ==, synchronized, WeakReference, etc (these are noted in the README as well).

But most "model" or "dto" classes won't be impacted by these behavioral changes? Perhaps library authors can choose to (dangerously) opt-in to Valhalla benefits while retaining compatibility with older JDKs but without having to adopt tip & tail or multi-release JARs.

10

u/pron98 Aug 18 '26 edited Aug 18 '26
  1. Adopting tip & tail is less work than using something like this (or any of the work required to adopt one-size-fits-all).
  2. Maybe most classes won't be affected, but if some are you're in trouble.
  3. I think some people are under the impression that just turning lots of classes into value classes will somehow make their application faster or something, and I'm afraid they're bound to be disappointed. Value types can help a lot if the hot path of your application suffers from many cache misses on array accesses. That was indeed a case where Java lagged behind C++. But if your program's profile isn't dominated by this problem, I'm not sure what mechanism of value types people think will help their performance. Like virtual threads, value types can help performance if you know what the problem is and how to use the feature to solve it. It's not some "go faster" pixie dust that you sprinkle to make things better.

5

u/maxxedev Aug 19 '26

It's not some "go faster" pixie dust that you sprinkle to make things better.

Very much agreed.

Btw, thanks for all your insightful comments on reddit. I always learn something new from you.

4

u/LegitimateEntrance72 Aug 18 '26

IMO tip & tail makes lot of sense.

As an example, you have your library version 1.0.0 that requires JDK17 because of record classes. Now you want to convert your record classes to value records, but "cant do that" because it would mean raising minimum required JDK to 29 or so... but adopting tip and tail means that 1.0.0 stays on JDK 17+ and wont get new features, while 2.0.0 will require JDK29+ and get new features.

1.0.0 can always be patched if needed, IMO its not asking too much to say "if you want latest features, you gotta be on the latest major release"