Back parser state exportedIdentifiers by set (#13406)

This commit is contained in:
Huáng Jùnliàng 2021-06-01 07:17:30 -04:00 committed by GitHub
parent b397aca024
commit ae3f5d905a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 15 deletions

View File

@ -0,0 +1,34 @@
import Benchmark from "benchmark";
import baseline from "@babel-baseline/parser";
import current from "../../lib/index.js";
import { report } from "../util.mjs";
const suite = new Benchmark.Suite();
// All codepoints in [0x4e00, 0x9ffc] are valid identifier name per Unicode 13
function createInput(length) {
if (length > 0x9ffc - 0x4e00) {
throw new Error(
`Length greater than ${
0x9ffc - 0x4e00
} is not supported! Consider modify the \`createInput\`.`
);
}
let source = "export { ";
for (let i = 0; i < length; i++) {
source += String.fromCharCode(0x4e00 + i) + ",";
}
return source + " } from './foo'";
}
function benchCases(name, implementation, options) {
for (const length of [256, 512, 1024, 2048]) {
const input = createInput(length);
suite.add(`${name} ${length} length-1 named export`, () => {
implementation.parse(input, options);
});
}
}
benchCases("baseline", baseline, { sourceType: "module" });
benchCases("current", current, { sourceType: "module" });
suite.on("cycle", report).run();

View File

@ -33,7 +33,7 @@
"node": ">=6.0.0" "node": ">=6.0.0"
}, },
"devDependencies": { "devDependencies": {
"@babel-baseline/parser": "npm:@babel/parser@^7.14.0", "@babel-baseline/parser": "npm:@babel/parser@^7.14.4",
"@babel/code-frame": "workspace:*", "@babel/code-frame": "workspace:*",
"@babel/helper-fixtures": "workspace:*", "@babel/helper-fixtures": "workspace:*",
"@babel/helper-validator-identifier": "workspace:*", "@babel/helper-validator-identifier": "workspace:*",

View File

@ -18,6 +18,9 @@ export default class BaseParser {
declare expressionScope: ExpressionScopeHandler; declare expressionScope: ExpressionScopeHandler;
declare plugins: PluginsMap; declare plugins: PluginsMap;
declare filename: ?string; declare filename: ?string;
// Names of exports store. `default` is stored as a name for both
// `export default foo;` and `export { foo as default };`.
declare exportedIdentifiers: Set<string>;
sawUnambiguousESM: boolean = false; sawUnambiguousESM: boolean = false;
ambiguousScriptDifferentAst: boolean = false; ambiguousScriptDifferentAst: boolean = false;

View File

@ -2114,7 +2114,7 @@ export default class StatementParser extends ExpressionParser {
| N.ExportDefaultSpecifier, | N.ExportDefaultSpecifier,
name: string, name: string,
): void { ): void {
if (this.state.exportedIdentifiers.indexOf(name) > -1) { if (this.exportedIdentifiers.has(name)) {
this.raise( this.raise(
node.start, node.start,
name === "default" name === "default"
@ -2123,7 +2123,7 @@ export default class StatementParser extends ExpressionParser {
name, name,
); );
} }
this.state.exportedIdentifiers.push(name); this.exportedIdentifiers.add(name);
} }
// Parses a comma-separated list of module exports. // Parses a comma-separated list of module exports.

View File

@ -349,8 +349,8 @@ export default class UtilParser extends Tokenizer {
const oldLabels = this.state.labels; const oldLabels = this.state.labels;
this.state.labels = []; this.state.labels = [];
const oldExportedIdentifiers = this.state.exportedIdentifiers; const oldExportedIdentifiers = this.exportedIdentifiers;
this.state.exportedIdentifiers = []; this.exportedIdentifiers = new Set();
// initialize scopes // initialize scopes
const oldInModule = this.inModule; const oldInModule = this.inModule;
@ -372,7 +372,7 @@ export default class UtilParser extends Tokenizer {
return () => { return () => {
// Revert state // Revert state
this.state.labels = oldLabels; this.state.labels = oldLabels;
this.state.exportedIdentifiers = oldExportedIdentifiers; this.exportedIdentifiers = oldExportedIdentifiers;
// Revert scopes // Revert scopes
this.inModule = oldInModule; this.inModule = oldInModule;

View File

@ -148,10 +148,6 @@ export default class State {
// after a non-directive is parsed // after a non-directive is parsed
strictErrors: Map<number, ErrorTemplate> = new Map(); strictErrors: Map<number, ErrorTemplate> = new Map();
// Names of exports store. `default` is stored as a name for both
// `export default foo;` and `export { foo as default };`.
exportedIdentifiers: Array<string> = [];
// Tokens length in token store // Tokens length in token store
tokensLength: number = 0; tokensLength: number = 0;

View File

@ -5,12 +5,12 @@ __metadata:
version: 4 version: 4
cacheKey: 7 cacheKey: 7
"@babel-baseline/parser@npm:@babel/parser@^7.14.0, npm-babel-parser@npm:@babel/parser@^7.14.0": "@babel-baseline/parser@npm:@babel/parser@^7.14.4":
version: 7.14.0 version: 7.14.4
resolution: "@babel/parser@npm:7.14.0" resolution: "@babel/parser@npm:7.14.4"
bin: bin:
parser: ./bin/babel-parser.js parser: ./bin/babel-parser.js
checksum: ef6165f3038b9f8761a02768ab14034f4935baf2e050a2924aa093f54e3164732bce7fee81b3c8ff3be03048091e4ea208a72b23a3715debf7fd06b79495c9e9 checksum: 3bc067c1ee0e0178d365e1b2988ea1a0d6d37af37870ea1a7e80729b3bdc40acda083cac44ce72f63a5b31a489e35120f617bd41f312dec4c86cf814cff8e64a
languageName: node languageName: node
linkType: hard linkType: hard
@ -963,7 +963,7 @@ __metadata:
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@babel/parser@workspace:packages/babel-parser" resolution: "@babel/parser@workspace:packages/babel-parser"
dependencies: dependencies:
"@babel-baseline/parser": "npm:@babel/parser@^7.14.0" "@babel-baseline/parser": "npm:@babel/parser@^7.14.4"
"@babel/code-frame": "workspace:*" "@babel/code-frame": "workspace:*"
"@babel/helper-fixtures": "workspace:*" "@babel/helper-fixtures": "workspace:*"
"@babel/helper-validator-identifier": "workspace:*" "@babel/helper-validator-identifier": "workspace:*"
@ -11641,6 +11641,15 @@ fsevents@^1.2.7:
languageName: node languageName: node
linkType: hard linkType: hard
"npm-babel-parser@npm:@babel/parser@^7.14.0":
version: 7.14.0
resolution: "@babel/parser@npm:7.14.0"
bin:
parser: ./bin/babel-parser.js
checksum: ef6165f3038b9f8761a02768ab14034f4935baf2e050a2924aa093f54e3164732bce7fee81b3c8ff3be03048091e4ea208a72b23a3715debf7fd06b79495c9e9
languageName: node
linkType: hard
"npm-run-path@npm:^2.0.0": "npm-run-path@npm:^2.0.0":
version: 2.0.2 version: 2.0.2
resolution: "npm-run-path@npm:2.0.2" resolution: "npm-run-path@npm:2.0.2"