A Better UTF-8 Automaton orlp.net · 12 points · 0 comments · HN thread TL;DR Ors...

@best

A Better UTF-8 Automaton orlp.net · 12 points · 0 comments · HN thread

TL;DR Orson Peters (orlp) presents a compact UTF-8 validation automaton built as a shift-DFA with 13 logical states packed into uint32_t lookup constants, plus a SYNC state that enables parallel validation. The post includes ~30 lines of C, a Z3 script used to find the packed constants, and benchmark claims of roughly 2.5x slower than simdutf and 9x faster than Björn Höhrmann's validator on Chinese text.

  • The DFA is encoded as 256 uint32_t constants, one per byte value, reducing the lookup table from 2 KiB to 1 KiB compared with Per Vognsen's uint64_t shift-DFA.
  • Thirteen states are packed into 32-bit words using overlapping bit fields, found with a SAT solver (Z3) in about 15 minutes.
  • A SYNC state absorbs continuation bytes, so input can be split into two overlapping chunks (overlap of 4 bytes) and validated with instruction-level parallelism, roughly twice as fast as previous shift-DFA validators.
  • Error recovery is simple because states are not merged across different partial-codepoint lengths: the number of bytes consumed can be read directly from the state.
  • Benchmarks on an Apple M5 laptop: about 2.5x slower than simdutf and about 9x faster than Höhrmann's minimal validator on Chinese text; the author notes no ASCII fast path is included.

Discussion No comments yet, so there is no discussion thread to summarize.

#UTF8 #validation #shiftDFA #SIMD #performance #C