Add support for compiling with atomics for Node.js#4318
Merged
daxpedda merged 1 commit intowasm-bindgen:mainfrom Dec 5, 2024
Merged
Add support for compiling with atomics for Node.js#4318daxpedda merged 1 commit intowasm-bindgen:mainfrom
atomics for Node.js#4318daxpedda merged 1 commit intowasm-bindgen:mainfrom
Conversation
daxpedda
added a commit
to daxpedda/wasm-bindgen
that referenced
this pull request
Dec 6, 2024
newpavlov
pushed a commit
to rust-random/getrandom
that referenced
this pull request
Dec 9, 2024
As discussed in #541. This PR adds the following improvements: - Caching of the global `Crypto` object. - Detecting if our Wasm memory is based on a `SharedArrayBuffer`. If not, we can copy bytes directly into our memory instead of having to go through JS. This saves allocating the buffer in JS and copying the bytes into Wasm memory. This is also the most common path. `SharedArrayBuffer` requires `target_feature = "atomics"`, which is unstable and requires Rust nightly. See #559 (comment) for full context. - The atomic path only creates a sub-array when necessary, potentially saving another FFI call. - The atomic path will now allocate an `Uint8Array` with the minimum amount of bytes necessary instead of a fixed size. - The maximum chunk size for the non-atomic path and the maximum `Uint8Array` size for the atomic paths have been increased to 65536 bytes: the maximum allowed buffer size for `Crypto.getRandomValues()`. All in all this should give a performance improvement of ~5% to ~500% depending on the amount of requested bytes and which path is taken. See #559 (comment) for some benchmark results. This spawned a bunch of improvements and fixes in `wasm-bindgen` that are being used here: - wasm-bindgen/wasm-bindgen#4315 - wasm-bindgen/wasm-bindgen#4316 - wasm-bindgen/wasm-bindgen#4318 - wasm-bindgen/wasm-bindgen#4319 - wasm-bindgen/wasm-bindgen#4340
atomics for Node.js
takumi-earth
pushed a commit
to earthlings-dev/getrandom
that referenced
this pull request
Jan 27, 2026
As discussed in rust-random#541. This PR adds the following improvements: - Caching of the global `Crypto` object. - Detecting if our Wasm memory is based on a `SharedArrayBuffer`. If not, we can copy bytes directly into our memory instead of having to go through JS. This saves allocating the buffer in JS and copying the bytes into Wasm memory. This is also the most common path. `SharedArrayBuffer` requires `target_feature = "atomics"`, which is unstable and requires Rust nightly. See rust-random#559 (comment) for full context. - The atomic path only creates a sub-array when necessary, potentially saving another FFI call. - The atomic path will now allocate an `Uint8Array` with the minimum amount of bytes necessary instead of a fixed size. - The maximum chunk size for the non-atomic path and the maximum `Uint8Array` size for the atomic paths have been increased to 65536 bytes: the maximum allowed buffer size for `Crypto.getRandomValues()`. All in all this should give a performance improvement of ~5% to ~500% depending on the amount of requested bytes and which path is taken. See rust-random#559 (comment) for some benchmark results. This spawned a bunch of improvements and fixes in `wasm-bindgen` that are being used here: - wasm-bindgen/wasm-bindgen#4315 - wasm-bindgen/wasm-bindgen#4316 - wasm-bindgen/wasm-bindgen#4318 - wasm-bindgen/wasm-bindgen#4319 - wasm-bindgen/wasm-bindgen#4340
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.
Our Node.js shim doesn't provide a way to pass in a
WebAssembly.MemoryorSharedArrayBuffer, so we just set the memory to not be imported but simply instantiated in Wasm.I don't know the historical context behind LLVM emitting the memory as an import by default when using atomics.
EDIT: LLVM does not, Rust does.