Skip to content

Commit 4f13d80

Browse files
perf(js): memoize a word's reserved kind so plain identifiers skip checkUnreserved
Every identifier ran checkUnreserved's length and first-char gates and, for the short lowercase majority, a Map lookup. The word cache already classifies each word once; it now records the reserved kind beside the token type, and parseIdent skips the check when it is zero.
1 parent f97857e commit 4f13d80

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

‎lib/javascript/syntax.js‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,7 @@ class LazyComment {
17401740
* _importPhasesEnabled: boolean,
17411741
* _lazyComments: CollectedComment[] | undefined,
17421742
* _newlineBefore: 0 | 1 | 2,
1743+
* _wordKind: number,
17431744
* _fullTokenFastPath: boolean,
17441745
* _stmtFastPath: boolean,
17451746
* _lastArrow: Expression | null,
@@ -1925,6 +1926,19 @@ const wordMatches = (input, start, cached, wordLen) => {
19251926
return true;
19261927
};
19271928

1929+
/**
1930+
* The reserved kind `checkUnreserved` acts on for a word, 0 when it has nothing
1931+
* to check: pure in (word, lookups), so it is memoized beside the token type.
1932+
* @param {string} word word
1933+
* @param {WordLookups} lookups the parser's keyword sets
1934+
* @returns {number} the reserved kind, 4 for the names with their own checks
1935+
*/
1936+
const wordKind = (word, lookups) => {
1937+
const kind = lookups.reservedKinds.get(word);
1938+
if (kind !== undefined) return kind;
1939+
return word === "arguments" || word === "await" || word === "yield" ? 4 : 0;
1940+
};
1941+
19281942
/**
19291943
* Keyword-or-name classification: pure in (word, keywords), so it can be
19301944
* memoized per `WORD_CACHE` slot.
@@ -2351,6 +2365,7 @@ class WebpackParser extends BaseParser {
23512365
self._fullTokenFastPath = shape.ecmaVersion >= 12;
23522366
self._inlineFinish = self.finishToken === proto.finishToken;
23532367
self._newlineBefore = 2;
2368+
self._wordKind = -1;
23542369
self._deStack = [];
23552370
self._deDepth = 0;
23562371
self._propHashFastPath = self.checkPropClash === proto.checkPropClash;
@@ -2540,6 +2555,7 @@ class WebpackParser extends BaseParser {
25402555
// 0 no, 1 yes, 2 unknown (canInsertSemicolon then scans the gap)
25412556
/** @type {0 | 1 | 2} */
25422557
this._newlineBefore = 2;
2558+
this._wordKind = -1;
25432559
// LIFO pool for call-scoped destructuring-errors records; depth resets
25442560
// implicitly since a raise aborts the whole parse
25452561
/** @type {DestructuringErrorsShim[]} */
@@ -5077,17 +5093,20 @@ class WebpackParser extends BaseParser {
50775093
word = cached;
50785094
if (WORD_CACHE[slot + 2] === owner) {
50795095
type = /** @type {TokenType} */ (WORD_CACHE[slot + 1]);
5096+
this._wordKind = /** @type {number} */ (WORD_CACHE[slot + 3]);
50805097
} else {
50815098
type = classifyWord(cached, lookups.keywords);
50825099
WORD_CACHE[slot + 1] = type;
50835100
WORD_CACHE[slot + 2] = owner;
5101+
this._wordKind = WORD_CACHE[slot + 3] = wordKind(cached, lookups);
50845102
}
50855103
} else {
50865104
word = input.slice(start, pos);
50875105
WORD_CACHE[slot] = word;
50885106
type = classifyWord(word, lookups.keywords);
50895107
WORD_CACHE[slot + 1] = type;
50905108
WORD_CACHE[slot + 2] = owner;
5109+
this._wordKind = WORD_CACHE[slot + 3] = wordKind(word, lookups);
50915110
}
50925111
if (type === tokTypes.name && this._inlineFinish) {
50935112
// finishToken(name) inlined: offset/type/value stores plus
@@ -5106,7 +5125,8 @@ class WebpackParser extends BaseParser {
51065125
return this.finishToken(type, word);
51075126
}
51085127
// Every keyword is 2-10 chars, so a word outside the cacheable range is a
5109-
// plain name and can be neither `of` nor `yield`.
5128+
// plain name and can be neither `of` nor `yield`, nor reserved.
5129+
this._wordKind = 0;
51105130
if (this._inlineFinish) {
51115131
this.end = pos;
51125132
this.type = tokTypes.name;
@@ -5123,6 +5143,7 @@ class WebpackParser extends BaseParser {
51235143
* @returns {void}
51245144
*/
51255145
_finishWordSlow(word) {
5146+
this._wordKind = -1;
51265147
this.finishToken(classifyWord(word, this._wordLookups.keywords), word);
51275148
}
51285149

@@ -5491,9 +5512,11 @@ class WebpackParser extends BaseParser {
54915512
const node = /** @type {Identifier} */ (
54925513
/** @type {unknown} */ (new IdentifierNode(this.start, this.end, name))
54935514
);
5515+
// the reserved kind readWord memoized for this token, before next() replaces it
5516+
const kind = this._wordKind;
54945517
this.next(Boolean(liberal));
54955518
if (!liberal) {
5496-
this.checkUnreserved(node);
5519+
if (kind !== 0) this.checkUnreserved(node);
54975520
if (node.name === "await" && !this.awaitIdentPos) {
54985521
this.awaitIdentPos = node.start;
54995522
}

0 commit comments

Comments
 (0)