Skip to content

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().

kyleconroy and others added 2 commits December 5, 2023 18:09
Co-authored-by: Anuraag Agrawal <[email protected]>
Co-authored-by: Anuraag Agrawal <[email protected]>
@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
@kyleconroy kyleconroy deleted the kyle/wazero branch January 2, 2024 19:07
alfonsodev pushed a commit to ExponentiaTeam/sqlc that referenced this pull request Oct 13, 2025
* feat(plugins): Use wazero instead of wasmtime

* Remove wasm build tags

* Update internal/ext/wasm/wasm.go

Co-authored-by: Anuraag Agrawal <[email protected]>

* Update internal/ext/wasm/wasm.go

Co-authored-by: Anuraag Agrawal <[email protected]>

* Fix build

* Suggestions for PR sqlc-dev#3042 (sqlc-dev#3082)

* Only compile wasm once per process

* Remove unused

* Store runtime in flightgroup as well

* Handle error from instantiate

---------

Co-authored-by: Anuraag Agrawal <[email protected]>
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.

3 participants