update benchmark

This commit is contained in:
Siarhei Fedartsou 2022-10-02 23:18:35 +02:00
parent 32deb11cfb
commit cbc08688b3
2 changed files with 12 additions and 13 deletions

View File

@ -32,19 +32,20 @@ template <typename Out> struct Renderer
write('"');
// here we assume that vast majority of strings don't need to be escaped,
// so we check it first and escape only if needed
auto size = SizeOfEscapedJSONString(string.value);
if (size == string.value.size())
{
write(string.value);
}
else
if (RequiresJSONStringEscaping(string.value))
{
std::string escaped;
escaped.reserve(size);
// just a guess that 16 bytes for escaped characters will be enough to avoid
// reallocations
escaped.reserve(string.value.size() + 16);
EscapeJSONString(string.value, escaped);
write(escaped);
}
else
{
write(string.value);
}
write('"');
}

View File

@ -58,9 +58,8 @@ template <int length, int precision> char *printInt(char *buffer, int value)
return buffer;
}
inline size_t SizeOfEscapedJSONString(const std::string &string)
inline bool RequiresJSONStringEscaping(const std::string &string)
{
size_t size = 0;
for (const char letter : string)
{
switch (letter)
@ -73,13 +72,12 @@ inline size_t SizeOfEscapedJSONString(const std::string &string)
case '\n':
case '\r':
case '\t':
size += 2;
break;
return true;
default:
size += 1;
continue;
}
}
return size;
return false;
}
inline void EscapeJSONString(const std::string &input, std::string &output)