RFR: 8338532: Speed up the ClassFile API MethodTypeDesc#ofDescriptor

Shaojin Wen duke at openjdk.org
Mon Aug 19 06:32:23 UTC 2024


On Sun, 18 Aug 2024 00:15:57 GMT, Chen Liang <liach at openjdk.org> wrote:

>> The following are the performance numbers on multiple platforms. When the number of parameters is > 8, the performance will regress by about 33%. When the number of parameters is <= 8, the performance will be significantly improved or neutral.
>> 
>> ## 1. Benchmark script
>> 
>> # baseline
>> git checkout 74066bcca82749722e6fee57469520d418bf3430
>> make test TEST="micro:java.lang.constant.MethodTypeDescFactories.ofDescriptor"
>> 
>> # current
>> git checkout c02d2306935d99685e34ef960aa72e10feb82a39
>> make test TEST="micro:java.lang.constant.MethodTypeDescFactories.ofDescriptor"
>> 
>> 
>> ## 2. Performance numbers
>> 
>> ### 2.1 Mac Book M1 Pro
>> 
>> -# baseline
>> -Benchmark                                                         (descString)  Mode  Cnt     Score     Error  Units
>> -MethodTypeDescFactories.ofDescriptor   (Ljava/lang/Object;Ljava/lang/String;)I  avgt    6   104.154 ?   2.636  ns/op
>> -MethodTypeDescFactories.ofDescriptor                                       ()V  avgt    6     3.843 ?   0.014  ns/op
>> -MethodTypeDescFactories.ofDescriptor  ([IJLjava/lang/String;Z)Ljava/util/List;  avgt    6   221.013 ?  81.200  ns/op
>> -MethodTypeDescFactories.ofDescriptor                     ()[Ljava/lang/String;  avgt    6    20.603 ?   0.026  ns/op
>> -MethodTypeDescFactories.ofDescriptor                                  (..IIJ)V  avgt    6   221.404 ? 189.754  ns/op
>> -MethodTypeDescFactories.ofDescriptor                  (.....................).  avgt    6  1128.418 ?  16.850  ns/op
>> 
>> +# current
>> +Benchmark                                                         (descString)  Mode  Cnt     Score    Error  Units
>> +MethodTypeDescFactories.ofDescriptor   (Ljava/lang/Object;Ljava/lang/String;)I  avgt    6    53.900 ?  0.061  ns/op
>> +MethodTypeDescFactories.ofDescriptor                                       ()V  avgt    6     3.609 ?  0.017  ns/op
>> +MethodTypeDescFactories.ofDescriptor  ([IJLjava/lang/String;Z)Ljava/util/List;  avgt    6    69.666 ?  0.596  ns/op
>> +MethodTypeDescFactories.ofDescriptor                     ()[Ljava/lang/String;  avgt    6    20.978 ?  0.197  ns/op
>> +MethodTypeDescFactories.ofDescriptor                                  (..IIJ)V  avgt    6   126.968 ?  0.715  ns/op
>> +MethodTypeDescFactories.ofDescriptor                  (.....................).  avgt    6  1651.624 ? 30.462  ns/op
>> 
>> 
>> | descString | baseline  | current | delta |
>> | --- | --- | --- | --- |
>> | (Ljava/lang/Object;Ljava/lang/String;)I | 104.154 | 53.900 | 93.24%...
>
> @wenshao Do you think we should resort back to allocating an `ArrayList` if our long cannot encode the descriptor's offset info? I think you can add a case of 10 args like `([III.Z[B..[.[B)` and see if your patch improves over the baseline. You can add this case to `MethodTypeDescFactories` too.
> 
> <details>
> <summary>
> Only look at this if your approach is still slower than ArrayList allocation in my given simpler case.
> </summary>
> My proposal for `paramTypes` is like:
> 
>  - Fast return if empty
>  - Fast loop to fill up `lengths`
>    - Break if we encounter the 9th params or a long field descriptor
>  - At break, if `cur == len`, means fast path works, we go through fast path
>  - Else, we fall back to the old path of using `ArrayList`; add elements collected in `lengths` and the element between the last of `lengths` and cur (so it's either 9th element or too long)
> 
> This approach completely avoids a 2nd scan.
> </details>

According to @liach s suggestion, the performance has improved in scenarios where the number of parameters is greater than 8.

## 1. Benchmark script

git remote add wenshao git at github.com:wenshao/jdk.git
git fetch wenshao

# baseline
git checkout 1e17a42eda73bc5138e05502a004a8c293a93eb8
make test TEST="micro:java.lang.constant.MethodTypeDescFactories.ofDescriptor"

# current
git checkout c4fe4a1bf0895661c5d3f2d8cef06760e5605ec9
make test TEST="micro:java.lang.constant.MethodTypeDescFactories.ofDescriptor"


## 2. Performance numbers

### 2.1 Mac Book M1 Pro

-# baseline
-Benchmark                                                         (descString)  Mode  Cnt     Score     Error  Units
-MethodTypeDescFactories.ofDescriptor   (Ljava/lang/Object;Ljava/lang/String;)I  avgt    6    98.402 ?   5.075  ns/op
-MethodTypeDescFactories.ofDescriptor                                       ()V  avgt    6     3.884 ?   0.094  ns/op
-MethodTypeDescFactories.ofDescriptor  ([IJLjava/lang/String;Z)Ljava/util/List;  avgt    6   222.884 ?  42.684  ns/op
-MethodTypeDescFactories.ofDescriptor                     ()[Ljava/lang/String;  avgt    6    25.398 ?   0.154  ns/op
-MethodTypeDescFactories.ofDescriptor                                  (..IIJ)V  avgt    6   232.646 ? 153.202  ns/op
-MethodTypeDescFactories.ofDescriptor                         ([III.Z[B..[.[B).  avgt    6   338.739 ?   0.784  ns/op
-MethodTypeDescFactories.ofDescriptor                  (.....................).  avgt    6  1105.693 ?   2.729  ns/op

+# current
+Benchmark                                                         (descString)  Mode  Cnt     Score    Error  Units
+MethodTypeDescFactories.ofDescriptor   (Ljava/lang/Object;Ljava/lang/String;)I  avgt    6    55.584 ?  0.254  ns/op
+MethodTypeDescFactories.ofDescriptor                                       ()V  avgt    6     3.596 ?  0.014  ns/op
+MethodTypeDescFactories.ofDescriptor  ([IJLjava/lang/String;Z)Ljava/util/List;  avgt    6    68.550 ?  0.658  ns/op
+MethodTypeDescFactories.ofDescriptor                     ()[Ljava/lang/String;  avgt    6    20.974 ?  0.191  ns/op
+MethodTypeDescFactories.ofDescriptor                                  (..IIJ)V  avgt    6   120.125 ?  0.983  ns/op
+MethodTypeDescFactories.ofDescriptor                         ([III.Z[B..[.[B).  avgt    6   373.592 ? 16.552  ns/op
+MethodTypeDescFactories.ofDescriptor                  (.....................).  avgt    6  1166.071 ?  4.203  ns/op


| descString | baseline  | current | delta |
| --- | --- | --- | --- |
| (Ljava/lang/Object;Ljava/lang/String;)I | 98.402 | 55.584 | 77.03% |
| ()V | 3.884 | 3.596 | 8.01% |
| ([IJLjava/lang/String;Z)Ljava/util/List; | 222.884 | 68.550 | 225.14% |
| ()[Ljava/lang/String; | 25.398 | 20.974 | 21.09% |
| (..IIJ)V | 232.646 | 120.125 | 93.67% |
| ([III.Z[B..[.[B). | 338.739 | 373.592 | -9.33% |
| (.....................). | 1105.693 | 1166.071 | -5.18% |

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

PR Comment: https://git.openjdk.org/jdk/pull/20611#issuecomment-2295120437


More information about the core-libs-dev mailing list