Skip to content

deno: add static ESM export for the webassembly bytes#88

Open
kunring wants to merge 2 commits into
yoav-lavi:mainfrom
kunring:main
Open

deno: add static ESM export for the webassembly bytes#88
kunring wants to merge 2 commits into
yoav-lavi:mainfrom
kunring:main

Conversation

@kunring

@kunring kunring commented Aug 7, 2022

Copy link
Copy Markdown

Description

Currently the .wasm file is only importable dynamically. This is a problem for multiple reasons. In the spirit of Deno's ecosystem, all imports that can be static should be static. Deno's native bundler won't be able to resolve dynamic imports, but instead leaves them as dynamic. This means that the .wasm has to be loaded from the internet every time the program is ran.

This change doesn't touch the current system, but instead adds an alternative static way to import the .wasm. Usage is as follows:

import { bytes } from "./melody_wasm_static.js"
import { initSync, compiler } from "./melody_wasm.js"
initSync(bytes);
console.log(compiler("1 to 32 of <alphanumeric>;"));

This is compliant with the static imports of the Deno ecosystem and Deno's bundler and compiler are able to resolve this to standalone files that don't require an internet connection to execute:
image

There should probably be a mention of this in the documentation, or this should be the only way to import the wasm in the deno module.

Type of change

  • 🚀 Feature

@yoav-lavi

Copy link
Copy Markdown
Owner

Thanks @kunring!

I'm currently on mobile but will look into this, if the initialization is synchronous I'd probably do it either automatically in the module or lazily once.

Would be important to see how this performs as it does the base64 decode on every run (although not sure how expensive it'd be, and definitely would be faster than a download), and it would mean creating new versions would be a bit more involved, but if the alternative is downloading the Wasm module on every run it may be worth it (does Deno not cache these at all?)

@kunring

kunring commented Aug 11, 2022

Copy link
Copy Markdown
Author

Measured with console.time() the base64 decode takes about 15-20ms on my low end machine.

Deno doesn't seem to cache fetch() calls, while static imports are cached by default, so this would speed up consecutive starts.

@kunring

kunring commented Aug 11, 2022

Copy link
Copy Markdown
Author

I added a Deno script to generate the .js file from the .wasm file in the folder, so that you can simply run that on your pipeline

@kunring kunring closed this Aug 11, 2022
@kunring kunring reopened this Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants