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

8273329: Remove redundant null check from String.getBytes(String charsetName) #5361

Closed
wants to merge 1 commit into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Sep 3, 2021

Current implementation looks like this:

public byte[] getBytes(String charsetName)
        throws UnsupportedEncodingException {
    if (charsetName == null) throw new NullPointerException();
    return encode(lookupCharset(charsetName), coder(), value);
}

Null check seems to be redundant here because the same check of charsetName is done within String.lookupCharset(String):

private static Charset lookupCharset(String csn) throws UnsupportedEncodingException {
    Objects.requireNonNull(csn);
    try {
        return Charset.forName(csn);
    } catch (UnsupportedCharsetException | IllegalCharsetNameException x) {
        throw new UnsupportedEncodingException(csn);
    }
}

Progress

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

Issue

  • JDK-8273329: Remove redundant null check from String.getBytes(String charsetName)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/5361/head:pull/5361
$ git checkout pull/5361

Update a local copy of the PR:
$ git checkout pull/5361
$ git pull https://git.openjdk.java.net/jdk pull/5361/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 5361

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/5361.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 3, 2021

👋 Welcome back stsypanov! 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 openjdk bot added the rfr label Sep 3, 2021
@openjdk
Copy link

openjdk bot commented Sep 3, 2021

@stsypanov 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 core-libs label Sep 3, 2021
@mlbridge
Copy link

mlbridge bot commented Sep 3, 2021

Webrevs

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Redundant null checks get collapsed by HotSpot, so not a performance improvement.
Having null checks at public entry points also shows a stack trace that is more specific about where the null came from. So not much value in changing this.

@openjdk
Copy link

openjdk bot commented Sep 3, 2021

⚠️ @stsypanov the full name on your profile does not match the author name in this pull requests' HEAD commit. If this pull request gets integrated then the author name from this pull requests' HEAD commit will be used for the resulting commit. If you wish to push a new commit with a different author name, then please run the following commands in a local repository of your personal fork:

$ git checkout 8273329
$ git commit -c user.name='Preferred Full Name' --allow-empty -m 'Update full name'
$ git push

@openjdk
Copy link

openjdk bot commented Sep 3, 2021

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

8273329: Remove redundant null check from String.getBytes(String charsetName)

Reviewed-by: rriggs, iris, naoto

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

  • cec6c06: 8272232: javax/swing/JTable/4275046/bug4275046.java failed with "Expected value in the cell: 'rededited' but found 'redEDITED'."
  • 14a3ac0: 8271911: replay compilations of methods which use JSR292 (easy cases)
  • d414a88: 8273240: Dynamic test ArchiveConsistency.java should use CDSArchiveUtils
  • 23fa0dc: 8272905: Consolidate discovered lists processing
  • ff4018b: 8268148: unchecked warnings handle ? and ? extends Object differently
  • 8c37909: 8273234: extended 'for' with expression of type tvar causes the compiler to crash
  • 28ba78e: 8244675: assert(IncrementalInline || (_late_inlines.length() == 0 && !has_mh_late_inlines()))
  • d05494f: 8266239: Some duplicated javac command-line options have repeated effect
  • 93eec9a: 8272776: NullPointerException not reported
  • 7b023a3: 8273257: jshell doesn't compile a sealed hierarchy with a sealed interface and a non-sealed leaf
  • ... and 1 more: https://git.openjdk.java.net/jdk/compare/fa9c8657dfaa2018f501de1a8aa62f004342c245...master

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 (@RogerRiggs, @irisclark, @naotoj) 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 openjdk bot added the ready label Sep 3, 2021
@mrserb
Copy link
Member

mrserb commented Sep 3, 2021

In such cases when the specific exception throwing is removed from the method because it can be produced by some other used method, the test might be useful. So if the code in the method will be changed, or the usage of other method will be removed the exception still be thrown. Probably such test exists already, then just point to it here.

@stsypanov
Copy link
Contributor Author

stsypanov commented Sep 3, 2021

@mrserb you are right, there's such test, see /test/jdk/java/lang/String/Exceptions.getBytes(), line 384.

@mrserb
Copy link
Member

mrserb commented Sep 3, 2021

@mrserb you are right, there's such test, see /test/jdk/java/lang/String/Exceptions.getBytes(), line 384.

Thank you for confirmation, looks fine.

@stsypanov
Copy link
Contributor Author

stsypanov commented Sep 3, 2021

Redundant null checks get collapsed by HotSpot, so not a performance improvement.

Hi Roger, indeed, no improvement observed. How did you know it beforehand? I was pretty sure we are going to win a couple of ns on this wide-used method.

P.S. This double-check of null seems to be a left-over from #2102. Previously there was only one NPE-throwing check there, but after String.encode(Charset, byte, byte[]) was introduced we have two of them.

naotoj
naotoj approved these changes Sep 4, 2021
Copy link
Member

@naotoj naotoj left a comment

Looks good. Please add some noreg keyword to the JIRA issue.

@stsypanov
Copy link
Contributor Author

stsypanov commented Sep 4, 2021

/integrate

@openjdk openjdk bot added the sponsor label Sep 4, 2021
@openjdk
Copy link

openjdk bot commented Sep 4, 2021

@stsypanov
Your change (at version 441bcff) is now ready to be sponsored by a Committer.

@stsypanov
Copy link
Contributor Author

stsypanov commented Sep 4, 2021

@naotoj done, added noreg-cleanup tag

@naotoj
Copy link
Member

naotoj commented Sep 8, 2021

/sponsor

@openjdk
Copy link

openjdk bot commented Sep 8, 2021

Going to push as commit e5f298a.
Since your change was applied there have been 51 commits pushed to the master branch:

  • cb112af: 8269373: some tests in jdk/tools/launcher/ fails on localized Windows platform
  • 7e662e7: 8272413: Incorrect num of element count calculation for vector cast
  • f2f8136: 8265443: IGV: disambiguate groups by emiting additional properties
  • 59c9f75: 8273375: Remove redundant 'new String' calls after concatenation in java.desktop
  • faa942c: 8273000: Remove WeakReference-based class initialisation barrier implementation
  • 21012f2: 8078641: MethodHandle.asTypeCache can retain classes from unloading
  • 1855574: 8273038: ChannelInputStream.transferTo() uses FileChannel.transferTo(FileChannel)
  • 6750c34: 8270533: AArch64: size_fits_all_mem_uses should return false if its output is a CAS
  • a66629a: 8254167: G1: Record regions where evacuation failed to provide targeted iteration
  • 286a1f6: 8273440: Zero: Disable runtime/Unsafe/InternalErrorTest.java
  • ... and 41 more: https://git.openjdk.java.net/jdk/compare/fa9c8657dfaa2018f501de1a8aa62f004342c245...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Sep 8, 2021
@openjdk openjdk bot added integrated and removed ready rfr sponsor labels Sep 8, 2021
@openjdk
Copy link

openjdk bot commented Sep 8, 2021

@naotoj @stsypanov Pushed as commit e5f298a.

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

@stsypanov stsypanov deleted the 8273329 branch Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs integrated
5 participants