Skip to content

Conversation

@folkertdev
Copy link
Member

@folkertdev folkertdev commented Apr 15, 2025

Fixes #301

On current stable rust, this produces (from what I can tell) better and also shorter code.

https://godbolt.org/z/b3dh3n8Ma

One of these day's I'll get wasmtime to work on godbolt. It should be possible (to compile with llvm for wasm and then use wasmtime to get the generated assembly). I did it manually for now and the assembly also did look better and shorter.

@codecov
Copy link

codecov bot commented Apr 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Flag Coverage Δ
fuzz-compress 41.78% <0.00%> (-0.06%) ⬇️
fuzz-decompress 30.14% <0.00%> (-0.05%) ⬇️
test-aarch64-apple-darwin 90.30% <0.00%> (+<0.01%) ⬆️
test-x86_64-apple-darwin 88.42% <100.00%> (+0.01%) ⬆️
test-x86_64-unknown-linux-gnu 89.94% <0.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
zlib-rs/src/deflate/slide_hash.rs 98.96% <100.00%> (+0.01%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@folkertdev folkertdev force-pushed the wasm-slide-hash-safe branch from 9825ca3 to 026a4a3 Compare April 15, 2025 12:02
@folkertdev folkertdev force-pushed the wasm-slide-hash-safe branch from 026a4a3 to 70511dd Compare April 15, 2025 12:10
@folkertdev folkertdev requested a review from bjorn3 April 15, 2025 12:21
@bjorn3
Copy link
Collaborator

bjorn3 commented Apr 15, 2025

Yeah, looks better. The new code only has a single loop while the old code has two loops. And the new code folds the offsets into the vector load and store instructions while the old code emits separate addition instructions.

Output of wasm-decompile from the WebAssembly Binary Toolkit (WABT)
export memory memory(initial: 16, max: 0);

global stack_pointer:int = 1048576;
export global data_end:int = 1048576;
export global heap_base:int = 1048576;

table T_a:funcref(min: 1, max: 1);

export function old(a:simd_ptr@1, b:simd_ptr@1, c:int) {
  var d:int = b & -8;
  if (eqz(d)) goto B_a;
  var e:simd = i16x8_splat(c);
  c = b + -8;
  b = (c >> 3) + 1 & 3;
  if (eqz(b)) goto B_b;
  d = d - (b << 3);
  loop L_c {
    a[0] = a[0] i16x8_sub_sat_u e;
    a = a + 16;
    b = b + -1;
    if (b) continue L_c;
  }
  label B_b:
  if (c < 24) goto B_a;
  loop L_d {
    a[0] = a[0] i16x8_sub_sat_u e;
    b = a + 16;
    b[0] = b[0] i16x8_sub_sat_u e;
    b = a + 32;
    b[0] = b[0] i16x8_sub_sat_u e;
    b = a + 48;
    b[0] = b[0] i16x8_sub_sat_u e;
    a = a + 64;
    d = d + -32;
    if (d) continue L_d;
  }
  label B_a:
}

export function new(a:simd_ptr@2, b:int, c:int) {
  b = b & -32;
  if (eqz(b)) goto B_a;
  b = 0 - b;
  var d:simd = i16x8_splat(c);
  loop L_b {
    a[0] = (a[0] i16x8_max_u d) i16x8_sub d;
    a[8] = (a[8] i16x8_max_u d) i16x8_sub d;
    a[16] = (a[16] i16x8_max_u d) i16x8_sub d;
    a[24] = (a[24] i16x8_max_u d) i16x8_sub d;
    a = a + 64;
    b = b + 32;
    if (b) continue L_b;
  }
  label B_a:
}

@folkertdev folkertdev merged commit 721308f into main Apr 15, 2025
24 checks passed
@folkertdev folkertdev deleted the wasm-slide-hash-safe branch April 15, 2025 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

blocked: use an autovectorized implementation of slide_hash for wasm32

3 participants