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
8289908: Skip bounds check for cases when String is constructed from entirely used byte[] #9407
Conversation
…entirely used byte[]
👋 Welcome back stsypanov! A progress list of the required criteria for merging this PR into |
@stsypanov The following labels will be automatically applied to this pull request:
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. |
Webrevs
|
@@ -1424,7 +1426,7 @@ public String(byte[] bytes, Charset charset) { | |||
* @since 1.1 | |||
*/ | |||
public String(byte[] bytes, int offset, int length) { | |||
this(bytes, offset, length, Charset.defaultCharset()); | |||
this(bytes, offset, length, Charset.defaultCharset(), checkBoundsOffCount(offset, length, bytes.length)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you avoid the extra constructor by applying checkBoundOffCount
to the offset argument; it returns the offset.
this(bytes, checkBoundsOffCount(offset, length, bytes.length), length, Charset.defaultCharset());
or call Preconditions.checkFromIndexSize
directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if I roll back the added constructor I'll go through existing public one public String(byte[] bytes, int offset, int length, Charset charset)
doing bounds check twice, won't I?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new constructor looks very odd, especially when it does not have an explanation and doesn't describe the required preconditions for calling it. Is there a better way than adding a non-functional argument?
The "unused" name is going to draw a warning from IntelliJ and some enterprising developer is going to try to remove it, not understanding why its there. And there is a bit of overhead pushing the extra arg.
The constructor should be private, it has a very narrow scope of use given the lack of checking its args.
It would be nice if the Hotspot compiler would recognize the llmits on the range and optimize away the checks;
it would have broader applicability then this point fix.
I would be interesting to ask the compiler folks if that's a future optimization.
These source changes make it harder to understand what's happening where; though that is sometimes work it for a noticeable performance improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we already have e.g. String(char[], int, int, Void)
and String(AbstractStringBuilder asb, Void sig)
where trailing argument is for disambiguation against private constructors. I did the same for mine and applied the same naming as in other trailing Void
params.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark results after:
Benchmark Mode Cnt Score Error Units
StringConstructor.newStringFromArray avgt 50 4,354 ± 0,195 ns/op
StringConstructor.newStringFromArrayWithCharset avgt 50 4,035 ± 0,088 ns/op
StringConstructor.newStringFromArrayWithCharsetName avgt 50 8,166 ± 0,062 ns/op
StringConstructor.newStringFromRangedArray avgt 50 4,132 ± 0,054 ns/op
StringConstructor.newStringFromRangedArrayWithCharset avgt 50 4,416 ± 0,206 ns/op
StringConstructor.newStringFromRangedArrayWithCharsetName avgt 50 7,421 ± 0,041 ns/op
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matching the existing Void argument looks better.
The new private method should have a comment saying that it does not do any precondition checks on the arguments.
(Reordering the arguments is an alternative to adding an argument, for example, (Charset, byte[], int, int).
But it is less readable and can raise questions due to the different order of arguments.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated PR. With newer version I get these results:
Benchmark Mode Cnt Score Error Units
StringConstructor.newStringFromArray avgt 50 4,831 ± 0,205 ns/op
StringConstructor.newStringFromArrayWithCharset avgt 50 3,940 ± 0,008 ns/op
StringConstructor.newStringFromArrayWithCharsetName avgt 50 7,662 ± 0,112 ns/op
StringConstructor.newStringFromRangedArray avgt 50 4,175 ± 0,065 ns/op
StringConstructor.newStringFromRangedArrayWithCharset avgt 50 3,970 ± 0,037 ns/op
StringConstructor.newStringFromRangedArrayWithCharsetName avgt 50 7,480 ± 0,014 ns/op
|
@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:
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 26 new commits pushed to the
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 (@prrace, @RogerRiggs, @turbanoff) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for the updates.
/integrate |
@stsypanov |
/sponsor |
Going to push as commit efed7a7.
Your commit was automatically rebased without conflicts. |
@prrace @stsypanov Pushed as commit efed7a7. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
We can skip bounds check and null check for Charset in case we use the array entirely and the Charset is either default one or proven to be non-null.
Benchmark results:
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/9407/head:pull/9407
$ git checkout pull/9407
Update a local copy of the PR:
$ git checkout pull/9407
$ git pull https://git.openjdk.org/jdk pull/9407/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 9407
View PR using the GUI difftool:
$ git pr show -t 9407
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/9407.diff