Skip to content

Conversation

seiyab
Copy link
Contributor

@seiyab seiyab commented Aug 22, 2024

Description

Algorithm in printDocToString() for fill() has quadratic time complexity (existing comment is partially wrong).
This PR make it linear (possibly log-linear if I miss some logic. less than quadratic at most).

This will improve some edge cases significantly faster.
For common practical cases, most people will be affected little, probably (time for yarn test was almost same on my machine).
For users who mainly write languages which tend to use fill(), like HTML, they will get slightly faster formatting (time for yarn test ./tests/format/html get ~5% faster).

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
    • Do we need this for this PR?
  • I’ve read the contributing guidelines.

Try the playground for this PR

@seiyab

This comment was marked as resolved.

expect(endTime - startTime).toBeLessThan(1000);
expect(endTime - startTime).toBeLessThan(100);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

On my machine, it was:

  • 181.9 on origin/main
  • 13.3 on this branch

It get x10~ faster so dividing by 10 should be safe.

Copy link
Contributor Author

@seiyab seiyab Aug 22, 2024

Choose a reason for hiding this comment

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

Repeating the test, 181.9 on origin/main looks to be a bad case. It can be sometimes done in about 100ms.

@seiyab seiyab marked this pull request as ready for review August 22, 2024 06:30
const remainingCmd = {
ind,
mode,
doc: { ...doc, parts: parts.slice(2) },
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can simply save the the offset to doc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right. It looks simpler 681a603.

Copy link
Member

@fisker fisker left a comment

Choose a reason for hiding this comment

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

Great work!

@fisker
Copy link
Member

fisker commented Aug 23, 2024

We are printing fill two parts each time, I wonder if we can use binary search to print as many as possible at one time. Not sure if it will improve the performance, but I had this idea for a while, and didn't get time to try.

@seiyab
Copy link
Contributor Author

seiyab commented Aug 23, 2024

Thank you for review!

Binary search looks good at the first impression. However, probably it won't improve computational complexity. We have to measure length for each element. If we measure it during binary searh, it will get O(n logn). Reducing measuring cost with cumsum, it will get O(n). Note that though binary search is O(logn), constructing comsum costs O(n).
It doesn't mean binary search can't improve speed because constant factor can be still improved. But it won't be significant.
To improve more, we should make sure whether printing fill can still be bottleneck by measuring it more, first.

Copy link
Member

@sosukesuzuki sosukesuzuki left a comment

Choose a reason for hiding this comment

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

Thank you for working on this

@sosukesuzuki sosukesuzuki merged commit c921abe into prettier:main Aug 23, 2024
29 checks passed
@seiyab seiyab deleted the fill-complexity branch August 23, 2024 23:52
@seiyab seiyab mentioned this pull request Nov 27, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants