JsonParser: Cache the last seen string starting with each possible char

Keep a 256-entry string cache during parse to avoid creating some new
strings when possible. This cache is far from perfect but very cheap.
Since none of the strings are transient, this only costs us a couple of
pointers and a bit of ref-count manipulation.

The cache hit rate on 4chan_catalog.json is ~33% and the speedup on
the load_4chan_catalog benchmark is ~7%.
This commit is contained in:
Andreas Kling 2019-08-04 18:22:41 +02:00
parent a8740f82eb
commit 4e004a664f
2 changed files with 14 additions and 1 deletions

View file

@ -92,7 +92,18 @@ String JsonParser::consume_quoted_string()
}
}
consume_specific('"');
return String::copy(buffer);
if (buffer.is_empty())
return {};
auto& last_string_starting_with_character = m_last_string_starting_with_character[buffer.first()];
if (last_string_starting_with_character.length() == buffer.size()) {
if (!memcmp(last_string_starting_with_character.characters(), buffer.data(), buffer.size()))
return last_string_starting_with_character;
}
last_string_starting_with_character = String::copy(buffer);
return last_string_starting_with_character;
}
JsonObject JsonParser::parse_object()

View file

@ -40,6 +40,8 @@ private:
StringView m_input;
int m_index { 0 };
String m_last_string_starting_with_character[256];
};
}