Skip to content

Conversation

@trevorkbond
Copy link

@trevorkbond trevorkbond commented Nov 21, 2025

Enhance ResourceParsingClassHierarchyResolver.getClassInfo to use ClassReaderImpl to improve performance. Previously this method inflated and stored all UTF-8 entries in the constant pool and later accessed the array of strings to try finding the name of the superclass. ClassReaderImpl instead stores constant pool offsets and later lazily reads/inflates UTF8 entries as needed.

I’ve ran all tier 1 tests and tests within test/jdk/jdk/classfile on the latest version of this change, and they all pass.

I ran some informal performance testing to see if these changes led to any improvement. I created a .class file with several thousand unique strings in a String array to deliberately enlarge the number of UTF-8 entries in the constant pool. I then benchmarked the performance of running a process nearly identical to ClassHierarchyInfoTest.testClassLoaderParsingResolver on this custom class over 200 runs using JMH. The results are as follows.

Version Avg Time (ns/op) Δ vs Before
Before 1,483,671.539 ± 3,477.744
After 1,380,064.517 ± 3,482.434 ≈ 7.0% faster

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8350938: ResourceParsingClassHierarchyResolver inflates all Utf8 CP entries (Enhancement - P5)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28458/head:pull/28458
$ git checkout pull/28458

Update a local copy of the PR:
$ git checkout pull/28458
$ git pull https://git.openjdk.org/jdk.git pull/28458/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28458

View PR using the GUI difftool:
$ git pr show -t 28458

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28458.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 21, 2025

👋 Welcome back trevorkbond! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Nov 21, 2025

@trevorkbond This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8350938: ResourceParsingClassHierarchyResolver inflates all Utf8 CP entries

Reviewed-by: liach

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 45 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@liach) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk
Copy link

openjdk bot commented Nov 21, 2025

@trevorkbond The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 21, 2025
@mlbridge
Copy link

mlbridge bot commented Nov 21, 2025

Webrevs

@dansmithcode
Copy link

I'm available as a sponsor when we get this reviewed

@ExE-Boss
Copy link

It might be better to make the previous version lazy, as the new version requires pre‑parsing the whole input, whereas the old version would only read the classfile header.

@jdlib
Copy link

jdlib commented Nov 22, 2025

The new version would always read all classfile bytes, the old one in chunks of 8192 bytes, both stop parsing at this_class position (with or without inflating constant pool entries).
Maybe the benchmark would not show much difference for typical classfiles (not having thousands of strings).
But in my eyes removing the logic to parse the constant pool and instead reuse ClassReader is a good thing.

@ExE-Boss
Copy link

Maybe ClassReader could be updated to have a version which lazily uses an InputStream.

Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in principle. Even though people wonder about the buffering policy, I think it is negligible considering that we have caching mechanism. And the constant pool is flexible sized, so "only read the classfile header" might be an optimisitic assumption.

int superIndex = in.readUnsignedShort();
var superClass = superIndex > 0 ? ClassDesc.ofInternalName(cpStrings[cpClasses[superIndex]]) : null;
try (ci) {
ClassReader reader = new ClassReaderImpl(ci.readAllBytes(), (ClassFileImpl) ClassFile.of());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ClassReader reader = new ClassReaderImpl(ci.readAllBytes(), (ClassFileImpl) ClassFile.of());
var reader = new ClassReaderImpl(ci.readAllBytes(), ClassFileImpl.DEFAULT_CONTEXT);

And you can remove the 3 imports you have added.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this. I adjusted that line and the imports accordingly. I still found a need to import java.lang.classfile.constantpool.ClassEntry for the line further down converting the ClassEntry to a ClassDesc. If that can also be avoided I'm happy to fix. I also removed a few imports previously needed but that can be removed with these changes.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 25, 2025
@liach
Copy link
Member

liach commented Nov 25, 2025

Also, the key to this patch is not performance - it is about the maintainability from a consistent way to parse class files, so in the future, we don't need to update this site any more if we have new constant pool entry type or other format expansions.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Nov 25, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 26, 2025
@trevorkbond
Copy link
Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Nov 26, 2025
@openjdk
Copy link

openjdk bot commented Nov 26, 2025

@trevorkbond
Your change (at version a990cd3) is now ready to be sponsored by a Committer.

@jaikiran
Copy link
Member

Hello Trevor, please update the copyright year on ClassHierarchyImpl.java from 2022, 2024, to 2022, 2025,.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs [email protected] ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored

Development

Successfully merging this pull request may close these issues.

6 participants