-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Reduce element decorator temp variables #16280
Merged
JLHwung
merged 6 commits into
babel:feat-7.24.0/decorators
from
JLHwung:improve-member-decs-temp-variables
Feb 14, 2024
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ddefc1e
optimize: allocate temp variable for every decorated element
JLHwung ad7add1
update test fixtures
JLHwung a91381e
refactor: sort decorator info once
JLHwung 740a10c
refactor: simplify maybeExtractDecorators interface
JLHwung 0977e10
refactor: rename maybeExtractDecorators to handleDecoratorExpressions
JLHwung b86cb8c
unify haveThis and hasThis
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -455,7 +455,7 @@ const SETTER = 4; | |
|
||
const STATIC_OLD_VERSION = 5; // Before 2023-05 | ||
const STATIC = 8; // 1 << 3 | ||
const DECORATORS_HAVE_THIS = 16; // 1 << 3 | ||
const DECORATORS_HAVE_THIS = 16; // 1 << 4 | ||
|
||
function getElementKind(element: NodePath<ClassDecoratableElement>): number { | ||
switch (element.node.type) { | ||
|
@@ -478,9 +478,9 @@ function getElementKind(element: NodePath<ClassDecoratableElement>): number { | |
|
||
// Information about the decorators applied to an element | ||
interface DecoratorInfo { | ||
// The expressions of the decorators themselves | ||
decorators: t.Expression[]; | ||
decoratorsThis: t.Expression[]; | ||
// An array of applied decorators or a memoised identifier | ||
decoratorsArray: t.Identifier | t.ArrayExpression | t.Expression; | ||
decoratorsHaveThis: boolean; | ||
|
||
// The kind of the decorated value, matches the kind value passed to applyDecs | ||
kind: number; | ||
|
@@ -499,40 +499,32 @@ interface DecoratorInfo { | |
locals: t.Identifier | t.Identifier[] | undefined; | ||
} | ||
|
||
// Information about a computed property key. These must be evaluated | ||
// interspersed with decorator expressions, which is why they get added to the | ||
// array of DecoratorInfos later on. | ||
interface ComputedPropInfo { | ||
localComputedNameId: t.Identifier; | ||
keyNode: t.Expression; | ||
} | ||
|
||
function isDecoratorInfo( | ||
info: DecoratorInfo | ComputedPropInfo, | ||
): info is DecoratorInfo { | ||
return "decorators" in info; | ||
} | ||
|
||
function filteredOrderedDecoratorInfo( | ||
info: (DecoratorInfo | ComputedPropInfo)[], | ||
): DecoratorInfo[] { | ||
const filtered = info.filter(isDecoratorInfo); | ||
|
||
/** | ||
* Sort decoration info in the application order: | ||
* - static non-fields | ||
* - instance non-fields | ||
* - static fields | ||
* - instance fields | ||
* | ||
* @param {DecoratorInfo[]} info | ||
* @returns {DecoratorInfo[]} Sorted decoration info | ||
*/ | ||
function toSortedDecoratorInfo(info: DecoratorInfo[]): DecoratorInfo[] { | ||
return [ | ||
...filtered.filter( | ||
...info.filter( | ||
el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER, | ||
), | ||
...filtered.filter( | ||
...info.filter( | ||
el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER, | ||
), | ||
...filtered.filter(el => el.isStatic && el.kind === FIELD), | ||
...filtered.filter(el => !el.isStatic && el.kind === FIELD), | ||
...info.filter(el => el.isStatic && el.kind === FIELD), | ||
...info.filter(el => !el.isStatic && el.kind === FIELD), | ||
]; | ||
} | ||
|
||
function generateDecorationList( | ||
decorators: t.Expression[], | ||
decoratorsThis: (t.Expression | null)[], | ||
decoratorsThis: (t.Expression | undefined)[], | ||
version: DecoratorVersionKind, | ||
) { | ||
const decsCount = decorators.length; | ||
|
@@ -555,17 +547,11 @@ function generateDecorationList( | |
} | ||
|
||
function generateDecorationExprs( | ||
info: (DecoratorInfo | ComputedPropInfo)[], | ||
decorationInfo: DecoratorInfo[], | ||
version: DecoratorVersionKind, | ||
): t.ArrayExpression { | ||
return t.arrayExpression( | ||
filteredOrderedDecoratorInfo(info).map(el => { | ||
const { decs, hasThis } = generateDecorationList( | ||
el.decorators, | ||
el.decoratorsThis, | ||
version, | ||
); | ||
|
||
decorationInfo.map(el => { | ||
let flag = el.kind; | ||
if (el.isStatic) { | ||
flag += | ||
|
@@ -574,10 +560,10 @@ function generateDecorationExprs( | |
? STATIC | ||
: STATIC_OLD_VERSION; | ||
} | ||
if (hasThis) flag += DECORATORS_HAVE_THIS; | ||
if (el.decoratorsHaveThis) flag += DECORATORS_HAVE_THIS; | ||
|
||
return t.arrayExpression([ | ||
decs.length === 1 ? decs[0] : t.arrayExpression(decs), | ||
el.decoratorsArray, | ||
t.numericLiteral(flag), | ||
el.name, | ||
...(el.privateMethods || []), | ||
|
@@ -586,12 +572,10 @@ function generateDecorationExprs( | |
); | ||
} | ||
|
||
function extractElementLocalAssignments( | ||
decorationInfo: (DecoratorInfo | ComputedPropInfo)[], | ||
) { | ||
function extractElementLocalAssignments(decorationInfo: DecoratorInfo[]) { | ||
const localIds: t.Identifier[] = []; | ||
|
||
for (const el of filteredOrderedDecoratorInfo(decorationInfo)) { | ||
for (const el of decorationInfo) { | ||
const { locals } = el; | ||
|
||
if (Array.isArray(locals)) { | ||
|
@@ -905,27 +889,32 @@ function transformClass( | |
return; | ||
} | ||
|
||
const elementDecoratorInfo: (DecoratorInfo | ComputedPropInfo)[] = []; | ||
const elementDecoratorInfo: DecoratorInfo[] = []; | ||
|
||
let constructorPath: NodePath<t.ClassMethod> | undefined; | ||
const decoratedPrivateMethods = new Set<string>(); | ||
|
||
let classInitLocal: t.Identifier, classIdLocal: t.Identifier; | ||
|
||
const decoratorsThis = new Map<t.Decorator, t.Expression>(); | ||
const maybeExtractDecorators = ( | ||
decorators: t.Decorator[], | ||
memoiseInPlace: boolean, | ||
) => { | ||
// Memoise the this value `a.b` of decorator member expressions `@a.b.dec`, | ||
type handleDecoratorExpressionsResult = { | ||
// whether the whole decorator list requires memoisation | ||
needMemoise: boolean; | ||
// the this value of each decorator if applicable | ||
decoratorsThis: (t.Expression | undefined)[]; | ||
}; | ||
function handleDecoratorExpressions( | ||
expressions: t.Expression[], | ||
): handleDecoratorExpressionsResult { | ||
let needMemoise = false; | ||
for (const decorator of decorators) { | ||
const { expression } = decorator; | ||
const decoratorsThis: (t.Expression | null)[] = []; | ||
for (const expression of expressions) { | ||
let object; | ||
if ( | ||
(version === "2023-11" || | ||
(!process.env.BABEL_8_BREAKING && version === "2023-05")) && | ||
t.isMemberExpression(expression) | ||
) { | ||
let object; | ||
if ( | ||
t.isSuper(expression.object) || | ||
t.isThisExpression(expression.object) | ||
|
@@ -937,17 +926,12 @@ function transformClass( | |
} | ||
object = t.cloneNode(expression.object); | ||
} | ||
decoratorsThis.set(decorator, object); | ||
} | ||
if (!scopeParent.isStatic(expression)) { | ||
needMemoise = true; | ||
if (memoiseInPlace) { | ||
decorator.expression = memoiseExpression(expression, "dec"); | ||
} | ||
} | ||
decoratorsThis.push(object); | ||
needMemoise ||= !scopeParent.isStatic(expression); | ||
} | ||
return needMemoise && !memoiseInPlace; | ||
}; | ||
return { needMemoise, decoratorsThis }; | ||
} | ||
|
||
let needsDeclaraionForClassBinding = false; | ||
let classDecorationsFlag = 0; | ||
|
@@ -960,11 +944,13 @@ function transformClass( | |
|
||
path.node.decorators = null; | ||
|
||
const needMemoise = maybeExtractDecorators(classDecorators, false); | ||
const decoratorExpressions = classDecorators.map(el => el.expression); | ||
const { needMemoise, decoratorsThis } = | ||
handleDecoratorExpressions(decoratorExpressions); | ||
|
||
const { hasThis, decs } = generateDecorationList( | ||
classDecorators.map(el => el.expression), | ||
classDecorators.map(dec => decoratorsThis.get(dec)), | ||
decoratorExpressions, | ||
decoratorsThis, | ||
version, | ||
); | ||
classDecorationsFlag = hasThis ? 1 : 0; | ||
|
@@ -1012,15 +998,39 @@ function transformClass( | |
} | ||
|
||
const { node } = element; | ||
const decorators = element.node.decorators; | ||
const decorators = node.decorators; | ||
|
||
const hasDecorators = !!decorators?.length; | ||
|
||
const isComputed = "computed" in node && node.computed; | ||
|
||
let name = "computedKey"; | ||
|
||
if (node.key.type === "PrivateName") { | ||
name = node.key.id.name; | ||
} else if (!isComputed && node.key.type === "Identifier") { | ||
name = node.key.name; | ||
} | ||
let decoratorsArray: t.Identifier | t.ArrayExpression | t.Expression; | ||
let decoratorsHaveThis; | ||
|
||
if (hasDecorators) { | ||
maybeExtractDecorators(decorators, true); | ||
const decoratorExpressions = decorators.map(d => d.expression); | ||
const { needMemoise, decoratorsThis } = | ||
handleDecoratorExpressions(decoratorExpressions); | ||
const decorationList = generateDecorationList( | ||
decoratorExpressions, | ||
decoratorsThis, | ||
version, | ||
); | ||
const { decs } = decorationList; | ||
decoratorsHaveThis = decorationList.hasThis; | ||
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. Nit: we should unify |
||
decoratorsArray = decs.length === 1 ? decs[0] : t.arrayExpression(decs); | ||
if (needMemoise) { | ||
decoratorsArray = memoiseExpression(decoratorsArray, name + "Decs"); | ||
} | ||
} | ||
|
||
const isComputed = "computed" in element.node && element.node.computed; | ||
if (isComputed) { | ||
if (!element.get("key").isConstantExpression()) { | ||
node.key = memoiseExpression( | ||
|
@@ -1030,20 +1040,11 @@ function transformClass( | |
} | ||
} | ||
|
||
const kind = getElementKind(element); | ||
const { key } = node; | ||
const { key, static: isStatic } = node; | ||
|
||
const isPrivate = key.type === "PrivateName"; | ||
|
||
const isStatic = element.node.static; | ||
|
||
let name = "computedKey"; | ||
|
||
if (isPrivate) { | ||
name = key.id.name; | ||
} else if (!isComputed && key.type === "Identifier") { | ||
name = key.name; | ||
} | ||
const kind = getElementKind(element); | ||
|
||
if (isPrivate && !isStatic) { | ||
if (hasDecorators) { | ||
|
@@ -1187,8 +1188,8 @@ function transformClass( | |
|
||
elementDecoratorInfo.push({ | ||
kind, | ||
decorators: decorators.map(d => d.expression), | ||
decoratorsThis: decorators.map(d => decoratorsThis.get(d)), | ||
decoratorsArray, | ||
decoratorsHaveThis, | ||
name: nameExpr, | ||
isStatic, | ||
privateMethods, | ||
|
@@ -1276,13 +1277,17 @@ function transformClass( | |
staticFieldInitializerExpressions = []; | ||
} | ||
|
||
const sortedElementDecoratorInfo = | ||
toSortedDecoratorInfo(elementDecoratorInfo); | ||
|
||
const elementDecorations = generateDecorationExprs( | ||
elementDecoratorInfo, | ||
sortedElementDecoratorInfo, | ||
version, | ||
); | ||
|
||
const elementLocals: t.Identifier[] = | ||
extractElementLocalAssignments(elementDecoratorInfo); | ||
const elementLocals: t.Identifier[] = extractElementLocalAssignments( | ||
sortedElementDecoratorInfo, | ||
); | ||
|
||
if (protoInitLocal) { | ||
elementLocals.push(protoInitLocal); | ||
|
@@ -1407,9 +1412,7 @@ function transformClass( | |
elementLocals, | ||
classLocals, | ||
elementDecorations, | ||
classDecorationsId | ||
? t.cloneNode(classDecorationsId) | ||
: t.arrayExpression(classDecorations), | ||
classDecorationsId ?? t.arrayExpression(classDecorations), | ||
t.numericLiteral(classDecorationsFlag), | ||
needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, | ||
typeof className === "object" ? className : undefined, | ||
|
6 changes: 3 additions & 3 deletions
6
...bel-plugin-proposal-decorators/test/fixtures/2021-12-exported/member-decorator/output.mjs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
var _dec, _init_x; | ||
_dec = dec; | ||
var _xDecs, _init_x; | ||
_xDecs = dec; | ||
export class A { | ||
static { | ||
[_init_x] = babelHelpers.applyDecs(this, [[_dec, 0, "x"]], []); | ||
[_init_x] = babelHelpers.applyDecs(this, [[_xDecs, 0, "x"]], []); | ||
} | ||
x = _init_x(this); | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
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 think this PR is not depending on tc39/proposal-decorators#524, since any normative change of decorator application ordering should be implemented within
toSortedDecoratorInfo
.