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(plugins): Use wazero instead of wasmtime #3042

Merged
merged 8 commits into from
Jan 2, 2024
Merged

feat(plugins): Use wazero instead of wasmtime #3042

merged 8 commits into from
Jan 2, 2024

Conversation

kyleconroy
Copy link
Collaborator

@kyleconroy kyleconroy commented Dec 5, 2023

After merging #3027 and #3040, wasmtime is our only cgo dependency left. This change replaces with wazero, a pure-Go WASM runtime. I've played around with it locally and everything appears to work, but I'm going to need to understand how the caching works before this is merged.

Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

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

I was thinking of sending this, great to see it :D

wasiConfig.SetStdinFile(stdinPath)
wasiConfig.SetStdoutFile(stdoutPath)
wasiConfig.SetStderrFile(stderrPath)
// TODO: Handle error
Copy link
Contributor

Choose a reason for hiding this comment

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

There is Instantiate if you'd like to return the error. Though I think any failure here would be a programming bug, not non-determinstic

keys = append(keys, key)
vals = append(vals, os.Getenv(key))
// Compile the Wasm binary once so that we can skip the entire compilation time during instantiation.
mod, err := rt.CompileModule(ctx, wasmBytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's possible, it would be nice to rejigger to scope this to Runner, possibly with some map[/* wasm url */ string]wazero.CompiledModule. The compilation cache is good to reuse across executions of the sqlc process itself, but it's also good to only compile once per wasm within a process if possible, the cache key computation isn't trivial. Though if the latter doesn't happen that much maybe it doesn't matter

if err != nil {
return fmt.Errorf("define wasi: %w", err)
conf := wazero.NewModuleConfig()
conf = conf.WithArgs("plugin.wasm", method)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, consider chaining, it's arguably idiomatic for wazero users

conf := wazero.NewModuleConfig().
  WithArgs().
  WithStdin().
  WithStdout().

internal/ext/wasm/wasm.go Outdated Show resolved Hide resolved
internal/ext/wasm/wasm.go Outdated Show resolved Hide resolved
kyleconroy and others added 2 commits December 5, 2023 18:09
Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
@anuraaga
Copy link
Contributor

anuraaga commented Dec 6, 2023

Oops sorry looks like need to update the type argument to checkError method

@anuraaga
Copy link
Contributor

Hi @kyleconroy - sorry if this steps on any toes and feel free to ignore it, being interested in this PR I was hoping to help continue it. I sent #3082 as a PR to this branch with the suggestions. Thanks.

anuraaga and others added 3 commits January 2, 2024 09:58
* Only compile wasm once per process

* Remove unused

* Store runtime in flightgroup as well
@kyleconroy kyleconroy merged commit 4188d23 into main Jan 2, 2024
8 checks passed
@kyleconroy kyleconroy deleted the kyle/wazero branch January 2, 2024 19:07
This pull request was closed.
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