polish: skip creating extra reference for safely re-used node#10720
Merged
nicolo-ribaudo merged 2 commits intobabel:masterfrom Nov 16, 2019
Merged
polish: skip creating extra reference for safely re-used node#10720nicolo-ribaudo merged 2 commits intobabel:masterfrom
nicolo-ribaudo merged 2 commits intobabel:masterfrom
Conversation
| scope.push({ id: ref }); | ||
| let ref, assignment; | ||
| // skip creating extra reference when `left` is semantically safe to re-use | ||
| if (t.isIdentifier(node.left)) { |
Member
There was a problem hiding this comment.
What about using isConstantExpression, or maybe better maybeGenerateMemoised?
Contributor
Author
There was a problem hiding this comment.
Good idea! I end up with maybeGenerateMemoised because it is rare that the left is a literal --even if it is, creating a reference other than inlining this literal should costs less bytes in most situations, given that we generate [_]+ as identifier name for literals.
JLHwung
commented
Nov 15, 2019
| ref = node.left; | ||
| assignment = t.cloneNode(node.left); | ||
| } else { | ||
| assignment = t.assignmentExpression("=", ref, node.left); |
Contributor
Author
There was a problem hiding this comment.
if ref is non-null, it is always cloned from node.left.
jridgewell
approved these changes
Nov 15, 2019
nicolo-ribaudo
approved these changes
Nov 15, 2019
Member
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
We need to figure out why CircleCI isn't running for your PRs
This was referenced Dec 12, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In this PR we skip creating extra reference when the
leftof??is a static reference, which is safely to reused when later unfolding into conditional comparison.Compared to the master branch, this PR generate less code for the common case
var bar = foo ?? "bar"Before:
After: