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:
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?
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.
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.
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 KiB
Current
utf.c
Speedup
ASCII
8.50 µs
2.95 µs
2.9×
Hebrew
6.12 µs
2.94 µs
2.1×
CJK
5.18 µs
2.97 µs
1.7×
Emoji
4.58 µs
2.92 µs
1.6×
Mixed text
6.96 µs
2.93 µs
2.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!
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.
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)
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.
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)
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:
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?
58
u/mpyne 2d ago edited 1d 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.
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)