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

perf: Improve scope information collection performance #16923

Merged
merged 3 commits into from
Nov 19, 2024

Conversation

liuxingbaoyu
Copy link
Member

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Here we introduced a simplified traversal, which improved the overall performance by about 10%. (Well, I don’t understand why it brings such a big performance improvement. I tried more later but failed. 🤦‍♂️)

PS F:\babel\benchmark> node --expose-gc .\all\real-case-ts-mjs.mjs 
all/real-case-ts-mjs.mjs babel-parser-express.ts @ current: 36.76 ops/sec ±8.46% 30 runs (27ms)
all/real-case-ts-mjs.mjs babel-parser-express.ts @ baseline: 33.86 ops/sec ±5.73% 30 runs (30ms)
all/real-case-ts-mjs.mjs ts-parser.ts @ current: 6.51 ops/sec ±6.65% 30 runs (154ms)
all/real-case-ts-mjs.mjs ts-parser.ts @ baseline: 5.21 ops/sec ±4.09% 30 runs (192ms)
PS F:\babel\benchmark> node --expose-gc .\all\real-case-mjs-cjs.mjs
all/real-case-mjs-cjs.mjs ts-checker.mjs @ current: 1.11 ops/sec ±2.37% 30 runs (903ms)
all/real-case-mjs-cjs.mjs ts-checker.mjs @ baseline: 0.93 ops/sec ±2.46% 30 runs (1079ms)
all/real-case-mjs-cjs.mjs ts-parser.mjs @ current: 8.29 ops/sec ±3.11% 30 runs (121ms)
all/real-case-mjs-cjs.mjs ts-parser.mjs @ baseline: 7.36 ops/sec ±3.63% 30 runs (136ms)

@babel-bot
Copy link
Collaborator

babel-bot commented Oct 23, 2024

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58295

Comment on lines -110 to -113
} else {
// @ts-expect-error Expression produces too complex union
visitor[type] = fns;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a bug fix, previously it shallow copied fns to multiple places, but it just happened not to be modified. After we added the definition of BlockScoped it was modified later, causing the visitor to be wrong. Here we use mergePair to do a deep clone.

@liuxingbaoyu liuxingbaoyu marked this pull request as draft October 23, 2024 12:47
@liuxingbaoyu liuxingbaoyu marked this pull request as ready for review October 23, 2024 14:41
@liuxingbaoyu liuxingbaoyu added pkg: traverse (scope) PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels Oct 25, 2024
Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

I think the performance improvement comes from the fact that we avoided creation of traversal context in the simplified traversal. We can investigate if we can share the traversal context used by the sub-traverseNode calls with the root context of the node path where we issued path.traverse() from. If it is possible, it will benefit all Babel plugins.

@@ -133,8 +133,8 @@ export function stop(this: NodePath) {
this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
}

export function setScope(this: NodePath) {
if (this.opts?.noScope) return;
export function setScope(this: NodePath, ignoreNoScope?: boolean) {
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo Oct 30, 2024

Choose a reason for hiding this comment

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

Suggested change
export function setScope(this: NodePath, ignoreNoScope?: boolean) {
export function setScope(this: NodePath, force?: boolean) {

setScope is (unfortunately) part of the public API. What do you think abuot adding an internal

function _forceSetScope(path: NodePath) {}

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.

That's all good to me. :)

@liuxingbaoyu
Copy link
Member Author

I think the performance improvement comes from the fact that we avoided creation of traversal context in the simplified traversal. We can investigate if we can share the traversal context used by the sub-traverseNode calls with the root context of the node path where we issued path.traverse() from. If it is possible, it will benefit all Babel plugins.

I implemented a more complete traversal that fully shares a traversal context, but unfortunately the performance didn't improve.
Perhaps we can start by using this for more sub-traversals in our repository.

@nicolo-ribaudo nicolo-ribaudo merged commit ded1571 into babel:main Nov 19, 2024
54 checks passed
@mydea
Copy link

mydea commented Dec 4, 2024

I believe this PR is possibly causing some problems, e.g. emberjs/ember.js#20806 🤔 I was able to pin stuff down to the 7.26.3 release of @babel/traverse and I think these are the only changes in there!

@liuxingbaoyu
Copy link
Member Author

@mydea Thanks for the report! I will take a look.

JLHwung added a commit that referenced this pull request Dec 5, 2024
JLHwung added a commit that referenced this pull request Dec 5, 2024
Revert "perf: Improve scope information collection performance (#16923)"

This reverts commit ded1571.
@JLHwung JLHwung mentioned this pull request Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: traverse (scope) PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants