Classfile API performance and caching in Constant API

- liangchenblue at gmail.com
Thu Apr 20 04:15:06 UTC 2023


Hello,
Since Adam has revealed that Classfile API is somewhat hampered by
performance issues and added a few patches in
https://github.com/openjdk/jdk/pull/12945/ that performs caching,

His patch focuses on two optimizations:
1. Speed up construction of descriptor string for MethodTypeDesc
2. Caching of ClassDesc symbol in ClassEntry (representing CONSTANT_Class_info)

Inspired by and in addition to his patches, I decided to try and see
the extent of performance impact of repeated string computation and
allocations from:
1. Creation of internal names from ClassDesc
    I added a temporary API, String internalName(), to ClassDesc. This
API is forward-compatible with Valhalla, that it returns a String
suitable for a CONSTANT_Class_info.
2. Repeated descriptorString calls on MethodTypeDesc
    I added a simple caching logic, much like the existing one in
java.lang.invoke.MethodType.
In addition, if the internal name and descriptor string are already
available via factory methods, the cache fields are immediately
assigned as well.

The results are promising: In jdk.classfile.Write benchmark
https://github.com/openjdk/jdk/blob/master/test/micro/org/openjdk/bench/jdk/classfile/Write.java,
The throughput with Classfile API increased by 1/3.

Master (Patch of https://bugs.openjdk.org/browse/JDK-8305669, since
otherwise cannot run JMH with make on Windows):
Benchmark                Mode  Cnt      Score     Error  Units
Write.asmStream         thrpt    5  44866.172 ± 144.049  ops/s
Write.jdkTree           thrpt    5  16282.041 ±  95.782  ops/s
Write.jdkTreePrimitive  thrpt    5  16352.972 ± 280.751  ops/s

New (https://github.com/liachmodded/jdk/commit/e33261ad197c33836d56b4b48acabc04880a780b)
Benchmark                Mode  Cnt      Score     Error  Units
Write.asmStream         thrpt    5  45695.225 ± 649.249  ops/s
Write.jdkTree           thrpt    5  22786.872 ± 256.329  ops/s
Write.jdkTreePrimitive  thrpt    5  22437.085 ± 397.090  ops/s

As a result, I believe it may be worth to update the Constant API to
offer ClassDesc::internalName() and method type descriptor string
caching, to enable better performance in the Classfile API.

Thanks for reading,
Chen Liang


More information about the classfile-api-dev mailing list