Skip to content

Cache PackageFragment.getElementName() to avoid repeated String creation#4934

Open
laeubi wants to merge 1 commit into
eclipse-jdt:masterfrom
laeubi:issue_pf_name
Open

Cache PackageFragment.getElementName() to avoid repeated String creation#4934
laeubi wants to merge 1 commit into
eclipse-jdt:masterfrom
laeubi:issue_pf_name

Conversation

@laeubi
Copy link
Copy Markdown
Contributor

@laeubi laeubi commented Mar 14, 2026

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

Copy link
Copy Markdown
Contributor

@fedejeanne fedejeanne left a comment

Choose a reason for hiding this comment

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

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.

@laeubi
Copy link
Copy Markdown
Contributor Author

laeubi commented Mar 17, 2026

Yep the data-model is a bit "wide open" at jdt so theoretically it could change (but hard to prove it does not).

@laeubi
Copy link
Copy Markdown
Contributor Author

laeubi commented Mar 22, 2026

@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PackageFragment.getElementName() creates a new String on every call

3 participants