Classfile API performance and caching in Constant API

- liangchenblue at gmail.com
Fri Apr 21 17:01:45 UTC 2023


Sure. My patches for new Constant API and caching will not migrate
existing parts using ASM or other libraries to Classfile API, if that
is what you mean.

In addition, I have another patch that provides general improvements
to MethodTypeDesc https://github.com/openjdk/jdk/pull/13186 while
fixing its mutability bug,
and wishes to add two new APIs MethodTypeDesc.of(ClassDesc returnType)
and MethodTypeDesc.of(ClassDesc returnType, Collection<ClassDesc>
parameterTypes) as well.

So, in general, what I want to accomplish in the Constant API is these:
1. Add ClassDesc.internalName() which returns a string suitable for
CONSTANT_Class_info for better performance
2. Add caching to this internalName and MethodTypeDesc.descriptorString()
3. Add MethodTypeDesc.of(ClassDesc returnType) and
MethodTypeDesc.of(ClassDesc returnType, Collection<ClassDesc>
parameterTypes) for convenience
4. Fix the bug that MethodTypeDesc.of(ClassDesc, ClassDesc[]) can be
mutated by input array
5. Move the storage of parameters in MethodTypeDesc from ClassDesc[]
to List<ClassDesc> (using List.of immutable lists) to speed up
parameterList()
6. Optimize MethodTypeDesc.resolveConstantDesc() to resolve from class
descs (parsing raw descriptor strings take longer from my benchmarks)
and reject more-than-255-slot descs more eagerly

Should I submit separate patches, one for each?

Chen

On Fri, Apr 21, 2023 at 11:29 AM Brian Goetz <brian.goetz at oracle.com> wrote:
>
> Can you separate the pure JDK implementation improvements (e.g., making j.u.c more efficient) from the "switch to using classfile API in X" into separate patches?
>
> On 4/21/2023 12:23 PM, - wrote:
>
> If there is no objection, I will submit a patch incorporating the
> caching and internalName method to the Constant API, to make the
> Classfile API more efficient.
>
> Chen
>
> On Wed, Apr 19, 2023 at 11:15 PM - <liangchenblue at gmail.com> wrote:
>
> 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