Skip to content

Commit

Permalink
perf: Improve @babel/types builders (babel#16842)
Browse files Browse the repository at this point in the history
* perf

* perf

* add bench
  • Loading branch information
liuxingbaoyu authored Sep 23, 2024
1 parent 4ff9408 commit ebe4b7c
Show file tree
Hide file tree
Showing 5 changed files with 1,378 additions and 444 deletions.
26 changes: 26 additions & 0 deletions benchmark/babel-types/builders/multiple.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Benchmark, baselineTypes, currentTypes } from "../../util.mjs";

const bench = new Benchmark();

function benchCases(implementations) {
for (const version in implementations) {
const t = implementations[version];
const func = t.stringLiteral;
const func2 = t.memberExpression;
const func3 = t.assignmentExpression;
const func4 = t.binaryExpression;

const id = t.identifier("id");
const id2 = t.identifier("id2");
bench.add(`${version} builder`, () => {
func("bar");
func2(id, id2, /*computed?*/ false /*, optional? missing*/);
func3("=", id, id2);
func4("+", id, id2);
});
}
}

benchCases({ baselineTypes, currentTypes });

bench.run();
40 changes: 29 additions & 11 deletions packages/babel-types/scripts/generators/builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import {
} from "../../lib/index.js";
import formatBuilderName from "../utils/formatBuilderName.js";
import stringifyValidator from "../utils/stringifyValidator.js";
// eslint-disable-next-line import/no-extraneous-dependencies
import { IS_BABEL_8 } from "$repo-utils";

// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
function bool(value) {
return value && value !== "false" && value !== "0";
}

if (!bool(process.env.BABEL_8_BREAKING)) {
if (!IS_BABEL_8()) {
// eslint-disable-next-line no-var
var lowerFirst = function (string) {
return string[0].toLowerCase() + string.slice(1);
Expand Down Expand Up @@ -100,9 +97,14 @@ function generateLowercaseBuilders() {
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import validateNode from "../validateNode.ts";
import * as _validate from "../../validators/validate.ts";
import type * as t from "../../index.ts";
import deprecationWarning from "../../utils/deprecationWarning.ts";
import * as utils from "../../definitions/utils.ts";
const { validateInternal: validate } = _validate;
const { NODE_FIELDS } = utils;
`;

const reservedNames = new Set(["super", "import"]);
Expand Down Expand Up @@ -136,7 +138,23 @@ import deprecationWarning from "../../utils/deprecationWarning.ts";
.join("\n")}\n }`;

if (builderNames.length > 0) {
output += `\n return validateNode<t.${type}>(${nodeObjectExpression});`;
output += `\n const node:t.${type} = ${nodeObjectExpression};`;
output += `\n const defs = NODE_FIELDS.${type};`;

fieldNames.forEach(fieldName => {
const field = NODE_FIELDS[type][fieldName];
if (field && builderNames.includes(fieldName)) {
const argName = toBindingIdentifierName(fieldName);
output += `\n validate(defs.${fieldName}, node, "${fieldName}", ${argName}${
JSON.stringify(
stringifyValidator(field.validate, "#node#")
).includes("#node#")
? ", 1"
: ""
});`;
}
});
output += `\n return node;`;
} else {
output += `\n return ${nodeObjectExpression};`;
}
Expand All @@ -146,7 +164,7 @@ import deprecationWarning from "../../utils/deprecationWarning.ts";
output += `export { ${formattedBuilderNameLocal} as ${formattedBuilderName} };\n`;
}

if (!bool(process.env.BABEL_8_BREAKING)) {
if (!IS_BABEL_8()) {
// This is needed for backwards compatibility.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
Expand All @@ -168,7 +186,7 @@ function ${type}(${generateBuilderArgs(newType).join(", ")}) {
}
export { ${type} as ${formattedBuilderName} };\n`;

if (!bool(process.env.BABEL_8_BREAKING)) {
if (!IS_BABEL_8()) {
// This is needed for backwards compatibility.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
Expand All @@ -191,7 +209,7 @@ function generateUppercaseBuilders() {
* conflict with AST types. TypeScript reads the uppercase.d.ts file instead.
*/
export {\n`;
export {\n`;

Object.keys(BUILDER_KEYS).forEach(type => {
const formattedBuilderName = formatBuilderName(type);
Expand Down
Loading

0 comments on commit ebe4b7c

Please sign in to comment.