perf(hash): Chunk message input in JavaScript to reduce WASM heap size #1010
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.
It is generally not possible to reduce the size of a WASM module's heap, only grow it (pointed out by
@evanwashere in Discord, see also WebAssembly/design#1300). There are some possible work-arounds, but they would add complexity and might not be possible with our current wasm-bindgen/wasm-pack build approach.
When you
.update(...)
our WASM Hasher with a message, that data needs to be copied over to the WASM heap, which may require it to grow if it doesn't have enough available space. So if you pass a message that's 10MB, or 2GB, the WASM heap and memory usage will increase to at least that much and never come down.This change instead splits messages into chunks of up to 64KB each and feeds them to WASM individually, keeping the total WASM heap size under a few megabytes regardless of the message size.
I benchmarked this to ensure it wouldn't adversely affect throughput, and found that as long as the piece size isn't too small, throughput is very-slightly improved relative to the existing code. I used 64KB chunks because they seemed the smallest fastest chunk size in my testing, by a small margin.