Skip to content

Commit fbc22bb

Browse files
committed
[JSC] Create a save point only if actually checking for "use strict"
https://bugs.webkit.org/show_bug.cgi?id=317599 rdar://180322283 Reviewed by Yusuke Suzuki. In the existing code, Parser::parseSourceElements unconditionally creates a save point. A save point is only necessary if shouldCheckForUseStrict is true, and in the majority of cases (parsing statement blocks), that flag is false and the save point is unused. Creating a save point involves copying almost 100 bytes of parser and lexer state, so let's only do that when necessary. Covered by existing tests. Canonical link: https://commits.webkit.org/315637@main
1 parent 1851699 commit fbc22bb

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

Source/JavaScriptCore/parser/Parser.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,11 @@ template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceEl
408408
TreeSourceElements sourceElements = context.createSourceElements();
409409
const Identifier* directive = nullptr;
410410
unsigned directiveLiteralLength = 0;
411-
auto savePoint = createSavePoint(context);
411+
std::optional<SavePoint> savePoint;
412412
bool shouldCheckForUseStrict = mode == CheckForStrictMode;
413-
413+
if (shouldCheckForUseStrict)
414+
savePoint.emplace(createSavePoint(context));
415+
414416
while (TreeStatement statement = parseStatementListItem(context, directive, &directiveLiteralLength)) {
415417
if (shouldCheckForUseStrict) {
416418
if (directive) {
@@ -429,7 +431,7 @@ template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceEl
429431
semanticFailIfFalse(isValidStrictMode(), "Invalid parameters or function name in strict mode");
430432
}
431433
// Since strict mode is changed, restoring lexer state by calling next() may cause errors.
432-
restoreSavePoint(context, savePoint);
434+
restoreSavePoint(context, *savePoint);
433435
propagateError();
434436
continue;
435437
}
@@ -4495,7 +4497,7 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseConditionalE
44954497
JSTokenLocation location(tokenLocation());
44964498
TreeExpression cond = parseBinaryExpression(context);
44974499
failIfFalse(cond, "Cannot parse expression");
4498-
if (!match(QUESTION))
4500+
if (!match(QUESTION)) [[likely]]
44994501
return cond;
45004502
m_parserState.nonTrivialExpressionCount++;
45014503
m_parserState.nonLHSCount++;
@@ -4562,7 +4564,7 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseBinaryExpres
45624564

45634565
context.appendBinaryExpressionInfo(operandStackDepth, current, exprStart, lastTokenEndPosition(), lastTokenEndPosition(), initialAssignments != m_parserState.assignmentCount);
45644566
int precedence = isBinaryOperator(m_token.m_type);
4565-
if (!precedence)
4567+
if (!precedence) [[likely]]
45664568
break;
45674569

45684570
// 12.6 https://tc39.github.io/ecma262/#sec-exp-operator

0 commit comments

Comments
 (0)