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

Chore: simplify and improve performance for autofix #8035

Merged
merged 2 commits into from Feb 11, 2017
Merged

Conversation

mysticatea
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X] Other, please explain:

What changes did you make? (Give an overview)

This PR refactors the way of autofix.

The current way is:

  1. Makes the array of characters.
    • This creates many string instances.
  2. Applies fixes to the array with Array#splice in reverse.
    • Array#splice is a O(n) operation, then it's repeated as many times as the number of fixes.
  3. Joins the array.

The new way of this PR is:

  1. Makes the fixed string by concatenation (+=). It concatenates "the part followed by a fix" and "the text of the fix" by turns.

As a result, the performance of autofix improved from O(n×m) to O(n+m). (n is the number of characters, m is the number of fixes)
I measured it by this script then I saw the new way gets 25% faster than the current way.

Is there anything you'd like reviewers to focus on?

  • This PR changes the order of fixing. This is reverse of the current order. So I needed to modify tests for overlapped fixes. However, we are doing multipass fixing, so I think it's no problem.

@mysticatea mysticatea added chore This change is not user-facing core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 7, 2017
@mention-bot
Copy link

@mysticatea, thanks for your PR! By analyzing the history of the files in this pull request, we identified @not-an-aardvark, @platinumazure and @nzakas to be potential reviewers.

@eslintbot
Copy link

LGTM

let fix = null,
lastPos = 0,
start = 0,
end = 0,
Copy link
Member

Choose a reason for hiding this comment

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

Can start and end be defined in a smaller scope? (They don't seem to be used outside of the for-of loop.)

} else {
// Remain overwrapped fixes as problems.
// lastPos's initial value is 0.
if (lastPos > 0 && lastPos >= start) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this will fail to detect a conflict between these two fixes, because there is a special check for lastPos > 0:

({ range: [0, 0], text: "foo" });
({ range: [0, 0], text: "bar" });

lastPos will be 0 after applying the first fix, so lastPos > 0 will be false and the fix will be applied.

Maybe it would be better to set the initial value of lastPos to -Infinity instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

@eslintbot
Copy link

LGTM

@mysticatea mysticatea self-assigned this Feb 8, 2017
@mysticatea
Copy link
Member Author

I modified perf-fix2.js to avoid spawn. This is more accurate than perf-fix.js to measure performance of autofix part.

Then, it's about ×2 faster.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM, just want another set of eyes to review.

@ilyavolodin ilyavolodin merged commit fcc38db into master Feb 11, 2017
@mysticatea mysticatea deleted the refactor-fix branch February 11, 2017 05:25
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion chore This change is not user-facing core Relates to ESLint's core APIs and features evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants