-
Notifications
You must be signed in to change notification settings - Fork 28
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
Allow using CompiledModule from previous call instead of loading from disk #50
Comments
Basically I'd like to do something like add these two new functions. -
This will be generated in the An example implementation is provided below. Let me know if I am doing something wrong :) Here we are creating a
func (p *HelperFunctionPlugin) Open(ctx context.Context, pluginPath string) (wazero.Runtime, wazero.CompiledModule, error) {
b, err := os.ReadFile(pluginPath)
if err != nil {
return nil, nil, err
}
// Create a new runtime so that multiple modules will not conflict
r, err := p.newRuntime(ctx)
if err != nil {
return nil, nil, err
}
// Compile the WebAssembly module using the default configuration.
code, err := r.CompileModule(ctx, b)
if err != nil {
return nil, nil, err
}
// InstantiateModule runs the "_start" function, WASI's "main".
module, err := r.InstantiateModule(ctx, code, p.moduleConfig)
if err != nil {
// Note: Most compilers do not exit the module after running "_start",
// unless there was an Error. This allows you to call exported functions.
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {
return nil, nil, fmt.Errorf("unexpected exit_code: %d", exitErr.ExitCode())
} else if !ok {
return nil, nil, err
}
}
defer module.Close(ctx)
// Compare API versions with the loading plugin
apiVersion := module.ExportedFunction("helper_function_api_version")
if apiVersion == nil {
return nil, nil, errors.New("helper_function_api_version is not exported")
}
results, err := apiVersion.Call(ctx)
if err != nil {
return nil, nil, err
} else if len(results) != 1 {
return nil, nil, errors.New("invalid helper_function_api_version signature")
}
if results[0] != HelperFunctionPluginAPIVersion {
return nil, nil, fmt.Errorf("API version mismatch, host: %d, plugin: %d", HelperFunctionPluginAPIVersion, results[0])
}
info := module.ExportedFunction("helper_function_info")
if info == nil {
return nil, nil, errors.New("helper_function_info is not exported")
}
execute := module.ExportedFunction("helper_function_execute")
if execute == nil {
return nil, nil, errors.New("helper_function_execute is not exported")
}
return r, code, nil
}
func (p *HelperFunctionPlugin) LoadWithCompiled(ctx context.Context, r wazero.Runtime, code wazero.CompiledModule) (helperFunction, error) {
// InstantiateModule runs the "_start" function, WASI's "main".
module, err := r.InstantiateModule(ctx, code, p.moduleConfig)
if err != nil {
// Note: Most compilers do not exit the module after running "_start",
// unless there was an Error. This allows you to call exported functions.
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {
return nil, fmt.Errorf("unexpected exit_code: %d", exitErr.ExitCode())
} else if !ok {
return nil, err
}
}
info := module.ExportedFunction("helper_function_info")
if info == nil {
return nil, errors.New("helper_function_info is not exported")
}
execute := module.ExportedFunction("helper_function_execute")
if execute == nil {
return nil, errors.New("helper_function_execute is not exported")
}
malloc := module.ExportedFunction("malloc")
if malloc == nil {
return nil, errors.New("malloc is not exported")
}
free := module.ExportedFunction("free")
if free == nil {
return nil, errors.New("free is not exported")
}
return &helperFunctionPlugin{
runtime: r,
module: module,
malloc: malloc,
free: free,
info: info,
execute: execute,
}, nil
} |
@Ice3man543 by your description, I'm not sure if you have seen this or not. Can you comment first about it, because caching compilation is pretty involved, which is why we have this. In fact the recent ability to add RuntimeConfig to plugin-wasm was about sharing compilation https://pkg.go.dev/github.com/tetratelabs/wazero#example-package-CompileCache #32 |
@codefromthecrypt this makes a lot of sense. Thanks for the link, will look into it. BTW, the library is very neat. Kudos for your awesome work on it! |
@dmvolod gets credit for the recent phase of development, especially towards what you are asking about. Good luck! |
I have a list of web assembly modules written in go that provide extensibility to my program.
I don't want to compile the plug-in each time i use it though because the program is a long running multi goroutine application where 15ms+ for plugin loading each time would be very bad.
Ideally I'd like the ability to compile module beforehand and use it later by instantiating new instances per goroutine.
The text was updated successfully, but these errors were encountered: