reduced number of string copies in SkSL parser

Bug: skia:
Change-Id: Iab823b01b6f5f85bbb47c4ac69c6ce396dabf497
Reviewed-on: https://skia-review.googlesource.com/34000
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2017-08-14 10:35:37 -04:00 committed by Skia Commit-Bot
parent 10d4244e0a
commit 08b79b73a0
2 changed files with 33 additions and 14 deletions

View File

@ -148,29 +148,42 @@ std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() {
}
}
Token Parser::nextRawToken() {
Token Parser::nextRawToken(bool needText) {
if (fPushback.fKind != Token::INVALID_TOKEN) {
Token result = fPushback;
fPushback.fKind = Token::INVALID_TOKEN;
fPushback.fText = "";
return result;
}
int token = sksllex(fScanner);
return Token(Position(skslget_lineno(fScanner), -1), (Token::Kind) token,
String(skslget_text(fScanner)));
Token::Kind kind = (Token::Kind) sksllex(fScanner);
if (!needText) {
switch (kind) {
case Token::Kind::DIRECTIVE: // fall through
case Token::Kind::IDENTIFIER: // fall through
case Token::Kind::INT_LITERAL: // fall through
case Token::Kind::FLOAT_LITERAL: // fall through
case Token::Kind::SECTION:
needText = true;
default:
break;
}
}
static String unavailable("<unavailable>");
return Token(Position(skslget_lineno(fScanner), -1), kind,
needText ? String(skslget_text(fScanner)) : unavailable);
}
Token Parser::nextToken() {
Token token;
do {
token = this->nextRawToken();
token = this->nextRawToken(false);
} while (token.fKind == Token::WHITESPACE);
return token;
}
void Parser::pushback(Token t) {
ASSERT(fPushback.fKind == Token::INVALID_TOKEN);
fPushback = t;
fPushback = std::move(t);
}
Token Parser::peek() {
@ -186,7 +199,7 @@ bool Parser::checkNext(Token::Kind kind, Token* result) {
}
return true;
}
this->pushback(next);
this->pushback(std::move(next));
return false;
}
@ -315,7 +328,7 @@ std::unique_ptr<ASTDeclaration> Parser::section() {
String text;
int level = 1;
for (;;) {
Token next = this->nextRawToken();
Token next = this->nextRawToken(true);
switch (next.fKind) {
case Token::LBRACE:
++level;
@ -593,12 +606,13 @@ String Parser::layoutCode() {
if (!this->expect(Token::EQ, "'='")) {
return "";
}
Token start = this->peek();
Token start = this->nextRawToken(true);
this->pushback(start);
String code;
int level = 1;
bool done = false;
while (!done) {
Token next = this->peek();
Token next = this->nextRawToken(true);
switch (next.fKind) {
case Token::LPAREN:
++level;
@ -620,8 +634,11 @@ String Parser::layoutCode() {
if (!level) {
done = true;
}
if (!done) {
code += this->nextRawToken().fText;
if (done) {
this->pushback(std::move(next));
}
else {
code += next.fText;
}
}
return code;

View File

@ -64,9 +64,11 @@ public:
private:
/**
* Return the next token, including whitespace tokens, from the parse stream.
* Return the next token, including whitespace tokens, from the parse stream. If needText is
* false, the token's text is only filled in if it is a token with variable text (identifiers,
* numbers, etc.).
*/
Token nextRawToken();
Token nextRawToken(bool needText);
/**
* Return the next non-whitespace token from the parse stream.