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

Replace generic __clone call by specific methods #13611

Merged
merged 4 commits into from Jul 30, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jul 28, 2021

Q                       A
License MIT

In this PR we replace the generic Node#__clone by specific methods. We observe 80% performance boost compared to @babel/parser 7.14.8 in the length-1 named export benchmark cases. In this case the named export specifier is cloned as export values.

baseline 256 length-1 named export: 4_704 ops/sec ±1.59% (0.213ms)
baseline 512 length-1 named export: 2_426 ops/sec ±0.52% (0.412ms)
baseline 1024 length-1 named export: 1_118 ops/sec ±1.23% (0.895ms)
baseline 2048 length-1 named export: 556 ops/sec ±0.77% (1.799ms)
current 256 length-1 named export: 7_073 ops/sec ±33.67% (0.141ms)
current 512 length-1 named export: 4_441 ops/sec ±0.79% (0.225ms)
current 1024 length-1 named export: 2_142 ops/sec ±1.09% (0.467ms)
current 2048 length-1 named export: 943 ops/sec ±2.12% (1.06ms)

@JLHwung JLHwung added pkg: parser PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels Jul 28, 2021
return cloneIdentifier(node);
}

export function cloneIdentifier(node: any): any {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately I can not get flow types working on this method. The Placeholder type complicates the typings here.

Copy link
Member

Choose a reason for hiding this comment

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

Does using generics work? Something like

export function cloneIdentifier<T: Identifier | Placeholder<"Identifier">>(node: T): T

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried generics. However it seems that Flow cannot infer the type from type === "Placeholder". So I fallback to invariant comments, however Flow will throw "unexpected )" at the invariant comment /*:: invariant(node instanceof Placeholder<"Identifier">) */.

Copy link
Member

Choose a reason for hiding this comment

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

Has the parser conversion to TS already started?

Copy link
Member

Choose a reason for hiding this comment

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

Not yet; you can see the wip at #11578

// cloneIdentifier is only used in object shorthand and named import/export.
// Neither of them allow type annotations after the identifier or optional identifier
const { type, start, end, loc, range, extra, name } = node;
const cloned = Object.create(NodePrototype);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can use plain object after we remove __clone method from the AST node.

Copy link

@KFlash KFlash Jul 28, 2021

Choose a reason for hiding this comment

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

why do you need to clone? You will experience more than 80% perf boost if you avoid clone and instead rewrite lexer and parser. And also all the property access you are using kills perf.

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 28, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 28, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4bbf05d:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@@ -27,6 +27,7 @@ class Node implements NodeBase {
innerComments: Array<Comment>;
extra: { [key: string]: any };

// todo(Babel 8): remove this method in Babel 8
Copy link
Member

Choose a reason for hiding this comment

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

You could remove it from here and do

if (!process.env.BABEL_8_BREAKING) {
  NodePrototype.__clone = function () { /* ... */ };
}

return cloneIdentifier(node);
}

export function cloneIdentifier(node: any): any {
Copy link
Member

Choose a reason for hiding this comment

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

Does using generics work? Something like

export function cloneIdentifier<T: Identifier | Placeholder<"Identifier">>(node: T): T

packages/babel-parser/src/parser/statement.js Show resolved Hide resolved
}
const cloned = Object.create(NodePrototype);
Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will remove Object.create here in Babel 8.

Copy link

@KFlash KFlash Jul 29, 2021

Choose a reason for hiding this comment

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

Can't you just use the 'proto' trick?

const table = {
  __proto__: null,
  JLHwung: author
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

__proto__ is slower than Object.create:

baseline 256 length-1 named export: 5_650 ops/sec ±45.38% (0.177ms)
baseline 512 length-1 named export: 4_129 ops/sec ±1.16% (0.242ms)
baseline 1024 length-1 named export: 1_977 ops/sec ±1.31% (0.506ms)
baseline 2048 length-1 named export: 894 ops/sec ±2.1% (1.118ms)
current 256 length-1 named export: 5_269 ops/sec ±29.84% (0.19ms)
current 512 length-1 named export: 3_003 ops/sec ±2.24% (0.333ms)
current 1024 length-1 named export: 1_473 ops/sec ±2.04% (0.679ms)
current 2048 length-1 named export: 710 ops/sec ±1.6% (1.409ms)

Copy link

Choose a reason for hiding this comment

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

Interesting. I never clone so didn't check that. What I do is to have a create function that creates and return an obj based on the given params. When I need to "clone" I re-use that function and create a new obj based on the given params again. That way I avoid spread, clone and other slow stuff. And it makes GC happy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

create a new obj based on the given params again

I agree. But we should get rid of loc and ranges first so the parameters can be limited to start, end, value and type (solely for placeholder support).

Copy link

Choose a reason for hiding this comment

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

or just pack the loc data in a series of binary numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well there are also filename and identifierName in the loc.

baseline 256 length-1 named export: 4_704 ops/sec ±1.59% (0.213ms)
baseline 512 length-1 named export: 2_426 ops/sec ±0.52% (0.412ms)
baseline 1024 length-1 named export: 1_118 ops/sec ±1.23% (0.895ms)
baseline 2048 length-1 named export: 556 ops/sec ±0.77% (1.799ms)
current 256 length-1 named export: 7_073 ops/sec ±33.67% (0.141ms)
current 512 length-1 named export: 4_441 ops/sec ±0.79% (0.225ms)
current 1024 length-1 named export: 2_142 ops/sec ±1.09% (0.467ms)
current 2048 length-1 named export: 943 ops/sec ±2.12% (1.06ms)
@JLHwung JLHwung force-pushed the avoid--clone-call-in-parser branch from 0dca513 to 4bbf05d Compare July 29, 2021 14:02
@nicolo-ribaudo nicolo-ribaudo merged commit d3a7cd5 into babel:main Jul 30, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the avoid--clone-call-in-parser branch July 30, 2021 20:19
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 30, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser 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.

None yet

5 participants