Cache PackageFragment.getElementName() to avoid repeated String creation#4934
Open
laeubi wants to merge 1 commit into
Open
Cache PackageFragment.getElementName() to avoid repeated String creation#4934laeubi wants to merge 1 commit into
laeubi wants to merge 1 commit into
Conversation
fedejeanne
reviewed
Mar 17, 2026
Contributor
fedejeanne
left a comment
There was a problem hiding this comment.
Given that names is public and there is the possibility that outside callers modify it after it has been provided to the constructor of this class, I think this approach (calculating the value in the getter) is the safest choice.
If the field would have been private and a defensive copy of the parameter would have been used instead then one could have simply calculated the (cached)elementName in the constructor.
Contributor
Author
|
Yep the data-model is a bit "wide open" at jdt so theoretically it could change (but hard to prove it does not). |
Contributor
Author
|
@stephan-herrmann @iloveeclipse can someone review and probably merge? |
PackageFragment.getElementName() was creating a new String via Util.concatWith() on every invocation, despite the names array being immutable after construction. This adds a volatile cached field using the racy single-check idiom (safe for immutable String values). Fix eclipse-jdt#4930 Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
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.
PackageFragment.getElementName() was creating a new String via Util.concatWith() on every invocation, despite the names array being immutable after construction. This adds a volatile cached field using the racy single-check idiom (safe for immutable String values).
Fix #4930