Files
wazero/internal/wasm/function_definition.go
Crypt Keeper be33572289 Adds HostFunctionBuilder to enable high performance host functions (#828)
This PR follows @hafeidejiangyou advice to not only enable end users to
avoid reflection when calling host functions, but also use that approach
ourselves internally. The performance results are staggering and will be
noticable in high performance applications.

Before
```
BenchmarkHostCall/Call
BenchmarkHostCall/Call-16            	 1000000	      1050 ns/op
Benchmark_EnvironGet/environGet
Benchmark_EnvironGet/environGet-16         	  525492	      2224 ns/op
```

Now
```
BenchmarkHostCall/Call
BenchmarkHostCall/Call-16            	14807203	        83.22 ns/op
Benchmark_EnvironGet/environGet
Benchmark_EnvironGet/environGet-16         	  951690	      1054 ns/op
```

To accomplish this, this PR consolidates code around host function
definition and enables a fast path for functions where the user takes
responsibility for defining its WebAssembly mappings. Existing users
will need to change their code a bit, as signatures have changed.

For example, we are now more strict that all host functions require a
context parameter zero. Also, we've replaced
`HostModuleBuilder.ExportFunction` and `ExportFunctions` with a new type
`HostFunctionBuilder` that consolidates the responsibility and the
documentation.

```diff
 ctx := context.Background()
-hello := func() {
+hello := func(context.Context) {
         fmt.Fprintln(stdout, "hello!")
 }
-_, err := r.NewHostModuleBuilder("env").ExportFunction("hello", hello).Instantiate(ctx, r)
+_, err := r.NewHostModuleBuilder("env").
+        NewFunctionBuilder().WithFunc(hello).Export("hello").
+        Instantiate(ctx, r)
```

Power users can now use `HostFunctionBuilder` to define functions that
won't use reflection. There are two choices of interfaces to use
depending on if that function needs access to the calling module or not:
`api.GoFunction` and `api.GoModuleFunction`. Here's an example defining
one.

```go
builder.WithGoFunction(api.GoFunc(func(ctx context.Context, params []uint64) []uint64 {
	x, y := uint32(params[0]), uint32(params[1])
	sum := x + y
	return []uint64{sum}
}, []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32})
```
As you'll notice and as documented, this approach is more verbose and
not for everyone. If you aren't making a low-level library, you are
likely able to afford the 1us penalty for the convenience of reflection.
However, we are happy to enable this option for foundational libraries
and those with high performance requirements (like ourselves)!

Fixes #825

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-28 07:51:08 -07:00

170 lines
4.8 KiB
Go

package wasm
import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasmdebug"
)
// ImportedFunctions returns the definitions of each imported function.
//
// Note: Unlike ExportedFunctions, there is no unique constraint on imports.
func (m *Module) ImportedFunctions() (ret []api.FunctionDefinition) {
for _, d := range m.FunctionDefinitionSection {
if d.importDesc != nil {
ret = append(ret, d)
}
}
return
}
// ExportedFunctions returns the definitions of each exported function.
func (m *Module) ExportedFunctions() map[string]api.FunctionDefinition {
ret := map[string]api.FunctionDefinition{}
for _, d := range m.FunctionDefinitionSection {
for _, e := range d.exportNames {
ret[e] = d
}
}
return ret
}
// BuildFunctionDefinitions generates function metadata that can be parsed from
// the module. This must be called after all validation.
//
// Note: This is exported for tests who don't use wazero.Runtime or
// NewHostModule to compile the module.
func (m *Module) BuildFunctionDefinitions() {
if len(m.FunctionSection) == 0 {
return
}
var moduleName string
var functionNames NameMap
var localNames IndirectNameMap
if m.NameSection != nil {
moduleName = m.NameSection.ModuleName
functionNames = m.NameSection.FunctionNames
localNames = m.NameSection.LocalNames
}
importCount := m.ImportFuncCount()
m.FunctionDefinitionSection = make([]*FunctionDefinition, 0, importCount+uint32(len(m.FunctionSection)))
importFuncIdx := Index(0)
for _, i := range m.ImportSection {
if i.Type != ExternTypeFunc {
continue
}
m.FunctionDefinitionSection = append(m.FunctionDefinitionSection, &FunctionDefinition{
importDesc: &[2]string{i.Module, i.Name},
index: importFuncIdx,
funcType: m.TypeSection[i.DescFunc],
})
importFuncIdx++
}
for codeIndex, typeIndex := range m.FunctionSection {
code := m.CodeSection[codeIndex]
m.FunctionDefinitionSection = append(m.FunctionDefinitionSection, &FunctionDefinition{
index: Index(codeIndex) + importCount,
funcType: m.TypeSection[typeIndex],
goFunc: code.GoFunc,
})
}
n, nLen := 0, len(functionNames)
for _, d := range m.FunctionDefinitionSection {
// The function name section begins with imports, but can be sparse.
// This keeps track of how far in the name section we've searched.
funcIdx := d.index
var funcName string
for ; n < nLen; n++ {
next := functionNames[n]
if next.Index > funcIdx {
break // we have function names, but starting at a later index.
} else if next.Index == funcIdx {
funcName = next.Name
break
}
}
d.moduleName = moduleName
d.name = funcName
d.debugName = wasmdebug.FuncName(moduleName, funcName, funcIdx)
d.paramNames = paramNames(localNames, funcIdx, len(d.funcType.Params))
for _, e := range m.ExportSection {
if e.Type == ExternTypeFunc && e.Index == funcIdx {
d.exportNames = append(d.exportNames, e.Name)
}
}
}
}
// FunctionDefinition implements api.FunctionDefinition
type FunctionDefinition struct {
moduleName string
index Index
name string
debugName string
goFunc interface{}
funcType *FunctionType
importDesc *[2]string
exportNames []string
paramNames []string
}
// ModuleName implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) ModuleName() string {
return f.moduleName
}
// Index implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) Index() uint32 {
return f.index
}
// Name implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) Name() string {
return f.name
}
// DebugName implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) DebugName() string {
return f.debugName
}
// Import implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) Import() (moduleName, name string, isImport bool) {
if importDesc := f.importDesc; importDesc != nil {
moduleName, name, isImport = importDesc[0], importDesc[1], true
}
return
}
// ExportNames implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) ExportNames() []string {
return f.exportNames
}
// GoFunc implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) GoFunction() interface{} {
return f.goFunc
}
// ParamNames implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) ParamNames() []string {
return f.paramNames
}
// ParamTypes implements api.FunctionDefinition ParamTypes.
func (f *FunctionDefinition) ParamTypes() []ValueType {
return f.funcType.Params
}
// ResultTypes implements api.FunctionDefinition ResultTypes.
func (f *FunctionDefinition) ResultTypes() []ValueType {
return f.funcType.Results
}