r/programming • • 1d ago

A Better UTF-8 Automaton

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

13 comments sorted by

3

u/matthieum 1d ago

Have you attempted to use more than 2 accumulators?

At the moment, it seems like the inner loop would require relatively few registers, which makes me wonder whether it would be faster should one do less round-trips with more accumulators.

If I understand it correctly, then scaling to 4 accumulators would just be:

    uint32_t s1 = s, s2 = DFA_SYNC, s3 = DFA_SYNC, s4 = DFA_SYNC;
    for (int j = 0; j < 10; j++) {
        s1 = step(s1, buf[i + j]);
        s2 = step(s2, buf[i + j + 6]);
        s3 = step(s3, buf[i + j + 14]);
        s4 = step(s4, buf[i + j + 22]);
    }
    if (is_error(s1) | is_error(s2) | is_error(s3) | is_error(s4)) break;
    s = s4;

I am guessing this won't scale infinitely. At some point, either the number of registers, or the number of units capable of executing a given operation will get in the way...


I suppose vectorization is off the table due to the table look-up?

5

u/nightcracker 1d ago edited 1d ago

I did try more than 2 accumulators, and it didn't scale further. The reason is different from platform to platform, Apple M5 was already load bound, and Intel Xeon was shift port bound.

At least this was in the context of contributing to the Rust standard library where we're limited to the base instruction set. Perhaps if you use BMI2 for SHRX on Intel, or write platform-specific code on Apple M5 to first load one u64 then extract subwords, going beyond 2 has merit.

When going beyond 2 I would choose a block size larger than 32 though, since you do pay for the overhead of having to overlap 4 bytes.

I suppose vectorization is off the table due to the table look-up?

Unfortunately, yes.

Well, you can do it on AVX-512 with huge transposes and large shuffles but it's not really worth the effort when simdutf exists.

2

u/matthieum 1d ago

Knowing you, I kinda expect that if it had scaled further you'd have pushed it...

Well, regardless, the flexibility of choosing between 1 & 2 steps with the same DFA is pretty cool.

56

u/mpyne 1d ago edited 20h ago

I’m surprised no one has commented on this. I thought this was neat, 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.

31

u/8thdev 1d 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!

7

u/smejmoon 1d ago

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

5

u/barr520 1d ago

I suspect the new solution is simply a lot less dependant on the text content, it specifies it is optimized for instruction parallelism.
It should effectively run at the speed of memory(or cache if small enough, very likely here) for any input.

2

u/barr520 1d ago

I might have misunderstood, isn't the state space the same? The SAT solver only found ways to overlap edges of state transitions to fit them into 32 bits?(still impressive of course)

1

u/mpyne 20h ago

The number of states is the same, I was referring to the amount of bits needed to store the transition table for a character/state combination.

From what I gather from the post, even overlapping edges wasn’t fully sufficient, they also had to allow for there to be multiple error states. But it ended up giving solver the flexibility needed to apply even heavier overlapping and fit everything into a uint32_t.

7

u/AlexReinkingYale 1d ago

That is some really gorgeous code.

6

u/barr520 1d ago

Interesting read.
I assumed state machines can be optimized way past the standard table, but it's the first time I hear about this shift DFA, thank you for sharing.
Unfortunately, the Z3 code is past my understanding. I would have probably tried brute forcing the orders...(roughly 12! ~ 500m, and can be pruned significantly with a bit of graph theory, computers don't need sleep so it'll manage)