Validate runs of non-ASCII bytes with a shift-based UTF-8 DFA

The scalar bulk paths validated non-ASCII text one sequence at a time with
validate_one_utf8(). Replace these loops with a shift-based DFA (one table
load and one shift per byte, no branch on the byte):

- utf8_dfa_run() in scalar_string_bulk_run(): the lexer and the serializer
  without SSE2/NEON, and on x86 CPUs without SSSE3. On an error, the exact
  position comes from utf8_dfa_prefix(), so diagnostics are unchanged.
- utf8_dfa_prefix() in valid_utf8_prefix(): the BON8 reader (all builds),
  the BON8 writer and the BSON name check.

Both stop at the first ill-formed byte, so the serializer's error handlers
stay linear. On Apple M, CJK text (poet.json) got 30% faster in the lexer and
serializer without SIMD, and BON8 reading got 15% faster with SIMD.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-30 18:11:18 +01:00
1 parent 322063822f
commit 62b238dce8
4 files changed
+398 -32

No files matched your search

+72
View File
@@ -2138,6 +2138,14 @@ TEST_CASE("UTF-8 validators accept the same sequences")
CAPTURE(t)
CHECK((dfa == expected && bulk == (expected ? length : 0) && bulk_short == 0));
// the DFA of the bulk paths: the whole sequence or nothing
if (length >= 2)
{
const unsigned char* const run_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length);
const unsigned char* const short_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length - 1);
CHECK((run_end == bytes.data() + (expected ? length : 0) && short_end == bytes.data()));
}
if (t < 2 && length >= 2)
{
// the lexer's byte path
@@ -2165,6 +2173,70 @@ TEST_CASE("UTF-8 validators accept the same sequences")
}
}
TEST_CASE("UTF-8 DFA")
{
// utf8_dfa_prefix() and utf8_dfa_run() against a loop over
// validate_one_utf8(): every byte
// after a prefix that leads to each state of the DFA checks every entry of
// its table, and the bytes after it whether it stops at the right place
const auto reference = [](const unsigned char* p, const unsigned char* e)
{
while (p != e && *p >= 0x80u)
{
const std::size_t length = nlohmann::detail::validate_one_utf8(p, static_cast<std::size_t>(e - p));
if (length == 0)
{
break;
}
p += length;
}
return p;
};
// between sequences, after a valid sequence, 1/2/3 continuation bytes
// missing, and after the lead bytes that restrict the second byte
const std::vector<std::vector<unsigned char>> prefixes =
{
{}, {0xC3, 0xA4}, {0xC3}, {0xE1}, {0xF1}, {0xE1, 0x80}, {0xF1, 0x80}, {0xF1, 0x80, 0x80},
{0xE0}, {0xED}, {0xF0}, {0xF4}
};
const std::vector<std::vector<unsigned char>> suffixes =
{
{}, {0x80}, {0xBF}, {0x41}, {0xC3, 0xA4}, {0xE4, 0xB8, 0xAD}, {0xF0, 0x9F, 0x98, 0x80}, {0xFF}
};
for (const auto& prefix : prefixes)
{
for (unsigned byte = 0; byte <= 0xFF; ++byte)
{
for (const auto& suffix : suffixes)
{
std::vector<unsigned char> input(prefix);
input.push_back(static_cast<unsigned char>(byte));
input.insert(input.end(), suffix.begin(), suffix.end());
const unsigned char* const begin = input.data();
const unsigned char* const end = begin + input.size();
CAPTURE(prefix.size())
CAPTURE(byte)
CAPTURE(suffix.size())
const unsigned char* const expected = reference(begin, end);
CHECK(nlohmann::detail::utf8_dfa_prefix(begin, end) - begin == expected - begin);
// utf8_dfa_run() only tells whether the whole run is valid
const bool whole_run = expected == end || *expected < 0x80u;
CHECK(nlohmann::detail::utf8_dfa_run(begin, end) == (whole_run ? expected : nullptr));
}
}
}
// an ill-formed byte stops the scan at once, whatever follows
std::vector<unsigned char> invalid(1000, 0xFF);
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data());
CHECK(nlohmann::detail::utf8_dfa_run(invalid.data(), invalid.data() + invalid.size()) == nullptr);
invalid[0] = 0xC3;
invalid[1] = 0xA4;
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data() + 2);
}
TEST_CASE("float_bigint")
{
// the big integer of the exact comparison of a float token with the