diff options
author | Evan Martin <martine@danga.com> | 2011-09-09 22:52:36 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2011-09-09 22:52:36 (GMT) |
commit | 50489a5160425bb68d5c57fbaa352a5355c2254b (patch) | |
tree | b2b8e763ba4441c63e07f806a54213483333200a /src | |
parent | 085572d523b3d1bb40666dfd7a4a93dd1f0d376f (diff) | |
download | Ninja-50489a5160425bb68d5c57fbaa352a5355c2254b.zip Ninja-50489a5160425bb68d5c57fbaa352a5355c2254b.tar.gz Ninja-50489a5160425bb68d5c57fbaa352a5355c2254b.tar.bz2 |
optimize IsIdentChar
Rather than nested tests, use a table. Shaves 200ms off Chrome null startup.
Diffstat (limited to 'src')
-rw-r--r-- | src/parsers.cc | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/parsers.cc b/src/parsers.cc index 35790b2..849a3e1 100644 --- a/src/parsers.cc +++ b/src/parsers.cc @@ -102,12 +102,32 @@ bool Tokenizer::Newline(string* err) { return true; } +/// Return true if |c| is part of an identifier. static bool IsIdentChar(char c) { - return - ('a' <= c && c <= 'z') || - ('+' <= c && c <= '9') || // +,-./ and numbers - ('A' <= c && c <= 'Z') || - (c == '_') || (c == '$') || (c == '\\'); + // This function shows up hot on profiles. Instead of the natural + // 'if' statement, use a table as generated by this Python script: + // import string + // cs = set() + // for c in string.ascii_letters + string.digits + r'+,-./\_$': + // cs.add(ord(c)) + // for i in range(128): + // if i in cs: + // print '1,', + // else: + // print '0,', + // if i % 16 == 15: + // print + static const bool kIdents[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, + }; + return kIdents[(int)c]; } bool Tokenizer::ExpectToken(Token::Type expected, string* err) { |