Copy short printable-ASCII strings without starting the scanner (#5878)

dump() now checks strings of up to 32 bytes against a table of the bytes
that can be written verbatim, four bytes per step, and copies them in one
go if all of them can. That is cheaper than setting up the vector scanner,
whose setup costs more than such a string takes to scan; SQLite's
jsonAppendString() writes strings with the same kind of table loop. Most
object keys and many values are this short (citm_catalog averages 8 bytes
per string, jeopardy 14).

GCC 16 and 13 stopped inlining put_indent() and put_literal() into the
pretty-printing loops once dump_escaped() had grown, which made indented
output up to 13% slower; both are now always inlined.

The new test puts every ASCII character and a 2-byte character at every
position of strings of 1 to 40 bytes and compares the output with the
reference implementation of the existing UTF-8 tests.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann authored and GitHub committed 2026-10-21 20:01:28 +02:00
1 parent 52fecc1fab
commit 6aefd8c662
3 files changed
+153 -4

No files matched your search

+21
View File
@@ -1417,6 +1417,27 @@ void check_dump_against_reference(const std::string& s, const bool with_strict =
}
} // namespace
TEST_CASE("every ASCII character at every position of a short string is written like the reference")
{
// dump_escaped() copies strings of up to 32 bytes of printable ASCII after
// a table lookup per byte, and hands everything else to the scanners; put
// each ASCII character and a 2-byte character at every position of
// strings of up to 40 bytes to cover both sides of that bound
for (std::size_t length = 1; length <= 40; ++length)
{
for (std::size_t pos = 0; pos < length; ++pos)
{
for (int c = 0; c < 0x80; ++c)
{
std::string s(length, 'a');
s[pos] = static_cast<char>(c);
check_dump_against_reference(s);
}
check_dump_against_reference(std::string(pos, 'a') + "\xC3\xA4" + std::string(length - pos - 1, 'a'));
}
}
}
TEST_CASE("invalid UTF-8 at every position is handled like the reference")
{
// ill-formed byte sequences; those that end in a truncated sequence are