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
Replace generic __clone call by specific methods #13611
Conversation
return cloneIdentifier(node); | ||
} | ||
|
||
export function cloneIdentifier(node: any): any { |
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.
Unfortunately I can not get flow types working on this method. The Placeholder
type complicates the typings here.
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.
Does using generics work? Something like
export function cloneIdentifier<T: Identifier | Placeholder<"Identifier">>(node: T): T
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 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">) */
.
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.
Has the parser conversion to TS already started?
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.
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); |
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.
We can use plain object after we remove __clone
method from the AST node.
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.
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.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47668/ |
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:
|
@@ -27,6 +27,7 @@ class Node implements NodeBase { | |||
innerComments: Array<Comment>; | |||
extra: { [key: string]: any }; | |||
|
|||
// todo(Babel 8): remove this method in Babel 8 |
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.
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 { |
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.
Does using generics work? Something like
export function cloneIdentifier<T: Identifier | Placeholder<"Identifier">>(node: T): T
} | ||
const cloned = Object.create(NodePrototype); |
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.
'Object.create' is so much slower than a constructor. And overall it's slow.
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.
We will remove Object.create
here in Babel 8.
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't you just use the 'proto' trick?
const table = {
__proto__: null,
JLHwung: author
}
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.
__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)
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.
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
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.
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).
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.
or just pack the loc data in a series of binary numbers
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.
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)
0dca513
to
4bbf05d
Compare
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 thelength-1 named export
benchmark cases. In this case the named export specifier is cloned as export values.