Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8256167: Convert JDK use of Reference::get to Reference::refersTo #1609

Closed
wants to merge 15 commits into from

Conversation

mlchung
Copy link
Member

@mlchung mlchung commented Dec 3, 2020

This patch replaces some uses of Reference::get to Reference::refersTo to avoid keeping a referent strongly reachable that could cause unnecessary delay in collecting such object. I only made change in some but not all classes in core libraries when working with Kim on Reference::refersTo. The remaining uses are left for the component owners to convert at appropriate time.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8256167: Convert JDK use of Reference::get to Reference::refersTo

Reviewers

Download

$ git fetch https://git.openjdk.java.net/jdk pull/1609/head:pull/1609
$ git checkout pull/1609

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 3, 2020

👋 Welcome back mchung! 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 Dec 3, 2020

⚠️ @mlchung This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 3, 2020
@openjdk
Copy link

openjdk bot commented Dec 3, 2020

@mlchung The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-compiler
  • serviceability

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

@openjdk openjdk bot added serviceability serviceability-dev@openjdk.org hotspot-compiler hotspot-compiler-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Dec 3, 2020
@mlchung
Copy link
Member Author

mlchung commented Dec 3, 2020

/label remove hotspot-compiler

@openjdk openjdk bot removed the hotspot-compiler hotspot-compiler-dev@openjdk.org label Dec 3, 2020
@openjdk
Copy link

openjdk bot commented Dec 3, 2020

@mlchung
The hotspot-compiler label was successfully removed.

@mlbridge
Copy link

mlbridge bot commented Dec 3, 2020

Webrevs

Copy link
Contributor

@sspitsyn sspitsyn left a comment

Choose a reason for hiding this comment

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

Hi Mandy,
Looks good. I guess, you are going to update the copyright comments before the push.
Thanks,
Serguei

@openjdk
Copy link

openjdk bot commented Dec 4, 2020

@mlchung 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:

8256167: Convert JDK use of `Reference::get` to `Reference::refersTo`

Reviewed-by: sspitsyn, shade, dfuchs, alanb, kbarrett

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 6 new commits pushed to the master branch:

  • d3ac1bf: 8198390: Test MultiResolutionDrawImageWithTransformTest.java fails when -esa is passed
  • 51d325e: 8257633: Missing -mmacosx-version-min=X flag when linking libjvm
  • e27ea4d: 8257750: writeBuffer field of java.io.DataOutputStream should be final
  • dd0b945: 8257531: Super word not applied to a loop of simple Buffer operations
  • d76039d: 8257725: No throws of SSLHandshakeException
  • fcc8479: 8257724: Incorrect package of the linked class in BaseSSLSocketImpl

Please see this link for an up-to-date comparison between the source branch of this pull request and 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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 4, 2020
Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Replacements look fine to me.

// keeping a strong reference to the entry's referent
if (e.refersTo(key)) return true;

// then checks for equality
Copy link
Member

Choose a reason for hiding this comment

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

Obnoxiously minor nit: plurality is inconsistent. check if the given entry... above, and then check[s] for equality. Should be ...then check for equality?

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. Fixed this grammatical nit.

Copy link
Member

@dfuch dfuch left a comment

Choose a reason for hiding this comment

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

Hi Mandy,

This looks good to me. There are a few places where a single call to Reference::get is replaced by multiple calls to Reference::refersTo, allowing the reference to get cleared in between, but as far as I could see that doesn't affect the overall logic which is still sound.
So LGTM!

best regards,
-- daniel

Copy link
Contributor

@AlanBateman AlanBateman left a comment

Choose a reason for hiding this comment

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

Good to use this done in the same release as refersTo was added.


// then checks for equality
Object k = e.get();
return key == k || key.equals(k);

Choose a reason for hiding this comment

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

I think key == k is already covered by refersTo. But k could be null; checking for that here might be useful, to skip the call to equals in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point on checking k != null. A cleaner check:

        // then check for equality if the referent is not cleared
        Object k = e.get();
        return k != null || key.equals(k);

Copy link
Contributor

Choose a reason for hiding this comment

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

You meant: return k != null && key.equals(k); right?

Copy link
Member Author

Choose a reason for hiding this comment

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

oops...yes.

@SuppressWarnings("unchecked")
IdentityWeakReference<T> wr = (IdentityWeakReference<T>) o;
T got = get();
return got != null && wr.refersTo(got);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you could pre-screen the get() with:

if (this.refersTo(null) != wr.refersTo(null)) return false;

In case one of the two WeakReference(s) is cleared and the other is not, they are not equal and you don't call this.get() in such case.

Copy link
Member Author

Choose a reason for hiding this comment

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

expunge is called at get, put, and remove. While there is a chance that a referent might be cleared, I will leave this as is and this patch simply replaces to use refersTo.

Copy link
Contributor

@plevart plevart Dec 4, 2020

Choose a reason for hiding this comment

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

You're right. Where it would matter is in expunge() where the Map keys that get polled off the reference queue are all already cleared and when HashMap.remove(key) is called with such cleared key, it is this cleared key that is the target of key.equals(other) method invocation (and not vice versa), so it doesn't matter if .get() is called on such key as it is already cleared.

Copy link
Member

@kevinrushforth kevinrushforth left a comment

Choose a reason for hiding this comment

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

You have a typo that will cause a compilation error.

@@ -428,7 +434,7 @@ public boolean containsKey(Object key) {
Entry<K,V>[] tab = getTable();
int index = indexFor(h, tab.length);
Entry<K,V> e = tab[index];
while (e != null && !(e.hash == h && eq(k, e.get())))
while (e != null && !(e.hash == h && matchesKey(e, k))
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't compile, which is why the checks from the GitHub actions build failed.

Copy link
Member Author

@mlchung mlchung Dec 4, 2020

Choose a reason for hiding this comment

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

I caught that and it's in my local repo that I haven't pushed the commit. Sorry for not syncing my branch for review.

@mlchung
Copy link
Member Author

mlchung commented Dec 6, 2020

/integrate

@openjdk openjdk bot closed this Dec 6, 2020
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Dec 6, 2020
@openjdk
Copy link

openjdk bot commented Dec 6, 2020

@mlchung Since your change was applied there have been 8 commits pushed to the master branch:

  • 78be334: 8242332: Add SHA3 support to SunPKCS11 provider
  • c4339c3: 8243614: Typo in ReentrantLock's Javadoc
  • d3ac1bf: 8198390: Test MultiResolutionDrawImageWithTransformTest.java fails when -esa is passed
  • 51d325e: 8257633: Missing -mmacosx-version-min=X flag when linking libjvm
  • e27ea4d: 8257750: writeBuffer field of java.io.DataOutputStream should be final
  • dd0b945: 8257531: Super word not applied to a loop of simple Buffer operations
  • d76039d: 8257725: No throws of SSLHandshakeException
  • fcc8479: 8257724: Incorrect package of the linked class in BaseSSLSocketImpl

Your commit was automatically rebased without conflicts.

Pushed as commit 972bc3b.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@mlchung mlchung deleted the refersTo branch March 25, 2021 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated serviceability serviceability-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

8 participants