Skip to content

[libc] Always use __builtin_wasm_memory_copy in memcpy - #27653

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:always-bulkmem-memcpy
Sep 2, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:always-bulkmem-memcpy

Conversation

@sbc100

@sbc100 sbc100 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Remove the manual 32-bit copy loop for sizes < 512 bytes in emscripten_memcpy.c and always use __builtin_wasm_memory_copy instead, matching the behavior previously used only for -Oz.

Benchmarking across V8, SpiderMonkey, and JavaScriptCore shows that memory.copy is consistently faster than the manual loop for small sizes, especially for unaligned copies.

Head-to-head microbenchmark comparing the old manual loop against __builtin_wasm_memory_copy for sizes <= 511 bytes (500k iterations per size):

Aligned copies (sizes 1B to 511B):

  • V8: 72.82ms vs 100.87ms (27.8% faster)
  • SpiderMonkey: 93.09ms vs 100.27ms (7.2% faster)
  • JavaScriptCore: 60.04ms vs 91.14ms (34.1% faster)

Unaligned copies (source and dest offsets 0..3):

  • V8: 148.72ms vs 357.02ms (58.3% faster)
  • SpiderMonkey: 186.95ms vs 440.61ms (57.6% faster)
  • JavaScriptCore: 124.16ms vs 372.28ms (66.6% faster)

@sbc100
sbc100 requested review from dschuff and kripken September 2, 2026 20:21
@sbc100

sbc100 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

All the benchmarking here was done by gemini under my supervision.

@sbc100

sbc100 commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

AI Generated: Benchmark Details & Workloads

Here is the microbenchmark and workloads used to measure __builtin_wasm_memory_copy vs the manual copy loop from emscripten_memcpy.c:

Source Code (compare_memcpy.c)

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <emscripten/emscripten.h>

char dst[1024*1024*16+16] = {};
char src_buf[1024*1024*16+16] = {};
volatile uint8_t checksum = 0;

// The manual loop implementation from emscripten_memcpy.c (prior to this change)
void *__attribute__((noinline)) memcpy_old_loop(void *restrict dest, const void *restrict src, size_t n) {
  unsigned char *d = (unsigned char *)dest;
  const unsigned char *s = (const unsigned char *)src;
  unsigned char *aligned_d_end;
  unsigned char *block_aligned_d_end;
  unsigned char *d_end;

  if (n >= 512) {
    __builtin_wasm_memory_copy(0, 0, dest, src, n);
    return dest;
  }

  d_end = d + n;
  if ((((uintptr_t)d) & 3) == (((uintptr_t)s) & 3)) {
    while ((((uintptr_t)d) & 3) && d < d_end) {
      *d++ = *s++;
    }
    aligned_d_end = (unsigned char *)(((uintptr_t)d_end) & -4);
    if (((uintptr_t)aligned_d_end) >= 64) {
      block_aligned_d_end = aligned_d_end - 64;
      while (d <= block_aligned_d_end) {
        *(((uint32_t*)d)) = *(((uint32_t*)s));
        *(((uint32_t*)d) + 1) = *(((uint32_t*)s) + 1);
        *(((uint32_t*)d) + 2) = *(((uint32_t*)s) + 2);
        *(((uint32_t*)d) + 3) = *(((uint32_t*)s) + 3);
        *(((uint32_t*)d) + 4) = *(((uint32_t*)s) + 4);
        *(((uint32_t*)d) + 5) = *(((uint32_t*)s) + 5);
        *(((uint32_t*)d) + 6) = *(((uint32_t*)s) + 6);
        *(((uint32_t*)d) + 7) = *(((uint32_t*)s) + 7);
        *(((uint32_t*)d) + 8) = *(((uint32_t*)s) + 8);
        *(((uint32_t*)d) + 9) = *(((uint32_t*)s) + 9);
        *(((uint32_t*)d) + 10) = *(((uint32_t*)s) + 10);
        *(((uint32_t*)d) + 11) = *(((uint32_t*)s) + 11);
        *(((uint32_t*)d) + 12) = *(((uint32_t*)s) + 12);
        *(((uint32_t*)d) + 13) = *(((uint32_t*)s) + 13);
        *(((uint32_t*)d) + 14) = *(((uint32_t*)s) + 14);
        *(((uint32_t*)d) + 15) = *(((uint32_t*)s) + 15);
        d += 64;
        s += 64;
      }
    }
    while (d < aligned_d_end) {
      *((uint32_t *)d) = *((uint32_t *)s);
      d += 4;
      s += 4;
    }
  } else {
    if (((uintptr_t)d_end) >= 4) {
      aligned_d_end = d_end - 4;
      while (d <= aligned_d_end) {
        *d = *s;
        *(d + 1) = *(s + 1);
        *(d + 2) = *(s + 2);
        *(d + 3) = *(s + 3);
        d += 4;
        s += 4;
      }
    }
  }
  while (d < d_end) {
    *d++ = *s++;
  }
  return dest;
}

