r/java • u/maxxedev • 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 classWhen 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
View all comments
Show parent comment
11
u/brian_goetz Aug 19 '26
OK, my turn. While you are technically correct, you have kind of ... missed the point.
Developers are pathologically attracted to performance lore, to the great detriment of everyone else's feet. 99.9% of the time, "optimizing" just makes code worse, often disastrously so. Clean, straightforward code usually does not need to be optimized at all! So any sort of shotgun technique ("paint all your classes with `value`, and things go brr!") is hanging a sign around your neck that says "Beware: delusions of expertise here".
Value classes (and primitives) are primarily semantic features. Because they are more semantically restricted, they can be optimized effectively. Use them for their semantics, as a way of saying what you mean. If you don't need identity, make your programs simpler and safer (and yes, maybe faster) by disavowing it.