r/rust • • 11h ago

📸 media Looking for feedback on a Rust vs. Dart SIMD comparison for ASCII validation

Post image

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:

pub fn is_ascii_auto(b: &[u8]) -> bool {
    b.iter().fold(0u8, |a, &x| a | x) < 0x80
}

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:

#![feature(portable_simd)]
use std::simd::prelude::*;

pub fn is_ascii_portable(b: &[u8]) -> bool {
    let load = |c: &[u8], at: usize| u8x16::from_slice(&c[at..at + 16]);
    let mut chunks = b.chunks_exact(128);
    let mut acc = [u8x16::splat(0); 4];
    for c in &mut chunks {
        acc[0] |= load(c, 0) | load(c, 16);
        acc[1] |= load(c, 32) | load(c, 48);
        acc[2] |= load(c, 64) | load(c, 80);
        acc[3] |= load(c, 96) | load(c, 112);
    }
    let mut all = (acc[0] | acc[1]) | (acc[2] | acc[3]);
    let mut rest = chunks.remainder().chunks_exact(16);
    for c in &mut rest {
        all |= u8x16::from_slice(c);
    }
    let tail = rest.remainder().iter().fold(0u8, |a, &x| a | x);
    all.reduce_or() < 0x80 && tail < 0x80
}

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.

6 Upvotes

3 comments sorted by

2

u/Shnatsel 2h ago edited 1h ago

Your Rust code looks reasonable, and the fact that its performance tracks <[u8]>::is_ascii from 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.

My goal is to expose the Wasm SIMD API through a convenient interface which seems to be a shared goal with std::simd.

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.

// The pragmas remove bounds checks and interrupt checks @pragma('vm:unsafe:no-bounds-checks') @pragma('vm:unsafe:no-interrupts')

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/modulovalue 1h ago

Thank you Sergey!

> It's also worth noting that SIMD on 64-bit ARM is easy mode

Oh yes, NEON is a lot of fun!

> the optimizer eliminates them for you.

We do have bounds check elimination in Dart, but unfortunately it's not well optimized for SIMD use cases yet, where wider strides are essential. It's on my todo list to eventually improve those parts too. Now you got me wondering how far Rust goes and what cases even Rust can't prove enough about for BCE to kick in.

-1

u/charmSnappi_ 8h ago

this dart impl looks solid, the unroll makes total sense for avoiding those checks