Skip to content

Commit

Permalink
Hit the table once for whitespace, then tight-loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser committed Jul 3, 2024
1 parent 3bdfe84 commit 33ae58d
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1997,19 +1997,6 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean

const ch = codePointUnchecked(pos);

if (ch === CharacterCodes.tab || ch === CharacterCodes.space) {
if (skipTrivia) {
pos++;
continue;
}
else {
while (pos < end && isWhiteSpaceSingleLine(charCodeUnchecked(pos))) {
pos++;
}
return token = SyntaxKind.WhitespaceTrivia;
}
}

if (ch === CharacterCodes.lineFeed || ch === CharacterCodes.carriageReturn) {
tokenFlags |= TokenFlags.PrecedingLineBreak;
if (skipTrivia) {
Expand Down Expand Up @@ -2044,14 +2031,23 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}

if (tokenCategory & TokenCategory.Whitespace) {
pos++;
// Tight loop here on consecutive whitespace to avoid a table lookup.
while (pos < end) {
const nextCh = charCodeUnchecked(pos);
// Check for the original character to hitting the slow path.
if (nextCh === ch || isWhiteSpaceSingleLine(nextCh)) {
pos++;
continue;
}

break;
}

if (skipTrivia) {
pos++;
continue;
}
else {
while (pos < end && isWhiteSpaceSingleLine(charCodeUnchecked(pos))) {
pos++;
}
return token = SyntaxKind.WhitespaceTrivia;
}
}
Expand Down

0 comments on commit 33ae58d

Please sign in to comment.