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

Router: extract rules to Splits, use class lookup #4848

Merged
merged 3 commits into from
Feb 28, 2025

Conversation

kitbellew
Copy link
Collaborator

@kitbellew kitbellew commented Feb 26, 2025

A profiler run revealed that getSplits is the largest bottleneck of performance, despite being run only once, almost double that of the part which was previously thought to be the more expensive: BestFirstSearch.

Let's refactor away from using a massive match to several lookups by the token class.

In community-tests runs, there's a 35% reduction in running times (22 minutes to 14, 15 to 10).

@kitbellew
Copy link
Collaborator Author

this is a tricky change :) i tested on scala2+scala3+spark community repos that community-tests runs on, and it produces the same results, but conceivably could be different. if you could check on your repos, would appreciate it.

A profiler run revealed that `getSplits` is the largest bottleneck of
performance, despite being run only once, almost double that of the part
which was previously thought to be the more expensive: BestFirstSearch.

Let's refactor away from using a massive `match` to several lookups by
the token class.
@tgodzik
Copy link
Contributor

tgodzik commented Feb 28, 2025

this is a tricky change :) i tested on scala2+scala3+spark community repos that community-tests runs on, and it produces the same results, but conceivably could be different. if you could check on your repos, would appreciate it.

I checked Metals and Scala CLI repos and the results seem the same, but I only changed version in .scalafmt.conf and run using the jar based runner.

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

LGTM though I don't really have time to look at the full changes.

@kitbellew
Copy link
Collaborator Author

LGTM though I don't really have time to look at the full changes.

Yes, alas, this change is not easily reviewable. The code is basically the same but in different places; but even with that, there were a few tricky parts:

  • now there's a fixed order of rule evaluation (first we look at the left token, then the right, etc), whereas before matching could be in any order
  • the location in the source file (line, to be exact) is used as a tiebreaker when comparing formatting states, and since these lines have changed, I had to add an explicit rank field.

@kitbellew kitbellew merged commit 0aa4936 into scalameta:main Feb 28, 2025
22 checks passed
@kitbellew kitbellew deleted the 4848 branch February 28, 2025 17:25
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.

2 participants