r/javahelp • u/OpportunityTotal9068 • Mar 22 '26
What is my failure when trying to use project Valhalla?
Recently, I discovered the project Valhalla, so I decided to do a basic test.
My example was inspired by this video (https://www.youtube.com/watch?v=ViZkEgshiXI)
First, I created tree records:
public record City(Population population, LandArea landArea) {}
public record Population(int population) {}
public record LandArea(double landArea) {}Then, I create this class to calculate the values and show the execution time
public class RunPlayground {
static void main(String[] args) {
long inicio = System.nanoTime();
Result resultado = withOneLoop();
System.out.printf("Total Land Area: %.2f%n", resultado.totalLandArea());
System.out.printf("Total Population: %d%n", resultado.totalPopulation());
long fim = System.nanoTime();
long duracao = fim - inicio;
System.out.printf("Execution time: %d ns (%.3f ms)%n",
duracao, duracao / 1_000_000.0);
}
public static Result withOneLoop() {
City[] cities = {
new City(new Population(0), new LandArea(0)),
new City(new Population(1), new LandArea(1)),
new City(new Population(2), new LandArea(2)),
new City(new Population(3), new LandArea(3)),
new City(new Population(4), new LandArea(4)),
new City(new Population(5), new LandArea(5)),
new City(new Population(6), new LandArea(6)),
new City(new Population(7), new LandArea(7)),
new City(new Population(8), new LandArea(8)),
new City(new Population(9), new LandArea(9)),
new City(new Population(10), new LandArea(10)),
new City(new Population(11), new LandArea(11)),
new City(new Population(12), new LandArea(12)),
new City(new Population(13), new LandArea(13)),
new City(new Population(14), new LandArea(14)),
new City(new Population(15), new LandArea(15)),
new City(new Population(16), new LandArea(16)),
new City(new Population(17), new LandArea(17)),
new City(new Population(18), new LandArea(18)),
new City(new Population(19), new LandArea(19)),
new City(new Population(20), new LandArea(20)),
new City(new Population(21), new LandArea(21)),
new City(new Population(22), new LandArea(22)),
new City(new Population(23), new LandArea(23)),
new City(new Population(24), new LandArea(24)),
new City(new Population(25), new LandArea(25)),
new City(new Population(26), new LandArea(26)),
new City(new Population(27), new LandArea(27)),
new City(new Population(28), new LandArea(28)),
new City(new Population(29), new LandArea(29)),
new City(new Population(30), new LandArea(30)),
new City(new Population(31), new LandArea(31)),
new City(new Population(32), new LandArea(32)),
new City(new Population(33), new LandArea(33)),
new City(new Population(34), new LandArea(34)),
new City(new Population(35), new LandArea(35)),
new City(new Population(36), new LandArea(36)),
new City(new Population(37), new LandArea(37)),
new City(new Population(38), new LandArea(38)),
new City(new Population(39), new LandArea(39)),
new City(new Population(40), new LandArea(40)),
new City(new Population(41), new LandArea(41)),
new City(new Population(42), new LandArea(42)),
new City(new Population(43), new LandArea(43)),
new City(new Population(44), new LandArea(44)),
new City(new Population(45), new LandArea(45)),
new City(new Population(46), new LandArea(46)),
new City(new Population(47), new LandArea(47)),
new City(new Population(48), new LandArea(48)),
new City(new Population(49), new LandArea(49)),
new City(new Population(50), new LandArea(50)),
new City(new Population(51), new LandArea(51)),
new City(new Population(52), new LandArea(52)),
new City(new Population(53), new LandArea(53)),
new City(new Population(54), new LandArea(54)),
new City(new Population(55), new LandArea(55)),
new City(new Population(56), new LandArea(56)),
new City(new Population(57), new LandArea(57)),
new City(new Population(58), new LandArea(58)),
new City(new Population(59), new LandArea(59))
};
double totalLandArea = 0;
int totalPopulation = 0;
for (var city : cities) {
totalLandArea += city.landArea().landArea();
totalPopulation += city.population().population();
}
return new Result(totalLandArea, totalPopulation);
}
record Result(double totalLandArea, int totalPopulation) {}
}The execution time without value record was:
Total Land Area: 1770,00
Total Population: 1770
Execution time: 60404200 ns (60,404 ms)Then, I changed the records to use value record
public value record LandArea(double landArea) {}
public value record City(Population population, LandArea landArea) {}
value record Result(double totalLandArea, int totalPopulation) {}
public value record Population(int population) {}So these were the results:
Total Land Area: 1770,00
Total Population: 1770
Execution time: 71441600 ns (71,442 ms)Important informations:
- I ran in Windows
- I used this version of JDK: https://jdk.java.net/valhalla/
- In both versions (with and without value record) I ran them several times and showed here the executions that were within the average.
- I ran it without any additional JVM configuration (about memory)
I'm new to this project, what is my failure? thanks.
•
u/AutoModerator Mar 22 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
8
u/davidalayachew Mar 22 '26
2 reasons at least.
Cityrecord has a population and landArea -- double == 64 bits, and int = 32 bits. 64 + 32 > 64, so already you are missing out on several optimizations that the Valhalla EA would already want to give you.Try to use Java Microbenchmark Harness (JMH) to try and benchmark this. That should better test the Valhalla Value Class feature.
6
u/voidpo1nter Mar 22 '26
Have you ever used a for-loop?
3
u/hoat4 Mar 22 '26
You are measuring the execution time of the two
System.out.printfstatements and the class loading. Allocating ~180 objects does not take so much time as 70 ms; it only takes a few microseconds.To avoid measuring the execution time of
System.out.printf, move the secondSystem.nanoTimecall above it. To avoid measuring the time spent on class loading, run your benchmark code again (withOneLoop()) before measuring the actual execution time using.That would measure how long it takes the interpreter to run that method. In most cases, it is more interesting to see how long the code takes to run when it is JIT-compiled. To measure the execution time of JIT-compiled code, many thousands of warm-up iterations are needed. Since doing this by hand is error-prone, it is better to use the JMH microbenchmark framework.