// The new implementation (always bulk memory)
void *__attribute__((noinline)) memcpy_builtin_bulkmem(void *restrict dest, const void *restrict src, size_t n) {
  if (n) {
    __builtin_wasm_memory_copy(0, 0, dest, src, n);
  }
  return dest;
}

int sizes[] = {1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 48, 63, 64, 96, 127, 128, 255, 256, 511};
int num_sizes = sizeof(sizes)/sizeof(sizes[0]);

int main() {
  // 1. Aligned Benchmark (offsets 0, 0)
  const int aligned_iters = 500000;

  double t0 = emscripten_get_now();
  for (int s = 0; s < num_sizes; s++) {
    int sz = sizes[s];
    for (int i = 0; i < aligned_iters; i++) {
      memcpy_old_loop(dst, src_buf, sz);
      checksum += dst[sz >> 1];
    }
  }
  double t_old_aligned = emscripten_get_now() - t0;

  t0 = emscripten_get_now();
  for (int s = 0; s < num_sizes; s++) {
    int sz = sizes[s];
    for (int i = 0; i < aligned_iters; i++) {
      memcpy_builtin_bulkmem(dst, src_buf, sz);
      checksum += dst[sz >> 1];
    }
  }
  double t_new_aligned = emscripten_get_now() - t0;

  printf("Aligned (500k iters per size):\n");
  printf("  OLD (manual loop):     %.2f ms\n", t_old_aligned);
  printf("  NEW (builtin bulkmem): %.2f ms\n", t_new_aligned);

  // 2. Unaligned Benchmark (offsets: (0,0), (1,1), (1,2), (3,1), (2,0))
  const int unaligned_iters = 200000;
  int offsets[][2] = {{0, 0}, {1, 1}, {1, 2}, {3, 1}, {2, 0}};
  int num_offsets = sizeof(offsets)/sizeof(offsets[0]);

  t0 = emscripten_get_now();
  for (int off = 0; off < num_offsets; off++) {
    char *d = dst + offsets[off][0];
    char *s = src_buf + offsets[off][1];
    for (int si = 0; si < num_sizes; si++) {
      int sz = sizes[si];
      for (int i = 0; i < unaligned_iters; i++) {
        memcpy_old_loop(d, s, sz);
        checksum += d[sz >> 1];
      }
    }
  }
  double t_old_unaligned = emscripten_get_now() - t0;

  t0 = emscripten_get_now();
  for (int off = 0; off < num_offsets; off++) {
    char *d = dst + offsets[off][0];
    char *s = src_buf + offsets[off][1];
    for (int si = 0; si < num_sizes; si++) {
      int sz = sizes[si];
      for (int i = 0; i < unaligned_iters; i++) {
        memcpy_builtin_bulkmem(d, s, sz);
        checksum += d[sz >> 1];
      }
    }
  }
  double t_new_unaligned = emscripten_get_now() - t0;

  printf("Unaligned (200k iters per offset pair & size):\n");
  printf("  OLD (manual loop):     %.2f ms\n", t_old_unaligned);
  printf("  NEW (builtin bulkmem): %.2f ms\n", t_new_unaligned);

  return 0;
}

Compilation & Runner

emcc -O3 compare_memcpy.c -o compare_memcpy.js -sINITIAL_MEMORY=64MB -sENVIRONMENT=node,shell

# Engines:
~/.jsvu/bin/v8 --no-liftoff compare_memcpy.js
~/.jsvu/bin/spidermonkey compare_memcpy.js
~/.jsvu/bin/javascriptcore compare_memcpy.js

Remove the manual 32-bit copy loop for sizes < 512 bytes in
`emscripten_memcpy.c` and always use `__builtin_wasm_memory_copy`
instead, matching the behavior previously used only for `-Oz`.

Benchmarking across V8, SpiderMonkey, and JavaScriptCore shows that
`memory.copy` is consistently faster than the manual loop for small
sizes, especially for unaligned copies.

Head-to-head microbenchmark comparing the old manual loop against
`__builtin_wasm_memory_copy` for sizes <= 511 bytes (500k iterations
per size):

Aligned copies (sizes 1B to 511B):
- V8:             72.82ms vs 100.87ms (27.8% faster)
- SpiderMonkey:   93.09ms vs 100.27ms (7.2% faster)
- JavaScriptCore: 60.04ms vs  91.14ms (34.1% faster)

