second-state / WasmEdge-go

The GO language SDK and API for WasmEdge
https://www.secondstate.io/articles/extend-golang-app-with-webassembly-rust/
Apache License 2.0
107 stars 16 forks source link

Very slow execution func and AOT compile #30

Closed mirecl closed 2 years ago

mirecl commented 2 years ago

1. My OS: Mac OS Monterey (version 12.0.1) 2. Go version: 1.17.6 3. Tinygo: tinygo version 0.22.0 darwin/amd64 (using go version go1.17.6 and LLVM version 13.0.0) 4. My func for WASM:

package main

func main() {

}

//export Fibonacci
func Fibonacci(n int32) int32 {
    if n <= 1 {
        return n
    }
    return Fibonacci(n-1) + Fibonacci(n-2)
}

Command compile: tinygo build -o fib.wasm -target wasi fib.go 5. My main app:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/second-state/WasmEdge-go/wasmedge"
)

func main() {
    wasmedge.SetLogErrorLevel()
    conf := wasmedge.NewConfigure(wasmedge.WASI)
    store := wasmedge.NewStore()
    vm := wasmedge.NewVMWithConfigAndStore(conf, store)

    wasi := vm.GetImportObject(wasmedge.WASI)
    wasi.InitWasi(
        os.Args[1:],
        os.Environ(),
        []string{".:."},
    )
    wasmedge.SetLogErrorLevel()

    err := vm.LoadWasmFile("fib.wasm")
    if err != nil {
        fmt.Println("failed to load wasm")
    }
    vm.Validate()
    vm.Instantiate()

    res, err := vm.Execute("Fibonacci", uint32(40))
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(res)
}

6. Performance Comparison:

  1. Native Fibonacci(40) ~ 400ms
  2. WASM Fibonacci(40) ~ 32s 😪

❓Why slow:(?

7. AOT Compile: wasmedgec fib.wasm fib.wasm 8. Run app again:

fatal error: unexpected signal during runtime execution
[signal SIGBUS: bus error code=0x2 addr=0x41ff000 pc=0x45ee3ee]

runtime stack:
runtime.throw({0x40b9093, 0x45f0001})
        /usr/local/Cellar/go/1.17.6/libexec/src/runtime/panic.go:1198 +0x71
runtime.sigpanic()
        /usr/local/Cellar/go/1.17.6/libexec/src/runtime/signal_unix.go:719 +0x396

goroutine 1 [syscall]:
runtime.cgocall(0x4096f30, 0xc00005cdf8)
        /usr/local/Cellar/go/1.17.6/libexec/src/runtime/cgocall.go:156 +0x5c fp=0xc00005cdd0 sp=0xc00005cd98 pc=0x400529c
github.com/second-state/WasmEdge-go/wasmedge._Cfunc_WasmEdge_VMLoadWasmFromFile(0x4e5e7c0, 0x4e64350)
        _cgo_gotypes.go:2627 +0x49 fp=0xc00005cdf8 sp=0xc00005cdd0 pc=0x4090769
github.com/second-state/WasmEdge-go/wasmedge.(*VM).LoadWasmFile.func2(0x4e64350, 0x8)
       <MyPath>/go/pkg/mod/github.com/second-state/!wasm!edge-go@v0.9.2/wasmedge/vm.go:187 +0x51 fp=0xc00005ce38 sp=0xc00005cdf8 pc=0x40930f1
github.com/second-state/WasmEdge-go/wasmedge.(*VM).LoadWasmFile(0xc00005cf50, {0x40b3176, 0x1})
        <MyPath>/go/pkg/mod/github.com/second-state/!wasm!edge-go@v0.9.2/wasmedge/vm.go:187 +0x68 fp=0xc00005ce88 sp=0xc00005ce38 pc=0x4092f88
main.main()
        <MyPath>/main.go:25 +0x1d1 fp=0xc00005cf80 sp=0xc00005ce88 pc=0x4093f71
runtime.main()
        /usr/local/Cellar/go/1.17.6/libexec/src/runtime/proc.go:255 +0x227 fp=0xc00005cfe0 sp=0xc00005cf80 pc=0x40350a7
runtime.goexit()
        /usr/local/Cellar/go/1.17.6/libexec/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc00005cfe8 sp=0xc00005cfe0 pc=0x405e701
exit status 2

HELP ME:) ❓what am I doing wrong?

juntao commented 2 years ago

The interpreter could be very slow. So you do need AOT in order to get near native performance.

Can you add a print statement in main() and see if the AOT compiled Wasm file can run in the WasmEdge CLI? Thanks.

mirecl commented 2 years ago

@juntao Command: wasmedge --reactor fib.wasm Fibonacci 10 Respоnse: zsh: bus error wasmedge --reactor fib.wasm Fibonacci 10

q82419 commented 2 years ago

It's a known issue on MacOS 12. We are working for the bus error issue of AOT mode on Monterey now. So in the current state, maybe you can use the Linux platforms for the better supporting for WasmEdge-go. Thanks.

q82419 commented 2 years ago

Please refer to the issues: https://github.com/WasmEdge/WasmEdge/issues/1426 https://github.com/WasmEdge/WasmEdge/issues/1435

q82419 commented 2 years ago

Fixed this issue in this PR: https://github.com/WasmEdge/WasmEdge/pull/1589

Please use WasmEdge-go 0.10.1-alpha.2 or later versions. Thanks.