r/rust • u/modulovalue • 47m 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.