Commit 59ab328 duke
committed Jul 12, 2024
File tree 2 files changed +20
-3
lines changed
src/java.base/share/classes/java/lang/reflect
test/micro/org/openjdk/bench/java/lang/reflect
2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,8 @@ public final class Method extends Executable {
95
95
// If this branching structure would ever contain cycles, deadlocks can
96
96
// occur in annotation code.
97
97
private Method root ;
98
+ // Hash code of this object
99
+ private int hash ;
98
100
99
101
// Generics infrastructure
100
102
private String getGenericSignature () {return signature ;}
@@ -381,7 +383,13 @@ public boolean equals(Object obj) {
381
383
* method's declaring class name and the method's name.
382
384
*/
383
385
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 ;
385
393
}
386
394
387
395
/**
Original file line number Diff line number Diff line change 36
36
import org .openjdk .jmh .annotations .Warmup ;
37
37
38
38
/**
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().
41
42
*/
42
43
@ BenchmarkMode (Mode .AverageTime )
43
44
@ State (Scope .Benchmark )
@@ -50,6 +51,7 @@ public class MethodBenchmark {
50
51
Method emptyParametersMethod ;
51
52
Method oneExceptionMethod ;
52
53
Method oneParameterMethod ;
54
+ Method hashCodeMethod ;
53
55
54
56
public MethodBenchmark () {
55
57
try {
@@ -58,6 +60,8 @@ public MethodBenchmark() {
58
60
59
61
emptyExceptionsMethod = emptyParametersMethod ;
60
62
oneExceptionMethod = oneParameterMethod ;
63
+
64
+ hashCodeMethod = String .class .getDeclaredMethod ("toString" );
61
65
} catch (Exception e ) {
62
66
throw new RuntimeException (e );
63
67
}
@@ -82,4 +86,9 @@ public Object[] getParameterTypes() throws Exception {
82
86
public Object [] getParameterTypesEmpty () throws Exception {
83
87
return emptyParametersMethod .getParameterTypes ();
84
88
}
89
+
90
+ @ Benchmark
91
+ public int getMethodHashCode () {
92
+ return hashCodeMethod .hashCode ();
93
+ }
85
94
}
You can’t perform that action at this time.
0 commit comments