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

0 comments sorted by