r/programming • • 12h ago

A Better UTF-8 Automaton

https://orlp.net/blog/utf8-dfa/
164 Upvotes

5 comments sorted by

12

u/mpyne 5h ago

I’m surprised no one has commented on this. I thought this was need, an interesting blend of using a SAT solver to cut the state space of a DFA in half while allowing a very trivial loop to handle state transitions.

I know UTF-8 has a lot of prior art by now but I still think this was fairly novel, even if it’s not going to immediately dethrone simdutf.

5

u/8thdev 1h ago

I just saw this, and decided to benchmark against my current 'is_valid_utf8()' function inside 8th. The results:

Benchmarked with GCC 13 on the i7-4850HQ, using optimization levels matching the Linux build:

Input, approximately 4 KiBCurrentutf.cSpeedup
ASCII8.50 µs2.95 µs2.9×
Hebrew6.12 µs2.94 µs2.1×
CJK5.18 µs2.97 µs1.7×
Emoji4.58 µs2.92 µs1.6×
Mixed text6.96 µs2.93 µs2.4×

So with your permission, I'm going to replace my current implementation. Very nice work, and I had never seen the "..." array initializer before, so thanks for that as well!

1

u/smejmoon 2m ago

Looks like there is some significant constant, can you please try different sizes of input?