diff options
author | Andreas Kling <kling@serenityos.org> | 2020-10-17 23:44:41 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-17 23:44:41 +0200 |
commit | d8269c343c3c6005452741b0628efc59375c09cc (patch) | |
tree | f0c72e77df916351858bffc6f6ace65cc3c7ec8e /Libraries | |
parent | d3dfd554721aac21c20c7cbe75a98188bf39d29d (diff) | |
download | serenity-d8269c343c3c6005452741b0628efc59375c09cc.zip |
LibJS: Avoid creating temporary Strings to look up tokens while lexing
It would be cool to solve this in a general way so that looking up
a string literal or StringView in a HashMap with String keys avoids
creating a temp string.
For now, this patch simply addresses the issue in JS::Lexer.
This is a 2-3% speed-up on test-js.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/Lexer.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp index fb9760aab9..26a3a65a62 100644 --- a/Libraries/LibJS/Lexer.cpp +++ b/Libraries/LibJS/Lexer.cpp @@ -347,7 +347,7 @@ Token Lexer::next() } while (is_identifier_middle()); StringView value = m_source.substring_view(value_start - 1, m_position - value_start); - auto it = s_keywords.find(value); + auto it = s_keywords.find(value.hash(), [&](auto& entry) { return entry.key == value; }); if (it == s_keywords.end()) { token_type = TokenType::Identifier; } else { @@ -465,7 +465,8 @@ Token Lexer::next() char second_char = m_source[m_position]; char third_char = m_source[m_position + 1]; char three_chars[] { (char)m_current_char, second_char, third_char, 0 }; - auto it = s_three_char_tokens.find(three_chars); + StringView three_chars_view { three_chars }; + auto it = s_three_char_tokens.find(three_chars_view.hash(), [&](auto& entry) { return entry.key == three_chars_view; }); if (it != s_three_char_tokens.end()) { found_three_char_token = true; consume(); @@ -479,7 +480,8 @@ Token Lexer::next() if (!found_four_char_token && !found_three_char_token && m_position < m_source.length()) { char second_char = m_source[m_position]; char two_chars[] { (char)m_current_char, second_char, 0 }; - auto it = s_two_char_tokens.find(two_chars); + StringView two_chars_view = { two_chars }; + auto it = s_two_char_tokens.find(two_chars_view.hash(), [&](auto& entry) { return entry.key == two_chars_view; }); if (it != s_two_char_tokens.end()) { found_two_char_token = true; consume(); |