Skip to content

Commit 59ab328

Browse files
author
duke
committedJul 12, 2024
Automatic merge of jdk:master into master
2 parents cf425fd + 8ba9bc6 commit 59ab328

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed
 

‎src/java.base/share/classes/java/lang/reflect/Method.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public final class Method extends Executable {
9595
// If this branching structure would ever contain cycles, deadlocks can
9696
// occur in annotation code.
9797
private Method root;
98+
// Hash code of this object
99+
private int hash;
98100

99101
// Generics infrastructure
100102
private String getGenericSignature() {return signature;}
@@ -381,7 +383,13 @@ public boolean equals(Object obj) {
381383
* method's declaring class name and the method's name.
382384
*/
383385
public int hashCode() {
384-
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
386+
int hc = hash;
387+
388+
if (hc == 0) {
389+
hc = hash = getDeclaringClass().getName().hashCode() ^ getName()
390+
.hashCode();
391+
}
392+
return hc;
385393
}
386394

387395
/**

‎test/micro/org/openjdk/bench/java/lang/reflect/MethodBenchmark.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
import org.openjdk.jmh.annotations.Warmup;
3737

3838
/**
39-
* Benchmark measuring the speed of Method/Method.getExceptionTypes() and
40-
* getParameterTypes(), in cases where the result array is length zero.
39+
* Benchmark measuring the speed of Method/Method.getExceptionTypes(),
40+
* getParameterTypes() in cases where the result array is length zero,
41+
* and hashCode().
4142
*/
4243
@BenchmarkMode(Mode.AverageTime)
4344
@State(Scope.Benchmark)
@@ -50,6 +51,7 @@ public class MethodBenchmark {
5051
Method emptyParametersMethod;
5152
Method oneExceptionMethod;
5253
Method oneParameterMethod;
54+
Method hashCodeMethod;
5355

5456
public MethodBenchmark() {
5557
try {
@@ -58,6 +60,8 @@ public MethodBenchmark() {
5860

5961
emptyExceptionsMethod = emptyParametersMethod;
6062
oneExceptionMethod = oneParameterMethod;
63+
64+
hashCodeMethod = String.class.getDeclaredMethod("toString");
6165
} catch (Exception e) {
6266
throw new RuntimeException(e);
6367
}
@@ -82,4 +86,9 @@ public Object[] getParameterTypes() throws Exception {
8286
public Object[] getParameterTypesEmpty() throws Exception {
8387
return emptyParametersMethod.getParameterTypes();
8488
}
89+
90+
@Benchmark
91+
public int getMethodHashCode() {
92+
return hashCodeMethod.hashCode();
93+
}
8594
}

0 commit comments

Comments
 (0)
Please sign in to comment.