Reviewers: Yang,
Message:
PTAL
Description:
Speed up identifier, keyword and smi parsing
BUG=
Please review this at https://codereview.chromium.org/969353003/
Base URL: https://chromium.googlesource.com/v8/v8.git@master
Affected files (+66, -32 lines):
M src/scanner.h
M src/scanner.cc
Index: src/scanner.cc
diff --git a/src/scanner.cc b/src/scanner.cc
index
1d41cb59312039b22954a562d9328eddff6b75e6..bac6f8f70ff0da23f00779755b89e751c61f1c77
100644
--- a/src/scanner.cc
+++ b/src/scanner.cc
@@ -983,15 +983,21 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
int value = 0;
while (IsDecimalDigit(c0_)) {
value = 10 * value + (c0_ - '0');
- AddLiteralCharAdvance();
+
+ uc32 first_char = c0_;
+ Advance<false, false>();
+ AddLiteralChar(first_char);
}
if (next_.literal_chars->one_byte_literal().length() < 10 &&
c0_ != '.' && c0_ != 'e' && c0_ != 'E') {
smi_value_ = value;
literal.Complete();
+ HandleLeadSurrugate();
+
return Token::SMI;
}
+ HandleLeadSurrugate();
}
ScanDecimalDigits(); // optional
@@ -1193,8 +1199,55 @@ bool Scanner::IdentifierIsFutureStrictReserved(
Token::Value Scanner::ScanIdentifierOrKeyword() {
DCHECK(unicode_cache_->IsIdentifierStart(c0_));
LiteralScope literal(this);
- // Scan identifier start character.
- if (c0_ == '\\') {
+ if (c0_ >= 'a' && c0_ <= 'z') {
+ do {
+ uc32 first_char = c0_;
+ Advance<false, false>();
+ AddLiteralChar(first_char);
+ } while (c0_ >= 'a' && c0_ <= 'z');
+
+ if (IsDecimalDigit(c0_) || (c0_ >= 'A' && c0_ <= 'Z') || c0_ == '_' ||
+ c0_ == '$') {
+ // Identifier starting with lowercase.
+ uc32 first_char = c0_;
+ Advance<false, false>();
+ AddLiteralChar(first_char);
+ while ((c0_ >= 'a' && c0_ <= 'z') || IsDecimalDigit(c0_) ||
+ (c0_ >= 'A' && c0_ <= 'Z') || c0_ == '_' || c0_ == '$') {
+ uc32 first_char = c0_;
+ Advance<false, false>();
+ AddLiteralChar(first_char);
+ }
+ if (c0_ <= unibrow::Latin1::kMaxChar) {
+ literal.Complete();
+ return Token::IDENTIFIER;
+ }
+ } else if (c0_ <= unibrow::Latin1::kMaxChar) {
+ // Only a-z+: could be a keyword or identifier.
+ literal.Complete();
+ Vector<const uint8_t> chars =
next_.literal_chars->one_byte_literal();
+ return KeywordOrIdentifierToken(chars.start(), chars.length(),
+ harmony_scoping_, harmony_modules_,
+ harmony_classes_);
+ }
+
+ HandleLeadSurrugate();
+ } else if ((c0_ >= 'A' && c0_ <= 'Z') || c0_ == '_' || c0_ == '$') {
+ do {
+ uc32 first_char = c0_;
+ Advance<false, false>();
+ AddLiteralChar(first_char);
+ } while ((c0_ >= 'a' && c0_ <= 'z') || (c0_ >= 'A' && c0_ <= 'Z') ||
+ IsDecimalDigit(c0_) || c0_ == '_' || c0_ == '$');
+
+ if (c0_ <= unibrow::Latin1::kMaxChar) {
+ literal.Complete();
+ return Token::IDENTIFIER;
+ }
+
+ HandleLeadSurrugate();
+ } else if (c0_ == '\\') {
+ // Scan identifier start character.
uc32 c = ScanIdentifierUnicodeEscape();
// Only allow legal identifier start characters.
if (c < 0 ||
@@ -1204,36 +1257,13 @@ Token::Value Scanner::ScanIdentifierOrKeyword() {
}
AddLiteralChar(c);
return ScanIdentifierSuffix(&literal);
+ } else {
+ uc32 first_char = c0_;
+ Advance();
+ AddLiteralChar(first_char);
}
- uc32 first_char = c0_;
- Advance();
- AddLiteralChar(first_char);
-
- // Scan the rest of the identifier characters.
- while (c0_ >= 0 && unicode_cache_->IsIdentifierPart(c0_)) {
- if (c0_ != '\\') {
- uc32 next_char = c0_;
- Advance();
- AddLiteralChar(next_char);
- continue;
- }
- // Fallthrough if no longer able to complete keyword.
- return ScanIdentifierSuffix(&literal);
- }
-
- literal.Complete();
-
- if (next_.literal_chars->is_one_byte()) {
- Vector<const uint8_t> chars = next_.literal_chars->one_byte_literal();
- return KeywordOrIdentifierToken(chars.start(),
- chars.length(),
- harmony_scoping_,
- harmony_modules_,
- harmony_classes_);
- }
-
- return Token::IDENTIFIER;
+ return ScanIdentifierSuffix(&literal);
}
Index: src/scanner.h
diff --git a/src/scanner.h b/src/scanner.h
index
0e3013f4b8f7d6a910dcb9e26042b40238c1aaa2..94f9102c042cddbbffe03865e7c533128017179b
100644
--- a/src/scanner.h
+++ b/src/scanner.h
@@ -565,12 +565,16 @@ class Scanner {
}
// Low-level scanning support.
- template <bool capture_raw = false>
+ template <bool capture_raw = false, bool check_surrogate = true>
void Advance() {
if (capture_raw) {
AddRawLiteralChar(c0_);
}
c0_ = source_->Advance();
+ if (check_surrogate) HandleLeadSurrugate();
+ }
+
+ void HandleLeadSurrugate() {
if (unibrow::Utf16::IsLeadSurrogate(c0_)) {
uc32 c1 = source_->Advance();
if (!unibrow::Utf16::IsTrailSurrogate(c1)) {
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.