Use String keys in UrlJarFiles cache to avoid HashMap.getNode type profile pollution - #51145
Closed
waterWang wants to merge 1 commit into
Closed
Use String keys in UrlJarFiles cache to avoid HashMap.getNode type profile pollution#51145waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
Replace Map<JarFileUrlKey, JarFile> with Map<String, JarFile> in the loader cache. The custom JarFileUrlKey value object was allocated on every cache lookup, polluting the JVM-wide shared HashMap.getNode C2 type profile. This caused the method to go megamorphic, deoptimizing unrelated application HashMap.get() calls on hot code paths. The fix adds JarFileUrlKey.toKeyString() which produces a unique string representation without triggering DNS lookups, and changes the underlying cache map to use String keys. The DNS-avoidance behavior is preserved. See spring-projectsgh-51141. Closes spring-projectsgh-51141.
Member
|
Thanks for the PR but I am not quite sure that the benchmark in the related issue holds. This change ads adds string allocation overhead and degrades equality semantics so we wouldn't pursue either way. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This commit fixes the performance regression described in #51141.
Problem
Commit 08cc62a changed the loader cache from
Map<String, JarFile>toMap<JarFileUrlKey, JarFile>. A newJarFileUrlKeyvalue object is allocated on every cache lookup, polluting the JVM-wide sharedHashMap.getNodeC2 type profile. This causes the method to go megamorphic, so C2 stops devirtualizing/inliningequals()/hashCode()— degrading unrelated applicationHashMap<String,...>/HashMap<Integer,...>lookups on hot code paths.Before the commit the cache key was a
String(the same type applications commonly use), so the shared profile stayed mono/bimorphic.Fix
JarFileUrlKey.toKeyString()— produces a unique string representation without triggering DNS lookups (preserves the original DNS-avoidance behavior)Cachemap fromMap<JarFileUrlKey, JarFile>toMap<String, JarFile>new JarFileUrlKey(url).toKeyString()as the keySince
HashMap.getNodeis now called withStringkeys, the shared type profile is no longer polluted by the custom key type, and applicationHashMap.get()calls remain optimized.Verification
The
JarFileUrlKeyTeststest class covers the key equality semantics. Cache behavior is unchanged — the sameJarFileUrlKeyfields are used to derive the key string, so cache hit/miss semantics are identical.Closes #51141