Skip to content
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

feat: support jspi on available platforms #100

Merged
merged 4 commits into from
Nov 15, 2024

Conversation

chrisdickinson
Copy link
Contributor

@chrisdickinson chrisdickinson commented Nov 14, 2024

This allows us to use async host functions on the main thread on supported platforms. (Right now that's just Node v23+, as far as I know.)

Additionally: this PR also removes WASIp1 support for Deno, since WASI support is broken there as of Deno v2.

Example use:

const functions = {
  'extism:host/user': {
    async hello_world(context: CallContext, off: bigint) {
      await new Promise(resolve => setTimeout(resolve, 100))
      return context.store('wow okay then');
    },
  },
};

const plugin = await createPlugin(
  { wasm: [{ url: 'http://localhost:8124/wasm/code-functions.wasm' }] },
  { useWasi: true, functions },
);

try {
  const output = await plugin.call('count_vowels', 'hello world');
  assert.equal(output?.string(), 'wow okay then');
} finally {
  await plugin.close();
}

(Note: there might still be a good reason to keep background-worker around since –AFAICT– suspending Wasm still blocks the main thread while executing. However, we might be able to make the threaded version a lot nicer! Now that we can await in host functions on the background thread, we don't need to atomically copy data down from the host anymore, we can just postMessage the new blocks down. Best of both worlds! This represents future work outside the scope of this PR, however.)

This allows us to use async host functions on the main thread on supported
platforms. (Right now that's just Node v23+, as far as I know.)
@chrisdickinson chrisdickinson force-pushed the chris/20241114-support-jspi branch from 70c1248 to 89aa445 Compare November 14, 2024 23:29
@chrisdickinson chrisdickinson force-pushed the chris/20241114-support-jspi branch from e9c5ab0 to a1deb53 Compare November 14, 2024 23:51
Copy link
Contributor

@zshipko zshipko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! As I mentioned in a comment I'm slightly out of my depth here but from what I can tell the updates look good. Windows CI is failing, but once that is fixed I am in favor of merging this.

I know you mentioned background plugins are slower in the benchmark PR, would this have the potential to improve performance while still enabling async functions?

new(fn: CallableFunction): any
}

const AsyncFunction = (async () => { }).constructor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is when I realized I knew nothing about JS. Usually it happens a little further down the diff 😅

@chrisdickinson
Copy link
Contributor Author

I know you mentioned background plugins are slower in the benchmark PR, would this have the potential to improve performance while still enabling async functions?

Yep! On platforms that support JSPI, we should be able to instantiate foreground plugins that use async host functions, which clocked in at around 190-231 microseconds per instantiation.

playwright's safari on windows does not include webassembly support!
@chrisdickinson chrisdickinson force-pushed the chris/20241114-support-jspi branch from 6d327bd to 800b781 Compare November 15, 2024 00:56
@chrisdickinson chrisdickinson merged commit d349c3e into main Nov 15, 2024
5 checks passed
@chrisdickinson chrisdickinson deleted the chris/20241114-support-jspi branch November 15, 2024 01:01
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