Integrated: 8332586: Avoid cloning empty arrays in java.lang.reflect.{Method,Constructor}

jengebr duke at openjdk.org
Mon Jun 3 13:41:14 UTC 2024


On Tue, 21 May 2024 13:49:18 GMT, jengebr <duke at openjdk.org> wrote:

> Improve `java/lang/reflect/Method.java`  by eliminating needless cloning of Class[0] instances.  This cloning is intended to prevent callers from changing array contents, but many Methods have zero exceptions or zero parameters, and returning the original `Class[0]` is sufficient.
> 
> Results from the included JMH benchmark:
> Before:
> 
> Benchmark                                    Mode  Cnt  Score   Error  Units
> ConstructorBenchmark.getExceptionTypes       avgt    5  6.526 ± 0.183  ns/op
> ConstructorBenchmark.getExceptionTypesEmpty  avgt    5  5.803 ± 0.073  ns/op
> ConstructorBenchmark.getParameterTypes       avgt    5  6.521 ± 0.188  ns/op
> ConstructorBenchmark.getParameterTypesEmpty  avgt    5  5.747 ± 0.087  ns/op
> MethodBenchmark.getExceptionTypes            avgt    5  6.525 ± 0.163  ns/op
> MethodBenchmark.getExceptionTypesEmpty       avgt    5  5.783 ± 0.130  ns/op
> MethodBenchmark.getParameterTypes            avgt    5  6.518 ± 0.195  ns/op
> MethodBenchmark.getParameterTypesEmpty       avgt    5  5.742 ± 0.028  ns/op
> 
> 
> After:
> 
> Benchmark                                    Mode  Cnt  Score   Error  Units
> ConstructorBenchmark.getExceptionTypes       avgt    5  6.590 ± 0.124  ns/op
> ConstructorBenchmark.getExceptionTypesEmpty  avgt    5  1.351 ± 0.061  ns/op
> ConstructorBenchmark.getParameterTypes       avgt    5  6.651 ± 0.132  ns/op
> ConstructorBenchmark.getParameterTypesEmpty  avgt    5  1.353 ± 0.150  ns/op
> MethodBenchmark.getExceptionTypes            avgt    5  6.701 ± 0.151  ns/op
> MethodBenchmark.getExceptionTypesEmpty       avgt    5  1.422 ± 0.025  ns/op
> MethodBenchmark.getParameterTypes            avgt    5  6.629 ± 0.142  ns/op
> MethodBenchmark.getParameterTypesEmpty       avgt    5  1.273 ± 0.169  ns/op

This pull request has now been integrated.

Changeset: 27af19d9
Author:    John Engebretson <jengebr at amazon.com>
Committer: Aleksey Shipilev <shade at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/27af19d921a5cf15f5146471b58961815690b4f2
Stats:     177 lines in 4 files changed: 170 ins; 1 del; 6 mod

8332586: Avoid cloning empty arrays in java.lang.reflect.{Method,Constructor}

Reviewed-by: shade, rriggs, liach

-------------

PR: https://git.openjdk.org/jdk/pull/19327


More information about the core-libs-dev mailing list