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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,12 @@ class Node implements NodeBase { | |
trailingComments: Array<Comment>; | ||
innerComments: Array<Comment>; | ||
extra: { [key: string]: any }; | ||
} | ||
const NodePrototype = Node.prototype; | ||
|
||
__clone(): this { | ||
if (!process.env.BABEL_8_BREAKING) { | ||
// $FlowIgnore | ||
NodePrototype.__clone = function (): Node { | ||
// $FlowIgnore | ||
const newNode: any = new Node(); | ||
const keys = Object.keys(this); | ||
|
@@ -39,13 +43,51 @@ class Node implements NodeBase { | |
key !== "trailingComments" && | ||
key !== "innerComments" | ||
) { | ||
// $FlowIgnore | ||
newNode[key] = this[key]; | ||
} | ||
} | ||
|
||
return newNode; | ||
}; | ||
} | ||
|
||
function clonePlaceholder(node: any): any { | ||
return cloneIdentifier(node); | ||
} | ||
|
||
export function cloneIdentifier(node: any): any { | ||
// We don't need to clone `typeAnnotations` and `optional`: because | ||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. We can use plain object after we remove There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
cloned.type = type; | ||
cloned.start = start; | ||
cloned.end = end; | ||
cloned.loc = loc; | ||
cloned.range = range; | ||
cloned.extra = extra; | ||
cloned.name = name; | ||
if (type === "Placeholder") { | ||
cloned.expectedNode = node.expectedNode; | ||
} | ||
return cloned; | ||
} | ||
|
||
export function cloneStringLiteral(node: any): any { | ||
const { type, start, end, loc, range, extra } = node; | ||
if (type === "Placeholder") { | ||
return clonePlaceholder(node); | ||
} | ||
const cloned = Object.create(NodePrototype); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. We will remove There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
I agree. But we should get rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Well there are also |
||
cloned.type = "StringLiteral"; | ||
cloned.start = start; | ||
cloned.end = end; | ||
cloned.loc = loc; | ||
cloned.range = range; | ||
cloned.extra = extra; | ||
cloned.value = node.value; | ||
return cloned; | ||
} | ||
|
||
export class NodeUtils extends UtilParser { | ||
|
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
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