Repository navigation
Avoid copying the wasm binary in getWasmBinary under node #28110
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -387,8 +387,8 @@ async function getWasmBinary(binaryFile) { | |
| if (!wasmBinary) { | ||
| // Fetch the binary using readAsync | ||
| try { | ||
| var response = await readAsync(binaryFile); | ||
| return new Uint8Array(response); | ||
| // Await so a rejection is caught and falls back to getBinarySync. | ||
| return await readAsync(binaryFile); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning While To avoid copying the var response = await readAsync(binaryFile);
return response instanceof Uint8Array ? response : new Uint8Array(response); |
||
| } catch {} | ||
| } | ||
| // Otherwise, getBinarySync should be able to get it synchronously | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning
readAsync(binaryFile)directly means that on the web, wherereadAsyncreturns anArrayBuffer,getWasmBinarywill now return anArrayBufferinstead of aUint8Array.While
WebAssembly.instantiateaccepts both, other parts of the application or user-defined code might accessModule.wasmBinary(or the returned value ofgetWasmBinary) and expect aUint8Array(for example, checking.lengthor calling.subarray()). SinceArrayBufferdoes not have these properties/methods, this change could introduce runtime errors or silent bugs on the web.To avoid copying the
Bufferunder Node (sinceBufferis a subclass ofUint8Array) while still ensuring aUint8Arrayis returned on the web, we can check if the response is already an instance ofUint8Arrayand only wrap it innew Uint8Array()if it is not. This preserves the performance optimization under Node without changing the return type on the web.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value never reaches
Module.wasmBinaryor user code.getWasmBinaryhas one caller,instantiateArrayBuffer, which passes the result straight toWebAssembly.instantiate(binary, imports), and that accepts anArrayBufferas well as a typed array. The function is internal (it is not in any exported runtime symbol list), andwasmBinaryis a separate variable that this code only reads.So on the web the value now stays an
ArrayBufferall the way intoinstantiate, and nothing else sees it. I kept the plain return because it is smaller in every codesize expectation. If you would rather keep aUint8Arrayon the web, I can switch toresponse instanceof Uint8Array ? response : new Uint8Array(response).