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

perf: Improve generator perf #14701

Merged
merged 7 commits into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion babel.config.js
Expand Up @@ -162,7 +162,11 @@ module.exports = function (api) {
presets: [
[
"@babel/preset-typescript",
{ onlyRemoveTypeImports: true, allowDeclareFields: true },
{
onlyRemoveTypeImports: true,
allowDeclareFields: true,
optimizeConstEnums: true,
},
],
["@babel/env", envOpts],
["@babel/preset-flow", { allowDeclareFields: true }],
Expand Down Expand Up @@ -200,6 +204,10 @@ module.exports = function (api) {
].map(normalize),
plugins: ["babel-plugin-transform-charcodes"],
},
{
test: ["packages/babel-generator"].map(normalize),
plugins: [pluginGeneratorOptimization],
},
convertESM && {
test: ["./packages/babel-node/src"].map(normalize),
// Used to conditionally import kexec
Expand Down Expand Up @@ -781,3 +789,37 @@ function pluginInjectNodeReexportsHints({ types: t, template }, { names }) {
},
};
}

/**
* @param {import("@babel/core")} pluginAPI
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param {import("@babel/core")} pluginAPI
* @param {import("@babel/core").PluginAPI}

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently our babel/core type definitions are in the Microsoft repo, which does not yet have a PluginAPI.

module "C:/Users/user/AppData/Local/Microsoft/TypeScript/4.7/node_modules/@types/babel__core/index"

* @returns {import("@babel/core").PluginObj}
*/
function pluginGeneratorOptimization({ types: t }) {
return {
visitor: {
CallExpression: {
exit(path) {
const node = path.node;
if (
t.isMemberExpression(node.callee) &&
t.isThisExpression(node.callee.object)
) {
const args = node.arguments;

if (
node.callee.property.name === "token" &&
args.length === 1 &&
t.isStringLiteral(args[0])
) {
const str = args[0].value;
if (str.length == 1) {
node.callee.property.name = "tokenChar";
args[0] = t.numericLiteral(str.charCodeAt(0));
}
}
}
},
},
},
};
}
14 changes: 9 additions & 5 deletions benchmark/babel-generator/real-case/jquery.mjs
Expand Up @@ -15,16 +15,20 @@ function createInput(length) {
);
}

const inputs = [1, 4, 16, 64].map(length => ({
tag: length,
body: createInput(length),
}));

function benchCases(name, implementation, options) {
for (const length of [1, 2]) {
const input = createInput(length);
suite.add(`${name} ${length} jquery 3.6`, () => {
implementation(input, options);
for (const input of inputs) {
suite.add(`${name} ${input.tag} jquery 3.6`, () => {
implementation(input.body, options);
});
}
}

benchCases("baseline", baseline.default);
benchCases("current", current.default);
benchCases("baseline", baseline.default);
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved

suite.on("cycle", report).run();