Skip to content

Commit

Permalink
perf: Improve generator perf (#14701)
Browse files Browse the repository at this point in the history
Co-authored-by: Armano <armano2@users.noreply.github.com>
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
3 people committed Jul 8, 2022
1 parent 50f2a8b commit 6165537
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 313 deletions.
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
* @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);

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

0 comments on commit 6165537

Please sign in to comment.