r/rust • u/modulovalue • 11h ago
📸 media Looking for feedback on a Rust vs. Dart SIMD comparison for ASCII validation
Hello everyone,
I'm working on better support for SIMD in Dart (https://github.com/dart-lang/sdk/issues/64170). My goal is to expose the Wasm SIMD API through a convenient interface which seems to be a shared goal with std::simd.
I plan to publish a blog post where I share techniques on how to arrive at an efficient ASCII validator with SIMD.
I don't write Rust professionally, so I wanted to ask for some feedback to make sure that my comparison against Rust is fair in terms of readability and performance.
To demonstrate auto-vectorized performance I plan to share the following:
I will also include <[u8]>::is_ascii in the comparison, but to show how it could be implemented manually, I plan to demonstrate the performance of std::simd:
use *;
The equivalent Dart API that I'm working toward making possible is:
// The pragmas remove bounds checks and interrupt checks
@pragma('vm:unsafe:no-bounds-checks')
@pragma('vm:unsafe:no-interrupts')
bool u8x16Unroll16(Uint8List b) {
final n = b.length & ~255;
Uint8x16 acc = Uint8x16.zero;
int i = 0;
for (; i < n; i += 256) {
final t0 = Uint8x16.load(b, i + 0) | Uint8x16.load(b, i + 16);
final t1 = Uint8x16.load(b, i + 32) | Uint8x16.load(b, i + 48);
final t2 = Uint8x16.load(b, i + 64) | Uint8x16.load(b, i + 80);
final t3 = Uint8x16.load(b, i + 96) | Uint8x16.load(b, i + 112);
final t4 = Uint8x16.load(b, i + 128) | Uint8x16.load(b, i + 144);
final t5 = Uint8x16.load(b, i + 160) | Uint8x16.load(b, i + 176);
final t6 = Uint8x16.load(b, i + 192) | Uint8x16.load(b, i + 208);
final t7 = Uint8x16.load(b, i + 224) | Uint8x16.load(b, i + 240);
acc |= ((t0 | t1) | (t2 | t3)) | ((t4 | t5) | (t6 | t7));
}
for (; i + 16 <= b.length; i += 16) {
acc |= Uint8x16.load(b, i);
}
int tail = 0;
for (; i < b.length; i++) {
tail |= b[i];
}
return acc.signMask == 0 && tail < 0x80;
}
Any feedback would be appreciated, and if you can make the Rust version faster or more readable, I'd love to see it.
2
u/Shnatsel 2h ago edited 1h ago
Your Rust code looks reasonable, and the fact that its performance tracks
<[u8]>::is_asciifrom the standard library is encouraging. Perhaps remainder handling could be simplified, the fold already autovectorizes so you don't need to handle the part after the large chunks explicitly.I'm not aware of std::simd having anything to do with WebAssembly SIMD in particular. It is meant to be portable and can target WASM, but I don't think anything to do with WASM was an express design goal.
It's also worth noting that SIMD on 64-bit ARM is easy mode, you just get operations on 128-bit vectors working and you're done. To fully utilize recent 64-bit x86 you need a whole lot of other machinery which std::simd itself doesn't provide. I've written a detailed article about this. This doesn't have to be part of your minimum viable product but you should keep that in mind when designing the APIs.
It is unfortunate that Dart requires unsafe pragmas here. In Rust you don't have to unsafely disable bounds checks for std::simd, the optimizer eliminates them for you. A bounds check once per large chunk is probably not too bad, it's worth benchmarking a safe version too.
-1
u/charmSnappi_ 8h ago
this dart impl looks solid, the unroll makes total sense for avoiding those checks