Skip to content

Commit

Permalink
feat: compilation cache (#43)
Browse files Browse the repository at this point in the history
This PR is related to extism/extism#605

```
PS D:\dylibso\go-sdk> go test -benchmem -run=^$ -bench .
goos: windows
goarch: amd64
pkg: github.com/extism/go-sdk
cpu: 13th Gen Intel(R) Core(TM) i7-1365U
BenchmarkInitialize/noop-12                         385           3051739 ns/op         2942236 B/op       2751 allocs/op
BenchmarkInitializeWithCache/noop-12                2112            504269 ns/op         1686493 B/op       1554 allocs/op
```

Without using compilation cache: 3ms
with compilation cache: 0.5ms

Since we're already exposing `wazero.RuntimeConfig`, I think we just
need to mention it in the README
  • Loading branch information
mhmd-azeez authored Dec 5, 2023
1 parent df4f5ac commit c14b7a3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ exit, out, err = plugin.Call("count_vowels", []byte("Hello, World!"))
// => {"count": 3, "total": 6, "vowels": "aeiouAEIOU"}
```

### Enabling Compilation Cache
While Wazero (the underlying Wasm runtime) is very fast in initializing modules, you can make subsequent initializations even faster by enabling the compilation cache:

```go
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),
}

_, err := NewPlugin(ctx, manifest, config, []HostFunction{})
```

## Build example plugins

Since our [example plugins](./plugins/) are also written in Go, for compiling them we use [TinyGo](https://tinygo.org/):
Expand Down
50 changes: 50 additions & 0 deletions extism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,56 @@ func TestJsonManifest(t *testing.T) {
}
}

func BenchmarkInitialize(b *testing.B) {
ctx := context.Background()
cache := wazero.NewCompilationCache()
defer cache.Close(ctx)

b.ResetTimer()
b.Run("noop", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
manifest := Manifest{Wasm: []Wasm{WasmFile{Path: "wasm/noop.wasm"}}}

config := PluginConfig{
EnableWasi: true,
ModuleConfig: wazero.NewModuleConfig(),
RuntimeConfig: wazero.NewRuntimeConfig(),
}

_, err := NewPlugin(ctx, manifest, config, []HostFunction{})
if err != nil {
panic(err)
}
}
})
}

func BenchmarkInitializeWithCache(b *testing.B) {
ctx := context.Background()
cache := wazero.NewCompilationCache()
defer cache.Close(ctx)

b.ResetTimer()
b.Run("noop", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
manifest := Manifest{Wasm: []Wasm{WasmFile{Path: "wasm/noop.wasm"}}}

config := PluginConfig{
EnableWasi: true,
ModuleConfig: wazero.NewModuleConfig(),
RuntimeConfig: wazero.NewRuntimeConfig().WithCompilationCache(cache),
}

_, err := NewPlugin(ctx, manifest, config, []HostFunction{})
if err != nil {
panic(err)
}
}
})
}

func BenchmarkNoop(b *testing.B) {
ctx := context.Background()
cache := wazero.NewCompilationCache()
Expand Down

0 comments on commit c14b7a3

Please sign in to comment.