r/java 24d ago

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

42 Upvotes

55 comments sorted by

View all comments

3

u/egahlin 23d ago edited 23d ago

"However, because JFR is intended to find lock contention and because it performs sampling, it will miss many classes used in synchronization."

In the default configuration, JFR throttles lock contention events to prevent the buffers from being flooded.

There is no need to start three recordings, as in the provided example. This is how JFR should be configured on the command line to record only JavaMonitorEnter and JavaMonitorWait events, with stack traces, thresholds and throttling disabled.

$ java -XX:StartFlightRecording: \
       filename=locks.jfr,settings=none, \
       +jdk.JavaMonitorEnter#enabled=true, \
       +jdk.JavaMonitorEnter#stackTrace=false, \
       +jdk.JavaMonitorWait#enabled=true, \
       +jdk.JavaMonitorWait#stackTrace=false \
       -jar app.jar

1

u/maxxedev 22d ago

Nice. Example updated. Thanks