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.
This commit is contained in:
parent
d3dfd55472
commit
d8269c343c
Notes:
sideshowbarker
2024-07-19 01:52:00 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d8269c343c3
1 changed files with 5 additions and 3 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue