浏览代码

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%.
Andreas Kling 6 年之前
父节点
当前提交
4e004a664f
共有 2 个文件被更改,包括 14 次插入1 次删除
  1. 12 1
      AK/JsonParser.cpp
  2. 2 0
      AK/JsonParser.h

+ 12 - 1
AK/JsonParser.cpp

@@ -92,7 +92,18 @@ String JsonParser::consume_quoted_string()
         }
         }
     }
     }
     consume_specific('"');
     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()
 JsonObject JsonParser::parse_object()

+ 2 - 0
AK/JsonParser.h

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