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

+4 -3
View File
@@ -206,9 +206,10 @@ places, which differ in speed, diagnostics, and how they read the input:
the bytes as is and leave the check to dump() and the binary writers.
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
diagnostic for each kind of error.
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
They must accept exactly what the lexer's switch accepts.
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
reader, and the BON8 writer. They must accept exactly what the lexer's
switch accepts.
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).