Create the following source file:
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
class Test {
private int compute() {
IntSupplier s1 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
IntSupplier s2 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
IntSupplier s3 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
return s1.getAsInt() + s2.getAsInt() + s3.getAsInt();
}
}
Compile it with using openjdk 17.0.1 2021-10-19, without debug info:
$ javac Test.java
Then observe that lambdas were deduplicated using javap -p:
$ javap -p Test.class
Compiled from "Test.java"
class Test {
Test();
private int compute();
private static int lambda$compute$1();
private static int lambda$compute$0(int);
}
There are only two lambda bodies, despite we have six in the source code. This is expected behavior (seeJDK-8200301).
Do the same with java 18 or later (java 22 is also affected). Now, the lambdas are not deduplicated:
$ javap -p Test.class
Compiled from "Test.java"
class Test {
Test();
private int compute();
private static int lambda$compute$5();
private static int lambda$compute$4(int);
private static int lambda$compute$3();
private static int lambda$compute$2(int);
private static int lambda$compute$1();
private static int lambda$compute$0(int);
}
This is an unexpected regression.
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
class Test {
private int compute() {
IntSupplier s1 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
IntSupplier s2 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
IntSupplier s3 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
return s1.getAsInt() + s2.getAsInt() + s3.getAsInt();
}
}
Compile it with using openjdk 17.0.1 2021-10-19, without debug info:
$ javac Test.java
Then observe that lambdas were deduplicated using javap -p:
$ javap -p Test.class
Compiled from "Test.java"
class Test {
Test();
private int compute();
private static int lambda$compute$1();
private static int lambda$compute$0(int);
}
There are only two lambda bodies, despite we have six in the source code. This is expected behavior (see
Do the same with java 18 or later (java 22 is also affected). Now, the lambdas are not deduplicated:
$ javap -p Test.class
Compiled from "Test.java"
class Test {
Test();
private int compute();
private static int lambda$compute$5();
private static int lambda$compute$4(int);
private static int lambda$compute$3();
private static int lambda$compute$2(int);
private static int lambda$compute$1();
private static int lambda$compute$0(int);
}
This is an unexpected regression.
- relates to
-
JDK-8275233 Incorrect line number reported in exception stack trace thrown from a lambda expression
- Resolved
-
JDK-8200301 deduplicate lambda methods
- Resolved