Unaligned copies (source and dest offsets 0..3):
- V8:            148.72ms vs 357.02ms (58.3% faster)
- SpiderMonkey:  186.95ms vs 440.61ms (57.6% faster)
- JavaScriptCore: 124.16ms vs 372.28ms (66.6% faster)
@sbc100
sbc100 force-pushed the always-bulkmem-memcpy branch from 74c40cb to 05267de Compare September 2, 2026 20:26
Comment thread tools/system_libs.py
# some files also appear in libc, and a #define affects them
mem_files = files_in_path(
path='system/lib/libc',
filenames=['emscripten_memcpy.c', 'emscripten_memset.c',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

don't we still need to actually compile this file?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but we no longer need to compile it here in libc_optz. These file are all also included in the normal libc, and because this no longer has different semantics under Oz is no longer needs to live here at all

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

See the comment 3 lines up

@sbc100
sbc100 enabled auto-merge (squash) September 2, 2026 20:44
@sbc100
sbc100 merged commit c28f663 into emscripten-core:main Sep 2, 2026
42 checks passed
@sbc100
sbc100 deleted the always-bulkmem-memcpy branch September 2, 2026 21:22
@kripken

kripken commented Sep 2, 2026

Copy link
Copy Markdown
Member

Maybe different CPUs, but I see a much smaller benefit in the aligned case, only 6% (vs 28% here). Still, it is faster.

sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 2, 2026
Followup to emscripten-core#27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to
a byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes
1B to 16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 2, 2026
Followup to emscripten-core#27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to
a byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes
1B to 16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 2, 2026
Followup to emscripten-core#27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to
a byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes
1B to 16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 2, 2026
Followup to emscripten-core#27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to
a byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes
1B to 16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit that referenced this pull request Sep 2, 2026
Followup to #27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to
a byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes
1B to 16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 2, 2026
Followup to emscripten-core#27653 and emscripten-core#27656.

Replace Musl's `memset.c` and the `-Oz` logic in `emscripten_memset.c`
with `__builtin_wasm_memory_fill`.

Under ASan, keep `__attribute__((no_sanitize("address")))` on the naive
scalar loop because ASan's shadow memory poisoner calls `REAL(memset)`
directly on shadow memory itself, which would otherwise trigger an
immediate shadow-on-shadow fault.

Microbenchmark results (100k iterations across sizes 1B to 16KB):

Aligned sets (sizes 1B to 16KB):
- V8:             30.36ms vs 70.52ms (56.9% faster, 2.3x)
- SpiderMonkey:   31.91ms vs 67.01ms (52.4% faster, 2.1x)
- JavaScriptCore: 26.70ms vs 70.44ms (62.1% faster, 2.6x)

Unaligned sets (offsets 1..3 across sizes 1B to 16KB):
- V8:             30.61ms vs 65.28ms (53.1% faster, 2.1x)
- SpiderMonkey:   32.94ms vs 64.28ms (48.8% faster, 2.0x)
- JavaScriptCore: 26.90ms vs 63.24ms (57.5% faster, 2.4x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 2, 2026
Followup to emscripten-core#27653 and emscripten-core#27656.

Replace Musl's `memset.c` and the `-Oz` logic in `emscripten_memset.c`
with `__builtin_wasm_memory_fill`.

Under ASan, keep `__attribute__((no_sanitize("address")))` on the naive
scalar loop because ASan's shadow memory poisoner calls `REAL(memset)`
directly on shadow memory itself, which would otherwise trigger an
immediate shadow-on-shadow fault.

Microbenchmark results (100k iterations across sizes 1B to 16KB):

Aligned sets (sizes 1B to 16KB):
- V8:             30.36ms vs 70.52ms (56.9% faster, 2.3x)
- SpiderMonkey:   31.91ms vs 67.01ms (52.4% faster, 2.1x)
- JavaScriptCore: 26.70ms vs 70.44ms (62.1% faster, 2.6x)

Unaligned sets (offsets 1..3 across sizes 1B to 16KB):
- V8:             30.61ms vs 65.28ms (53.1% faster, 2.1x)
- SpiderMonkey:   32.94ms vs 64.28ms (48.8% faster, 2.0x)
- JavaScriptCore: 26.90ms vs 63.24ms (57.5% faster, 2.4x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 3, 2026
Followup to emscripten-core#27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to
a byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes
1B to 16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 3, 2026
Followup to emscripten-core#27653 and emscripten-core#27656.

Replace Musl's `memset.c` and the `-Oz` logic in `emscripten_memset.c`
with `__builtin_wasm_memory_fill`.

Under ASan, keep `__attribute__((no_sanitize("address")))` on the naive
scalar loop because ASan's shadow memory poisoner calls `REAL(memset)`
directly on shadow memory itself, which would otherwise trigger an
immediate shadow-on-shadow fault.

Microbenchmark results (100k iterations across sizes 1B to 16KB):

Aligned sets (sizes 1B to 16KB):
- V8:             30.36ms vs 70.52ms (56.9% faster, 2.3x)
- SpiderMonkey:   31.91ms vs 67.01ms (52.4% faster, 2.1x)
- JavaScriptCore: 26.70ms vs 70.44ms (62.1% faster, 2.6x)

Unaligned sets (offsets 1..3 across sizes 1B to 16KB):
- V8:             30.61ms vs 65.28ms (53.1% faster, 2.1x)
- SpiderMonkey:   32.94ms vs 64.28ms (48.8% faster, 2.0x)
- JavaScriptCore: 26.90ms vs 63.24ms (57.5% faster, 2.4x)
sbc100 added a commit that referenced this pull request Sep 3, 2026
Followup to #27653.

Replace Musl's `memmove.c` and the `-Oz` manual loop in
`emscripten_memmove.c` with `__builtin_wasm_memory_copy`.

WebAssembly's `memory.copy` instruction is specified with overlapping
copy (`memmove`) semantics, and engines (V8, SpiderMonkey, JSC)
implement it using native host `memmove`.

In Musl's C implementation, misaligned overlapping copies fall back to a
byte-by-byte scalar loop in wasm, which is 15x to 33x slower than
`memory.copy`.

Overlapping copy benchmark results (200k iterations across sizes 1B to
16KB):

Shift forward (dest > src):
- V8:             34.51ms vs  576.50ms (94.0% faster, 16.7x)
- SpiderMonkey:   33.73ms vs 1129.64ms (97.0% faster, 33.5x)
- JavaScriptCore: 33.32ms vs  715.26ms (95.3% faster, 21.5x)

Shift backward (dest < src):
- V8:             34.28ms vs  551.30ms (93.8% faster, 16.1x)
- SpiderMonkey:   33.74ms vs  546.25ms (93.8% faster, 16.2x)
- JavaScriptCore: 32.90ms vs  535.86ms (93.9% faster, 16.3x)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Sep 3, 2026
Followup to emscripten-core#27653 and emscripten-core#27656.

Replace Musl's `memset.c` and the `-Oz` logic in `emscripten_memset.c`
with `__builtin_wasm_memory_fill`.

Under ASan, keep `__attribute__((no_sanitize("address")))` on the naive
scalar loop because ASan's shadow memory poisoner calls `REAL(memset)`
directly on shadow memory itself, which would otherwise trigger an
immediate shadow-on-shadow fault.

Microbenchmark results (100k iterations across sizes 1B to 16KB):

Aligned sets (sizes 1B to 16KB):
- V8:             30.36ms vs 70.52ms (56.9% faster, 2.3x)
- SpiderMonkey:   31.91ms vs 67.01ms (52.4% faster, 2.1x)
- JavaScriptCore: 26.70ms vs 70.44ms (62.1% faster, 2.6x)

Unaligned sets (offsets 1..3 across sizes 1B to 16KB):
- V8:             30.61ms vs 65.28ms (53.1% faster, 2.1x)
- SpiderMonkey:   32.94ms vs 64.28ms (48.8% faster, 2.0x)
- JavaScriptCore: 26.90ms vs 63.24ms (57.5% faster, 2.4x)
sbc100 added a commit that referenced this pull request Sep 4, 2026
Followup to #27653 and #27656.

Replace Musl's `memset.c` and the `-Oz` logic in `emscripten_memset.c`
with `__builtin_wasm_memory_fill`.

Under ASan, keep `__attribute__((no_sanitize("address")))` on the naive
scalar loop because ASan's shadow memory poisoner calls `REAL(memset)`
directly on shadow memory itself, which would otherwise trigger an
immediate shadow-on-shadow fault.

Microbenchmark results (100k iterations across sizes 1B to 16KB):

Aligned sets (sizes 1B to 16KB):
- V8:             30.36ms vs 70.52ms (56.9% faster, 2.3x)
- SpiderMonkey:   31.91ms vs 67.01ms (52.4% faster, 2.1x)
- JavaScriptCore: 26.70ms vs 70.44ms (62.1% faster, 2.6x)

Unaligned sets (offsets 1..3 across sizes 1B to 16KB):
- V8:             30.61ms vs 65.28ms (53.1% faster, 2.1x)
- SpiderMonkey:   32.94ms vs 64.28ms (48.8% faster, 2.0x)
- JavaScriptCore: 26.90ms vs 63.24ms (57.5% faster, 2.4x)
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.

3 participants