Repository navigation
[libc] Always use __builtin_wasm_memory_copy in memcpy - #27653
Merged
Merged
Conversation
Collaborator
Author
|
All the benchmarking here was done by gemini under my supervision. |
Collaborator
Author
AI Generated: Benchmark Details & WorkloadsHere is the microbenchmark and workloads used to measure Source Code (
|
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
force-pushed
the
always-bulkmem-memcpy
branch
from
September 2, 2026 20:26
74c40cb to
05267de
Compare
dschuff
reviewed
Sep 2, 2026
| # 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', |
Member
There was a problem hiding this comment.
don't we still need to actually compile this file?
Collaborator
Author
There was a problem hiding this comment.
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
Collaborator
Author
There was a problem hiding this comment.
See the comment 3 lines up
sbc100
enabled auto-merge (squash)
September 2, 2026 20:44
dschuff
approved these changes
Sep 2, 2026
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Remove the manual 32-bit copy loop for sizes < 512 bytes in
emscripten_memcpy.cand always use__builtin_wasm_memory_copyinstead, matching the behavior previously used only for-Oz.Benchmarking across V8, SpiderMonkey, and JavaScriptCore shows that
memory.copyis 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_copyfor sizes <= 511 bytes (500k iterations per size):Aligned copies (sizes 1B to 511B):
Unaligned copies (source and dest offsets 0..3):