-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
8300818: Reduce complexity of padding with DateTimeFormatter #12131
Conversation
👋 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
|
src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java
Outdated
Show resolved
Hide resolved
for (int i = 0; i < padWidth - len; i++) { | ||
buf.insert(preLen, padChar); | ||
} | ||
buf.insert(preLen, String.valueOf(padChar).repeat(padWidth - len)); |
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.
Have you checked with a microbenchmark that this added allocation can be elided by JITs and that there's a significant speed-up with your changes? I don't have the necessary domain expertise to assert anything here but I suspect padding widths are typically short. Such as 2 or 4 (for day/month and year fields) so a micro should examine there's no regression for little to no padding. Unlike the original code you call insert
even if padWidth - len == 0
. This might be optimized away by JITs, but it'd be good to verify which is best.
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 modified code is called only when a user explicitly calls padNext(int, char)
, i.e. if I modified the example snippet as
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendLiteral("Date:")
//.padNext(20, ' ')
.append(DateTimeFormatter.ISO_DATE)
.toFormatter();
String text = dtf.format(LocalDateTime.now());
there's no regression.
Right now I cannot build ad-hoc JDK with my changes and check it with JMH, so I'd convert this to draft until I can verify it
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.
Meant that you should verify that something like this, which just add a little padding, doesn't regress with your changes:
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendLiteral("Year:")
.padNext(5)
.appendValue(ChronoField.YEAR)
.toFormatter();
...
dtf.format(LocalDateTime.now());
And similar for effectively no padding (.padNext(4)
in the above example). As this API might often be used to ensure short 2-4 char fields are correctly padded I think it's important that we're not adding overhead to such use cases.
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.
Added benchmark for this
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.
Special casing for len == 0 and keeping the existing buf.insert for len == 1 would avoid object creation except when it would improve performance.
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.
@RogerRiggs sorry I don't get it. Maybe you mean speacial casing for padWidth - len
?
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.
Yes, I meant on the length of the inserted padding.
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.
Done
fyi, you might want to wait for #12728 |
@RogerRiggs thanks for pointing this out. Could we also have an overloaded method for inserting apart from appending? |
Benchmarking:
|
@RogerRiggs mentioned PR was merged without suggested repeat-into-position method, so I this code remains as is. |
test/micro/org/openjdk/bench/java/time/format/DateTimeFormatterBench.java
Outdated
Show resolved
Hide resolved
for (int i = 0; i < padWidth - len; i++) { | ||
buf.insert(preLen, padChar); | ||
} | ||
buf.insert(preLen, String.valueOf(padChar).repeat(padWidth - len)); |
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.
Special casing for len == 0 and keeping the existing buf.insert for len == 1 would avoid object creation except when it would improve performance.
With latest changes
|
test/micro/org/openjdk/bench/java/time/format/DateTimeFormatterBench.java
Outdated
Show resolved
Hide resolved
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.
LGTM; thanks
@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 177 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 (@cl4es, @RogerRiggs) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
/integrate |
@stsypanov |
test/micro/org/openjdk/bench/java/time/format/DateTimeFormatterWithPaddingBench.java
Outdated
Show resolved
Hide resolved
I added a few more small padding cases and and re-ran on my Mac:
The expected steps in performance match the code paths of 0, 1, 2-6. |
/integrate |
@stsypanov |
/sponsor |
Going to push as commit 561ec9c.
Your commit was automatically rebased without conflicts. |
@RogerRiggs @stsypanov Pushed as commit 561ec9c. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Currently it's O(n) - we do
n
shifts of bytes withinStringBuilder
. This can be reduced to O(1) improving the code like:Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/12131/head:pull/12131
$ git checkout pull/12131
Update a local copy of the PR:
$ git checkout pull/12131
$ git pull https://git.openjdk.org/jdk.git pull/12131/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 12131
View PR using the GUI difftool:
$ git pr show -t 12131
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/12131.diff
Webrev
Link to Webrev Comment