-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimise memory view getters #2886
Merged
Merged
Conversation
This file contains 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
Getting the buffer of a `WebAssembly.Memory` seems to have some overhead to it, so instead check if the existing view is 0 bytes long, which happens when they become detached after the memory is grown. I'm pretty sure that they'll never be zero bytes long otherwise; but even if they are, it's not the end of the world to recreate the view every time in that rare circumstance.
With the last commit, if `init` was called twice on web/no-modules, the memory views would be stuck pointing to the first copy of memory, since it never actually gets detached; this fixes that by manually resetting them. This also allows eliminating the null check in the memory view getters, but it has practically no effect on performance.
Great work! |
Nice wins, and thanks! |
Liamolucko
added a commit
to Liamolucko/wasm-bindgen
that referenced
this pull request
Jun 27, 2022
In the bundler target, there's a circular dependency between the bindings and the wasm module. That means that the wasm module's exports aren't available at the top level. In rustwasm#2886, I didn't realise that and made the memory views be initialised at the top level, which resulted in an error from the wasm module's memory not being available yet. This fixes that by lazily initialising the memory views like they were before rustwasm#2886, except that they're reset to uninitialised in `init` to make sure they're updated if it's called multiple times (the reason I made them be immediately initialised in the first place).
ranile
pushed a commit
that referenced
this pull request
Jun 27, 2022
In the bundler target, there's a circular dependency between the bindings and the wasm module. That means that the wasm module's exports aren't available at the top level. In #2886, I didn't realise that and made the memory views be initialised at the top level, which resulted in an error from the wasm module's memory not being available yet. This fixes that by lazily initialising the memory views like they were before #2886, except that they're reset to uninitialised in `init` to make sure they're updated if it's called multiple times (the reason I made them be immediately initialised in the first place).
This was referenced Jan 21, 2023
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.
While working on RReverser/serde-wasm-bindgen#34, I noticed that a significant amount of time was spent getting memory views. Most of that time was spent in the getter for
WebAssembly.Memory.buffer
, so I've changed the memory view getters to check for invalidation without comparing to the wasm memory, by checking if the length is 0 (which indicates the buffer being detached).wasm-bindgen
benchmarks:serde-wasm-bindgen
benchmarks (not including RReverser/serde-wasm-bindgen#34):