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

8299677: Formatter.format might take a long time to format an integer or floating-point #11939

Closed
wants to merge 5 commits into from

Conversation

rgiulietti
Copy link
Contributor

@rgiulietti rgiulietti commented Jan 11, 2023

This change transforms a O(n^2) path to O(n) when prepending zero padding to decimal outputs, where n is the length of the padding.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8299677: Formatter.format might take a long time to format an integer or floating-point

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/11939/head:pull/11939
$ git checkout pull/11939

Update a local copy of the PR:
$ git checkout pull/11939
$ git pull https://git.openjdk.org/jdk pull/11939/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11939

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/11939.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 11, 2023

👋 Welcome back rgiulietti! 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 Pull request is ready for review label Jan 11, 2023
@openjdk
Copy link

openjdk bot commented Jan 11, 2023

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

  • core-libs
  • i18n

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 core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org labels Jan 11, 2023
@mlbridge
Copy link

mlbridge bot commented Jan 11, 2023

Webrevs

@AlanBateman
Copy link
Contributor

AlanBateman commented Jan 11, 2023

I assume this issue has been there for a long time, just not noticed because it would be unusual to specify a width of this magnitude. The switch to using repeat + one insert looks good. Are we okay for tests in this area?

@rgiulietti
Copy link
Contributor Author

@AlanBateman This is not a functional bug, only an enhancement to overcome the long times for large, unrealistic widths.
Consequently, I have no specific tests that measure the timing against some hard-coded limits, if I understand you correctly.

@JimLaskey
Copy link
Member

JimLaskey commented Jan 11, 2023

Just as a note, I've been thinking about adding a repeat(String src, int count) method to AbstractStringBuffer for quite some time. I've held off because I would add a fillWith(T[] dst, T[] src, int start, int end) methods to java.util.Arrays first. I think both sets of methods would be generally useful based on use patterns in the JDK but for time and motivation.

@AlanBateman
Copy link
Contributor

@AlanBateman This is not a functional bug, only an enhancement to overcome the long times for large, unrealistic widths.
Consequently, I have no specific tests that measure the timing against some hard-coded limits, if I understand you correctly.

My question wasn't phrased very well, I was really just asking if we have good test coverage for width in the format specifier so that it would catch any regressions from changes to this code.

@rgiulietti
Copy link
Contributor Author

Since specific tests for padding seem lacking, I'll add some as part of this PR.
Please expect another commit before marking as reviewed.

@rgiulietti
Copy link
Contributor Author

Added straightforward tests for padding.

sb.insert(begin, zero);
}
if (width > sb.length() && Flags.contains(f, Flags.ZERO_PAD)) {
String zeros = Character.toString(zero).repeat(width - sb.length());
Copy link
Contributor

@stsypanov stsypanov Jan 12, 2023

Choose a reason for hiding this comment

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

Character.toString(zero) could be replaced directly with String.valueOf(zero) saving one more method call

Copy link

@balcanuc balcanuc Jan 15, 2023

Choose a reason for hiding this comment

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

is the bug going to be backported to JDK8? Due to the use of 'repeat' this seems to not be possible.

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.

Thanks for expanding the tests for this.


/*
* @test
* @run junit Padding
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably should add a @summary tag to this, maybe a @bug too (I think JDK-4906370 added Formatter in Java 5).

@openjdk
Copy link

openjdk bot commented Jan 12, 2023

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

8299677: Formatter.format might take a long time to format an integer or floating-point

Reviewed-by: alanb, stsypanov, darcy

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 42 new commits pushed to 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 Jan 12, 2023
Copy link
Contributor

@LanceAndersen LanceAndersen left a comment

Choose a reason for hiding this comment

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

Overall this looks OK.

Please add either a general comment describing what we are trying to test or a comment for each test method. As we add new tests, adding comments for additional clarity will benefit future maintainers

@Nested
class BlankPaddingInt {

@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Any thought given to using @MethodSource: in order to specify a DataProvider? The benefit would be you could catch each failure vs having to address or comment out the assertEquals that is failing

@rgiulietti
Copy link
Contributor Author

All concerns were addressed, I hope.

@rgiulietti
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jan 12, 2023

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

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jan 12, 2023
@openjdk openjdk bot closed this Jan 12, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jan 12, 2023
@openjdk
Copy link

openjdk bot commented Jan 12, 2023

@rgiulietti Pushed as commit 33412c1.

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

@rgiulietti rgiulietti deleted the JDK-8299677 branch January 12, 2023 20:38
Copy link

@balcanuc balcanuc left a comment

Choose a reason for hiding this comment

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

in which JDK versions will the bug-fix back ported? As it is, it is impossible to be back ported to JDK8

sb.insert(begin, zero);
}
if (width > sb.length() && Flags.contains(f, Flags.ZERO_PAD)) {
String zeros = Character.toString(zero).repeat(width - sb.length());
Copy link

@balcanuc balcanuc Jan 15, 2023

Choose a reason for hiding this comment

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

is the bug going to be backported to JDK8? Due to the use of 'repeat' this seems to not be possible.

@rgiulietti
Copy link
Contributor Author

@balcanuc Backporting is done in other, separate projects. Whether changes in mainline are backported, and how, is publicly discussed in the respective mailing lists.

In particular, for JDK 8 Updates you might want to have a look at
this mailing list and the this git repo.

If you feel this change is relevant to JDK 8, you are welcome to discuss a solution that does not rely on repeat() on that mailing list, and then contribute your code to the JDK 8 Update project.

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 i18n i18n-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

7 participants