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
Conversation
@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. |
LGTM |
lib/util/source-code-fixer.js
Outdated
let fix = null, | ||
lastPos = 0, | ||
start = 0, | ||
end = 0, |
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.
Can start
and end
be defined in a smaller scope? (They don't seem to be used outside of the for-of
loop.)
lib/util/source-code-fixer.js
Outdated
} else { | ||
// Remain overwrapped fixes as problems. | ||
// lastPos's initial value is 0. | ||
if (lastPos > 0 && lastPos >= start) { |
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.
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.
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.
Good catch!
LGTM |
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. |
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, just want another set of eyes to review.
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:
Array#splice
in reverse.Array#splice
is aO(n)
operation, then it's repeated as many times as the number of fixes.The new way of this PR is:
+=
). 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)
toO(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?