On Mon, 3 Jun 2024 18:00:35 GMT, Sean Gwizdak <d...@openjdk.org> wrote:

>> Improve the speed of Method.hashCode by caching the hashcode on first use. 
>> I've seen an application where Method.hashCode is a hot path, and this is a 
>> fairly simple speedup. The memory overhead is low. 
>> 
>> This addresses issue 
>> [JDK-8332249](https://bugs.openjdk.org/browse/JDK-8332249). 
>> 
>> Before:
>> 
>> Benchmark                         Mode  Cnt  Score   Error  Units
>> # Intel Skylake
>> MethodHashCode.benchmarkHashCode  avgt    5  1.843 ± 0.149  ns/op
>> # Arm Neoverse N1
>> MethodHashCode.benchmarkHashCode  avgt    5  2.363 ± 0.091  ns/op
>> 
>> 
>> 
>> After:
>> 
>> 
>> Benchmark                         Mode  Cnt  Score   Error  Units
>> # Intel Skylake
>> MethodHashCode.benchmarkHashCode  avgt    5  1.121 ± 1.189  ns/op
>> # Arm Neoverse N1
>> MethodHashCode.benchmarkHashCode  avgt    5  1.001 ± 0.001  ns/op
>
> Sean Gwizdak has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains six additional 
> commits since the last revision:
> 
>  - Remove trailing whitespace.
>  - Move hashCode benchmark into the newly created MethodBenchmark file
>  - Merge branch 'master' into method-hashcode-JDK-8332249
>  - Remove changes to JavaDoc per guidance.
>  - Fix whitespace issues pointed by the bot
>  - Micro-optimize Method.hashCode

As usual in these cases, we need to make a footprint argument as well. `Method` 
is a special class with lots of injected fields, so the only "true" source of 
layout information is `-XX:+PrintFieldLayout`. It tells me there is a 4-byte 
tail due to object alignment in compressed oops mode. This can accommodate a 
new `int` field. There seems to be no space when compressed oops are disabled.


# Out of the box, compressed oops enabled
Layout of class java/lang/reflect/Method
Instance fields:
 @0 12/- RESERVED
 @12 "override" Z 1/1 INHERITED
 @13 "callerSensitive" B 1/1 REGULAR
 @14 2/1 EMPTY
 @16 "accessCheckCache" Ljava/lang/Object; 4/4 INHERITED
 @20 "parameterData" Ljava/lang/reflect/Executable$ParameterData; 4/4 INHERITED
 @24 "declaredAnnotations" Ljava/util/Map; 4/4 INHERITED
 @28 "slot" I 4/4 REGULAR
 @32 "modifiers" I 4/4 REGULAR
 @36 "clazz" Ljava/lang/Class; 4/4 REGULAR
 @40 "name" Ljava/lang/String; 4/4 REGULAR
 @44 "returnType" Ljava/lang/Class; 4/4 REGULAR
 @48 "parameterTypes" [Ljava/lang/Class; 4/4 REGULAR
 @52 "exceptionTypes" [Ljava/lang/Class; 4/4 REGULAR
 @56 "signature" Ljava/lang/String; 4/4 REGULAR
 @60 "genericInfo" Lsun/reflect/generics/repository/MethodRepository; 4/4 
REGULAR
 @64 "annotations" [B 4/4 REGULAR
 @68 "parameterAnnotations" [B 4/4 REGULAR
 @72 "annotationDefault" [B 4/4 REGULAR
 @76 "methodAccessor" Ljdk/internal/reflect/MethodAccessor; 4/4 REGULAR
 @80 "root" Ljava/lang/reflect/Method; 4/4 REGULAR
Instance size = 88 bytes

# -XX:-UseCompressedOops
Layout of class java/lang/reflect/Method
Instance fields:
 @0 12/- RESERVED
 @12 "override" Z 1/1 INHERITED
 @13 "callerSensitive" B 1/1 REGULAR
 @14 2/1 EMPTY
 @16 "accessCheckCache" Ljava/lang/Object; 8/8 INHERITED
 @24 "parameterData" Ljava/lang/reflect/Executable$ParameterData; 8/8 INHERITED
 @32 "declaredAnnotations" Ljava/util/Map; 8/8 INHERITED
 @40 "slot" I 4/4 REGULAR
 @44 "modifiers" I 4/4 REGULAR
 @48 "clazz" Ljava/lang/Class; 8/8 REGULAR
 @56 "name" Ljava/lang/String; 8/8 REGULAR
 @64 "returnType" Ljava/lang/Class; 8/8 REGULAR
 @72 "parameterTypes" [Ljava/lang/Class; 8/8 REGULAR
 @80 "exceptionTypes" [Ljava/lang/Class; 8/8 REGULAR
 @88 "signature" Ljava/lang/String; 8/8 REGULAR
 @96 "genericInfo" Lsun/reflect/generics/repository/MethodRepository; 8/8 
REGULAR
 @104 "annotations" [B 8/8 REGULAR
 @112 "parameterAnnotations" [B 8/8 REGULAR
 @120 "annotationDefault" [B 8/8 REGULAR
 @128 "methodAccessor" Ljdk/internal/reflect/MethodAccessor; 8/8 REGULAR
 @136 "root" Ljava/lang/reflect/Method; 8/8 REGULAR
Instance size = 144 bytes

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

PR Comment: https://git.openjdk.org/jdk/pull/19433#issuecomment-2160170846

Reply via email to