aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLParser.cpp
diff options
context:
space:
mode:
authorGravatar Brian Osman <brianosman@google.com>2017-08-15 11:14:30 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-08-15 15:38:26 +0000
commit634624a1850b335aa7cdfd6aec02e687ee8571a5 (patch)
tree010aed2c4aec9cda27dc4493748f707c77b0cd4e /src/sksl/SkSLParser.cpp
parent4009668cb828b2a0647b8316fa00af2980cf21d3 (diff)
Micro optimizations to SkSL parser
Some of these are pretty trivial, but a few really moved the needle. In total, this cut the time in the parser by about 10% in my profiling. The early returns from peek and checkNext are probably the biggest wins, everything else is mostly about cutting down on string copying. Bug: skia: Change-Id: I3da0a0cfbc557e8cef480cb3a7d301c2b3f8802a Reviewed-on: https://skia-review.googlesource.com/34720 Reviewed-by: Ethan Nicholas <ethannicholas@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
Diffstat (limited to 'src/sksl/SkSLParser.cpp')
-rw-r--r--src/sksl/SkSLParser.cpp63
1 files changed, 35 insertions, 28 deletions
diff --git a/src/sksl/SkSLParser.cpp b/src/sksl/SkSLParser.cpp
index d3e1d5712c..f712b34e1a 100644
--- a/src/sksl/SkSLParser.cpp
+++ b/src/sksl/SkSLParser.cpp
@@ -150,9 +150,9 @@ std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() {
Token Parser::nextRawToken(bool needText) {
if (fPushback.fKind != Token::INVALID_TOKEN) {
- Token result = fPushback;
+ Token result(std::move(fPushback));
fPushback.fKind = Token::INVALID_TOKEN;
- fPushback.fText = "";
+ fPushback.fText.clear();
return result;
}
Token::Kind kind = (Token::Kind) sksllex(fScanner);
@@ -187,11 +187,16 @@ void Parser::pushback(Token t) {
}
Token Parser::peek() {
- fPushback = this->nextToken();
+ if (fPushback.fKind == Token::INVALID_TOKEN) {
+ fPushback = this->nextToken();
+ }
return fPushback;
}
bool Parser::checkNext(Token::Kind kind, Token* result) {
+ if (fPushback.fKind != Token::INVALID_TOKEN && fPushback.fKind != kind) {
+ return false;
+ }
Token next = this->nextToken();
if (next.fKind == kind) {
if (result) {
@@ -204,20 +209,16 @@ bool Parser::checkNext(Token::Kind kind, Token* result) {
}
bool Parser::expect(Token::Kind kind, const char* expected, Token* result) {
- return this->expect(kind, String(expected), result);
-}
-
-bool Parser::expect(Token::Kind kind, String expected, Token* result) {
Token next = this->nextToken();
if (next.fKind == kind) {
if (result) {
- *result = next;
+ *result = std::move(next);
}
return true;
} else {
if (next.fText.size()) {
- this->error(next.fPosition, "expected " + expected + ", but found '" + next.fText +
- "'");
+ this->error(next.fPosition, "expected " + String(expected) + ", but found '" +
+ next.fText + "'");
} else {
this->error(next.fPosition, "parse error, recompile in debug mode for details");
}
@@ -233,7 +234,7 @@ void Parser::error(Position p, String msg) {
fErrors.error(p, msg);
}
-bool Parser::isType(String name) {
+bool Parser::isType(const String& name) {
return nullptr != fTypes[name];
}
@@ -409,7 +410,7 @@ std::unique_ptr<ASTDeclaration> Parser::declaration() {
std::move(parameters),
std::move(body)));
} else {
- return this->varDeclarationEnd(modifiers, std::move(type), name.fText);
+ return this->varDeclarationEnd(modifiers, std::move(type), std::move(name.fText));
}
}
@@ -994,7 +995,8 @@ std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(Modifiers mods) {
}
this->expect(Token::SEMICOLON, "';'");
return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fPosition, mods,
- name.fText, std::move(decls),
+ std::move(name.fText),
+ std::move(decls),
std::move(instanceName.fText),
std::move(sizes)));
}
@@ -1361,7 +1363,7 @@ std::unique_ptr<ASTExpression> Parser::commaExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1397,7 +1399,7 @@ std::unique_ptr<ASTExpression> Parser::assignmentExpression() {
return nullptr;
}
result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(std::move(result),
- t,
+ std::move(t),
std::move(right)));
}
default:
@@ -1440,7 +1442,7 @@ std::unique_ptr<ASTExpression> Parser::logicalOrExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1457,7 +1459,7 @@ std::unique_ptr<ASTExpression> Parser::logicalXorExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1474,7 +1476,7 @@ std::unique_ptr<ASTExpression> Parser::logicalAndExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1491,7 +1493,7 @@ std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1508,7 +1510,7 @@ std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1525,7 +1527,7 @@ std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
}
return result;
}
@@ -1545,7 +1547,7 @@ std::unique_ptr<ASTExpression> Parser::equalityExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
break;
}
default:
@@ -1571,7 +1573,8 @@ std::unique_ptr<ASTExpression> Parser::relationalExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
+ std::move(right)));
break;
}
default:
@@ -1595,7 +1598,8 @@ std::unique_ptr<ASTExpression> Parser::shiftExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
+ std::move(right)));
break;
}
default:
@@ -1619,7 +1623,8 @@ std::unique_ptr<ASTExpression> Parser::additiveExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
+ std::move(right)));
break;
}
default:
@@ -1644,7 +1649,8 @@ std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() {
if (!right) {
return nullptr;
}
- result.reset(new ASTBinaryExpression(std::move(result), t, std::move(right)));
+ result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
+ std::move(right)));
break;
}
default:
@@ -1667,7 +1673,8 @@ std::unique_ptr<ASTExpression> Parser::unaryExpression() {
if (!expr) {
return nullptr;
}
- return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(t, std::move(expr)));
+ return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(std::move(t),
+ std::move(expr)));
}
default:
return this->postfixExpression();
@@ -1846,7 +1853,7 @@ bool Parser::boolLiteral(bool* dest) {
bool Parser::identifier(String* dest) {
Token t;
if (this->expect(Token::IDENTIFIER, "identifier", &t)) {
- *dest = t.fText;
+ *dest = std::move(t.fText);
return true;
}
return false;