Skip to content
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

Fix performance degradation while host functions call #48

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/host-function-library/proto/greet_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/host-functions/greeting/greet_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gen/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {
}
ptr, size := %s(buf)
ptrSize := _%s(ptr, size)
%s(ptr)

ptr = uint32(ptrSize >> 32)
size = uint32(ptrSize)
Expand All @@ -132,6 +133,7 @@ func genHostFunctions(g *protogen.GeneratedFile, f *fileInfo) {
g.QualifiedGoIdent(method.Output.GoIdent),
g.QualifiedGoIdent(pluginWasmPackage.Ident("ByteToPtr")),
importedName,
g.QualifiedGoIdent(pluginWasmPackage.Ident("FreePtr")),
g.QualifiedGoIdent(pluginWasmPackage.Ident("PtrToByte")),
g.QualifiedGoIdent(method.Output.GoIdent),
))
Expand Down
54 changes: 54 additions & 0 deletions tests/fields/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"context"
"testing"

"github.com/knqyf263/go-plugin/tests/fields/proto"
)

func BenchmarkFields(b *testing.B) {
b.ReportAllocs()
ctx := context.Background()
p, err := proto.NewFieldTestPlugin(ctx)
if err != nil {
b.Fatal(err)
}

plugin, err := p.Load(ctx, "plugin/plugin.wasm")
if err != nil {
b.Fatal(err)
}
defer plugin.Close(ctx)

b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = plugin.Test(ctx, &proto.Request{
A: 1.2,
B: 3.4,
C: 5,
D: -6,
E: 7,
F: 8,
G: 9,
H: -10,
I: 11,
J: 12,
K: 13,
L: -14,
M: false,
N: "foo",
O: []byte("hoge"),
P: []string{"a", "b"},
Q: map[string]*proto.IntValue{
"key": {A: 15},
},
R: &proto.Request_Nested{
A: "samurai",
},
S: proto.Enum_A,
}); err != nil {
b.Fatal(err)
}
}
}
39 changes: 39 additions & 0 deletions tests/host-functions/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"os"
"testing"

"github.com/tetratelabs/wazero"

"github.com/knqyf263/go-plugin/tests/host-functions/proto"
)

func BenchmarkHostFunctions(b *testing.B) {
b.ReportAllocs()
ctx := context.Background()
mc := wazero.NewModuleConfig().WithStdout(os.Stdout)
p, err := proto.NewGreeterPlugin(ctx, proto.WazeroRuntime(func(ctx context.Context) (wazero.Runtime, error) {
return proto.DefaultWazeroRuntime()(ctx)
}), proto.WazeroModuleConfig(mc))
if err != nil {
b.Fatal(err)
}

// Pass my host functions that are embedded into the plugin.
plugin, err := p.Load(ctx, "plugin/plugin.wasm", myHostFunctions{})
if err != nil {
b.Fatal(err)
}
defer plugin.Close(ctx)

b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := plugin.Greet(ctx, &proto.GreetRequest{
Name: "Sato",
}); err != nil {
b.Fatal(err)
}
}
}
1 change: 1 addition & 0 deletions tests/host-functions/proto/host_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions wasm/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func ByteToPtr(buf []byte) (uint32, uint32) {

return uint32(uintptr(ptr)), uint32(len(buf))
}

func FreePtr(ptr uint32) {
C.free(unsafe.Pointer(uintptr(ptr)))
}