diff --git a/src/java.base/share/classes/java/lang/reflect/Executable.java b/src/java.base/share/classes/java/lang/reflect/Executable.java index 2f6f6cca89f04..a22d0fa80768b 100644 --- a/src/java.base/share/classes/java/lang/reflect/Executable.java +++ b/src/java.base/share/classes/java/lang/reflect/Executable.java @@ -70,6 +70,10 @@ public abstract sealed class Executable extends AccessibleObject abstract ConstructorRepository getGenericInfo(); boolean equalParamTypes(Class[] params1, Class[] params2) { + // The parameter arrays are trusted and the same for a root and all leaf + // copies. Thus, == on arrays is more useful than == on Executable. + if (params1 == params2) + return true; /* Avoid unnecessary cloning */ if (params1.length == params2.length) { for (int i = 0; i < params1.length; i++) { diff --git a/test/micro/org/openjdk/bench/java/lang/reflect/ExecutableCompareBenchmark.java b/test/micro/org/openjdk/bench/java/lang/reflect/ExecutableCompareBenchmark.java new file mode 100644 index 0000000000000..1413938dbd54e --- /dev/null +++ b/test/micro/org/openjdk/bench/java/lang/reflect/ExecutableCompareBenchmark.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.bench.java.lang.reflect; + +import java.lang.reflect.Method; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +/** + * Benchmark measuring the speed of Method and Constructor comparison. + */ +@BenchmarkMode(Mode.AverageTime) +@State(Scope.Benchmark) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Fork(1) +@Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +public class ExecutableCompareBenchmark { + Method appendRange; + Method appendRangeDup; + Method appendFull; + + public ExecutableCompareBenchmark() { + try { + appendRange = Appendable.class.getMethod("append", CharSequence.class, int.class, int.class); + appendRangeDup = Appendable.class.getMethod("append", CharSequence.class, int.class, int.class); + if (appendRange == appendRangeDup || !appendRange.equals(appendRangeDup)) { + throw new IllegalStateException(); + } + appendFull = Appendable.class.getMethod("append", CharSequence.class); + } catch (ReflectiveOperationException ex) { + throw new RuntimeException(ex); + } + } + + // Comparing identical Method objects. + @Benchmark + public boolean sameMethodObject() { + return appendRange.equals(appendRange); + } + + // Comparing equal Method objects with potentially different access + // suppression status. + @Benchmark + public boolean equalMethods() { + return appendRange.equals(appendRangeDup); + } + + // Comparing Method objects with only different parameter types; + // The declaring class, name, and the return type are the same. + @Benchmark + public boolean distinctParams() { + return appendRange.equals(appendFull); + } +} \ No newline at end of file