Skip to content

Conversation

@mhmd-azeez
Copy link
Contributor

Related to #97

This PR documents CompiledPlugin, it also adds a note clarifying the lifetime of a CompiledPlugin is tied to wazero cache if it's enabled

@johanjanssens
Copy link

johanjanssens commented May 14, 2025

@mhmd-azeez Thanks for this! Some feedback.

CompilationCache

ctx := context.Background()
cache := wazero.NewCompilationCache()
defer cache.Close(ctx)

manifest := Manifest{Wasm: []Wasm{WasmFile{Path: "wasm/noop.wasm"}}}

config := PluginConfig{
    EnableWasi:    true,
    ModuleConfig:  wazero.NewModuleConfig(),
    RuntimeConfig: wazero.NewRuntimeConfig().WithCompilationCache(cache),
}

_, err := NewPlugin(ctx, manifest, config, []HostFunction{})

This code doesn't make it clear to me that the cache lifetime extends that of the runtime. The info in the Wazero code can offer inspiration:

https://github.com/tetratelabs/wazero/blob/0dea5d7ee1de12d2817d6ac8548a4d36aaf59aea/cache.go#L64

Possible inspiration from Wazero examples: https://github.com/tetratelabs/wazero/blob/0dea5d7ee1de12d2817d6ac8548a4d36aaf59aea/examples/multiple-runtimes/counter.go

CompiledPlugin

The main reason to compile a plugin and later instantiate it is to allow for a plugin to be used across multiple goroutines as also mentioned in the code:

// NewPlugin creates compiles and instantiates a plugin that is ready

func NewCompiledPlugin(

The example exit, out, err := plugin.Call("count_vowels", data) would therefor best be explained by putting the Call into a goroutine, which demonstrates thread safety.

Possible inspiration from Wazero examples: https://github.com/tetratelabs/wazero/blob/0dea5d7ee1de12d2817d6ac8548a4d36aaf59aea/examples/concurrent-instantiation/main.go

Note that there is an extra caveat here that took me a off guard a bit, which is the plugin state is not shared across instances. In a concurrency scenario with multiple instances, state will be bound to the specific instance of the plugin.

@mhmd-azeez
Copy link
Contributor Author

@johanjanssens thanks for the feedback, I have added a comment in the wazero cache section, and i have used goroutines in the compiled plugin section

@johanjanssens
Copy link

Thanks! This is looking good, ready to roll :)

@mhmd-azeez mhmd-azeez merged commit de6f8ac into main May 14, 2025
3 checks passed
@mhmd-azeez mhmd-azeez deleted the docs/compiled-plugin branch May 14, 2025 21:17
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.

4 participants