Skip to content

Provide SWAR memcmp implementation when SIMD not enabled - #910

Merged
alexcrichton merged 1 commit into
WebAssembly:mainfrom
msuozzo:push-mrkmmnrqptsx
Nov 12, 2026
Merged

alexcrichton merged 1 commit into
WebAssembly:mainfrom
msuozzo:push-mrkmmnrqptsx

Conversation

@msuozzo

@msuozzo msuozzo commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Without SIMD, memcmp compares one byte per iteration. Add a path for
n >= 8 that compares 8-byte words and locates the first differing byte
with ctz on their XOR. Calls shorter than 8 bytes go straight to the
existing byte loop.

ns per call, old loop -> new, Clang trunk at -O3, AMD EPYC 7B13:

n     first difference   Wasmtime 50     V8 (Node 22)
4     none               4.4 -> 3.5      4.2 -> 4.5
8     byte 0             2.4 -> 2.5      3.2 -> 3.8
8     none               7.0 -> 2.9      7.7 -> 3.8
16    none               12.5 -> 3.9     12.3 -> 4.4
64    byte 0             2.4 -> 2.7      3.4 -> 3.7
64    none               47.4 -> 9.4     43.1 -> 8.8
1024  none               646 -> 128      521 -> 103

The function grows from 74->283 bytes at -O3 and from 66->159 at -Oz.

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Would you be up for adding a benchmark to this repository itself to perhaps assist others when this is changed in the future? There are no benchmarks at this time in wasi-libc, but I'd be surprised if there was no mostly-works-out-the-box style project that works with benchmarking and CMake

Comment thread libc-top-half/musl/src/string/memcmp.c Resolved
Comment thread libc-top-half/musl/src/string/memcmp.c Resolved
Without SIMD, memcmp compares one byte per iteration. Add a path for
n >= 8 that compares 8-byte words and locates the first differing byte
with ctz on their XOR. Calls shorter than 8 bytes go straight to the
existing byte loop.

ns per call, old loop -> new, Clang trunk at -O3, AMD EPYC 7B13:

  n     first difference   Wasmtime 50     V8 (Node 22)
  4     none               4.2 -> 3.5      4.3 -> 4.1
  8     byte 0             2.2 -> 2.2      3.2 -> 3.2
  8     none               6.6 -> 3.2      7.5 -> 3.6
  16    none               12.6 -> 4.4     11.2 -> 4.2
  64    byte 0             2.2 -> 2.2      3.2 -> 3.2
  64    none               46.9 -> 11.9    33.2 -> 8.3
  1024  none               642 -> 167      502 -> 89

The function grows from 74->311 bytes at -O3 and from 66->185 at -Oz.
@msuozzo

msuozzo commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks! Would you be up for adding a benchmark to this repository itself to perhaps assist others when this is changed in the future? There are no benchmarks at this time in wasi-libc, but I'd be surprised if there was no mostly-works-out-the-box style project that works with benchmarking and CMake

okay did a quick search and most i found either require libc++ (google benchmark) or don't support wasi/high-precision (ubench.h). generated #911 which looks pretty reasonable. would that work?

for reference, the two outputs would look like this. #911 (main+benchmark):

Benchmark_memcmp/equal/4                 24693450       4.40 ns/op     908.16 MB/s
Benchmark_memcmp/equal/8                 16491924       6.88 ns/op    1163.27 MB/s
Benchmark_memcmp/equal/16                 8656410      11.90 ns/op    1344.95 MB/s
Benchmark_memcmp/equal/64                 2528383      44.48 ns/op    1438.88 MB/s
Benchmark_memcmp/equal/1024                173675     646.65 ns/op    1583.54 MB/s
Benchmark_memcmp/diff0/8                 47868763       2.18 ns/op
Benchmark_memcmp/diff0/64                47847609       2.19 ns/op

and #910 (this pr):

Benchmark_memcmp/equal/4                 30891632       3.46 ns/op    1155.47 MB/s
Benchmark_memcmp/equal/8                 41728672       2.51 ns/op    3190.10 MB/s
Benchmark_memcmp/equal/16                33958410       3.16 ns/op    5063.95 MB/s
Benchmark_memcmp/equal/64                16492149       6.90 ns/op    9280.80 MB/s
Benchmark_memcmp/equal/1024               1320492      89.43 ns/op   11450.73 MB/s
Benchmark_memcmp/diff0/8                 47466348       2.23 ns/op
Benchmark_memcmp/diff0/64                47353489       2.25 ns/op

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll review the other PR for benchmarking

@alexcrichton
alexcrichton merged commit 4ec32c5 into WebAssembly:main Nov 12, 2026
37 checks passed
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.

2 participants