Closed apisorbust closed 4 months ago
I think maybe you are misunderstanding what the compilation cache is caching. It’s caching the compiled module, not the instance. To cache an instance you probably want to look at the plug-in pool PR
Following back up from discussion in Discord.
Please give this a try:
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),
}
plugin1, err := NewPlugin(ctx, manifest, config, []HostFunction{})
plugin2, err := NewPlugin(ctx, manifest, config, []HostFunction{})
Any update as to whether this works or not?
Hello @apisorbust I have tested this code (which uses the cache for 2 different plugins) and it seems to work. I am closing the issue, but feel free to reopen it if it doesn't work for you, or if that doesn't exactly cover what you meant
func main() {
ctx := context.Background()
compilationCache := wazero.NewCompilationCache()
defer func(cache wazero.CompilationCache, ctx context.Context) {
err := cache.Close(ctx)
if err != nil {
fmt.Printf("Failed to close compilation cache: %v\n", err)
os.Exit(1)
}
}(compilationCache, ctx)
manifest1 := extism.Manifest{
Wasm: []extism.Wasm{
extism.WasmUrl{
Url: "https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm",
},
},
}
manifest2 := extism.Manifest{
Wasm: []extism.Wasm{
extism.WasmUrl{
Url: "https://github.com/extism/plugins/releases/latest/download/consume.wasm",
},
},
}
config1 := extism.PluginConfig{
EnableWasi: true,
ModuleConfig: wazero.NewModuleConfig(),
RuntimeConfig: wazero.NewRuntimeConfig().WithCompilationCache(compilationCache),
}
_, err := extism.NewPlugin(ctx, manifest1, config1, []extism.HostFunction{})
if err != nil {
fmt.Printf("Failed to initialize plugin 1: %v\n", err)
os.Exit(1)
}
config2 := extism.PluginConfig{
EnableWasi: true,
ModuleConfig: wazero.NewModuleConfig(),
RuntimeConfig: wazero.NewRuntimeConfig().WithCompilationCache(compilationCache),
}
_, err = extism.NewPlugin(ctx, manifest2, config2, []extism.HostFunction{})
if err != nil {
fmt.Printf("Failed to initialize plugin 2: %v\n", err)
os.Exit(1)
}
fmt.Println("Successfully initialized plugins")
}
Result:
Successfully initialized plugins
I thought it might be possible to share the compilation cache across plugin instantiations. In my case I am creating one plugin instance per .wasm module. So maybe my error is assuming multiple .wasm modules can share the same cache? None the less I tried using a variable to store a single instance of cache:
var compilationCache := wazero.NewCompilationCache()
then using that variable when creating plugin instances:
That would end up causing a Panic:
SO.. perhaps this caching ONLY works for creating multiple instances of the SAME plugin? Not a big deal either way.. just thought perhaps this would save a little bit of resources across new plugin instantiations.