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

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.