JsonParser: Use Vector<char, 1024> instead of StringBuilder in parsing

This is a 10-12% speedup on the 4chan thread catalog JSON.
This commit is contained in:
Andreas Kling 2019-08-04 10:38:15 +02:00
parent e38f78faf5
commit c55129e573
Notes: sideshowbarker 2024-07-19 12:54:20 +09:00

View file

@ -1,7 +1,6 @@
#include <AK/JsonArray.h> #include <AK/JsonArray.h>
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/JsonParser.h> #include <AK/JsonParser.h>
#include <AK/StringBuilder.h>
namespace AK { namespace AK {
@ -34,10 +33,10 @@ void JsonParser::consume_while(C condition)
template<typename C> template<typename C>
String JsonParser::extract_while(C condition) String JsonParser::extract_while(C condition)
{ {
StringBuilder builder; Vector<char, 1024> buffer;
while (condition(peek())) while (condition(peek()))
builder.append(consume()); buffer.append(consume());
return builder.to_string(); return String::copy(buffer);
}; };
void JsonParser::consume_whitespace() void JsonParser::consume_whitespace()
@ -54,13 +53,13 @@ void JsonParser::consume_specific(char expected_ch)
String JsonParser::consume_quoted_string() String JsonParser::consume_quoted_string()
{ {
consume_specific('"'); consume_specific('"');
StringBuilder builder; Vector<char, 1024> buffer;
for (;;) { for (;;) {
char ch = peek(); char ch = peek();
if (ch == '"') if (ch == '"')
break; break;
if (ch != '\\') { if (ch != '\\') {
builder.append(consume()); buffer.append(consume());
continue; continue;
} }
consume(); consume();
@ -68,16 +67,16 @@ String JsonParser::consume_quoted_string()
switch (escaped_ch) { switch (escaped_ch) {
case 'n': case 'n':
case 'r': case 'r':
builder.append('\n'); buffer.append('\n');
break; break;
case 't': case 't':
builder.append('\t'); buffer.append('\t');
break; break;
case 'b': case 'b':
builder.append('\b'); buffer.append('\b');
break; break;
case 'f': case 'f':
builder.append('\f'); buffer.append('\f');
break; break;
case 'u': case 'u':
consume(); consume();
@ -85,15 +84,15 @@ String JsonParser::consume_quoted_string()
consume(); consume();
consume(); consume();
// FIXME: This is obviously not correct, but we don't have non-ASCII support so meh. // FIXME: This is obviously not correct, but we don't have non-ASCII support so meh.
builder.append("?"); buffer.append('?');
break; break;
default: default:
builder.append(escaped_ch); buffer.append(escaped_ch);
break; break;
} }
} }
consume_specific('"'); consume_specific('"');
return builder.to_string(); return String::copy(buffer);
} }
JsonValue JsonParser::parse_object() JsonValue JsonParser::parse_object()