Optimizes slice initializations (#842)
Signed-off-by: Clifton Kaznocha <ckaznocha@users.noreply.github.com> Co-authored-by: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com>
This commit is contained in:
@@ -479,16 +479,16 @@ func (e *engine) CompileModule(ctx context.Context, module *wasm.Module) error {
|
||||
|
||||
// NewModuleEngine implements the same method as documented on wasm.Engine.
|
||||
func (e *engine) NewModuleEngine(name string, module *wasm.Module, importedFunctions, moduleFunctions []*wasm.FunctionInstance, tables []*wasm.TableInstance, tableInits []wasm.TableInitEntry) (wasm.ModuleEngine, error) {
|
||||
imported := uint32(len(importedFunctions))
|
||||
imported := len(importedFunctions)
|
||||
me := &moduleEngine{
|
||||
name: name,
|
||||
functions: make([]*function, 0, imported+uint32(len(moduleFunctions))),
|
||||
importedFunctionCount: imported,
|
||||
functions: make([]*function, imported+len(moduleFunctions)),
|
||||
importedFunctionCount: uint32(imported),
|
||||
}
|
||||
|
||||
for _, f := range importedFunctions {
|
||||
for i, f := range importedFunctions {
|
||||
cf := f.Module.Engine.(*moduleEngine).functions[f.Idx]
|
||||
me.functions = append(me.functions, cf)
|
||||
me.functions[i] = cf
|
||||
}
|
||||
|
||||
codes, ok, err := e.getCodes(module)
|
||||
@@ -501,7 +501,7 @@ func (e *engine) NewModuleEngine(name string, module *wasm.Module, importedFunct
|
||||
for i, c := range codes {
|
||||
f := moduleFunctions[i]
|
||||
function := c.createFunction(f)
|
||||
me.functions = append(me.functions, function)
|
||||
me.functions[imported+i] = function
|
||||
}
|
||||
|
||||
for _, init := range tableInits {
|
||||
|
||||
@@ -2,9 +2,9 @@ package bench
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
@@ -89,9 +89,12 @@ func runCompilation(b *testing.B, r wazero.Runtime) wazero.CompiledModule {
|
||||
func runInitializationBench(b *testing.B, r wazero.Runtime) {
|
||||
compiled := runCompilation(b, r)
|
||||
defer compiled.Close(testCtx)
|
||||
// Configure with real sources to avoid performance hit initializing fake ones. These sources are not used
|
||||
// in the benchmark.
|
||||
config := wazero.NewModuleConfig().WithSysNanotime().WithSysWalltime().WithRandSource(rand.Reader)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
mod, err := r.InstantiateModule(testCtx, compiled, wazero.NewModuleConfig())
|
||||
mod, err := r.InstantiateModule(testCtx, compiled, config)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -590,28 +590,26 @@ func (m *Module) buildGlobals(importedGlobals []*GlobalInstance) (globals []*Glo
|
||||
// - This is exported for tests that don't call Instantiate, notably only
|
||||
// enginetest.go.
|
||||
func (m *ModuleInstance) BuildFunctions(mod *Module, listeners []experimental.FunctionListener) (fns []*FunctionInstance) {
|
||||
fns = make([]*FunctionInstance, 0, len(mod.FunctionDefinitionSection))
|
||||
for i := range mod.FunctionSection {
|
||||
importCount := mod.ImportFuncCount()
|
||||
fns = make([]*FunctionInstance, len(mod.FunctionSection))
|
||||
for i, section := range mod.FunctionSection {
|
||||
code := mod.CodeSection[i]
|
||||
fns = append(fns, &FunctionInstance{
|
||||
d := mod.FunctionDefinitionSection[uint32(i)+importCount]
|
||||
f := &FunctionInstance{
|
||||
IsHostFunction: code.IsHostFunction,
|
||||
LocalTypes: code.LocalTypes,
|
||||
Body: code.Body,
|
||||
GoFunc: code.GoFunc,
|
||||
TypeID: m.TypeIDs[mod.FunctionSection[i]],
|
||||
})
|
||||
}
|
||||
|
||||
importCount := mod.ImportFuncCount()
|
||||
for i, f := range fns {
|
||||
d := mod.FunctionDefinitionSection[uint32(i)+importCount]
|
||||
f.Module = m
|
||||
f.Idx = d.index
|
||||
f.Type = d.funcType
|
||||
f.Definition = d
|
||||
TypeID: m.TypeIDs[section],
|
||||
Module: m,
|
||||
Idx: d.index,
|
||||
Type: d.funcType,
|
||||
Definition: d,
|
||||
}
|
||||
if listeners != nil {
|
||||
f.Listener = listeners[i]
|
||||
}
|
||||
fns[i] = f
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user