Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What_s_up_Wasm_in_Go_.pdf

Taku Fukushima
March 13, 2023
20

 What_s_up_Wasm_in_Go_.pdf

Taku Fukushima

March 13, 2023
Tweet

Transcript

  1. 1
    What’s up Wasm in Go?
    GoでWebAssembly(Wasm)って今どんな感じなんでしたっけ?
    Taku Fukushima
    mercari.go #21
    Feb 28, 2023

    View Slide

  2. 2
    Abstract of Wasm binary written in Rust
    Agenda
    Demo
    What’s up Wasm in Go?
    Summary
    02
    03
    04
    01

    View Slide

  3. 3
    Abstract of Wasm binary
    written in Rust
    Chapter 1

    View Slide

  4. 4
    Goal: USPS address lookup service
    ● Required in customers’ address management
    ● ZIP Code validation and address completion in forms
    ● Current status: gRPC API in a microservice
    ● Address information is a static data
    ○ 41,704 lines of CSV: 1.6 MiB
    ○ Monthly updated
    ○ We retrieve uniquely formatted data from USPS, parse it and
    generate a CSV file with K8s CronJob
    ● No JS/browser dependency
    ZIP Code → Address Information

    View Slide

  5. 5
    画像

    Original data has all US address information in a line
    Our CSV data

    View Slide

  6. 6
    Make it Wasm binary
    ● CSV → binary data via Rust’s Build Scripts
    ○ We don’t want to load CSV at runtime
    ○ We want address data in binary
    ● Embed the binary into code via include_bytes!
    ● Compacted address info stored in Trie
    ○ std::collections::HashMap → rust-phf → fs_trie
    ● Export Wasm binary and JS compatibility layer via wasm-pack
    ZIP Code in string → Address Information in JSON

    View Slide

  7. 7
    Demo
    Chapter 2

    View Slide

  8. 8
    What’s up Wasm in Go?
    Chapter 3

    View Slide

  9. 9
    My past experience
    ● Go compiler has WASM target by default
    ○ GOOS=js GOARCH=wasm
    ○ With JavaScript glue
    ■ $(go env GOROOT)/misc/wasm/wasm_exec.js
    ○ Relatively bigger size Wasm binary
    ● TinyGo can generate binary with smaller footprint
    ○ tinygo build -o module.wasm -target wasi .
    Wasm binary generated from Go code contains Go runtime

    View Slide

  10. 10
    My current experience
    ● Still TinyGo is a good option
    ● go:embed can be used embedding binary data in Wasm binary
    ● Nice trie/radix tree library is needed with gob/binary format
    compatibility
    ○ github.com/hashicorp/go-immutable-radix did not get
    along with gob
    ○ * I just could not find one and there could be any out there
    Not changed so much since last time

    View Slide

  11. 11
    Future?
    ● GC
    ● Threads
    ● Does Go dream of lightweight Wasm binary output?
    New Wasm standards are added

    View Slide

  12. 12
    Summary
    Chapter 4

    View Slide

  13. 13
    Go in Wasm
    ● Default Go compiler supports
    ● Still TinyGo is a good option
    ● We could see some improvements in the future?
    ● Various matured Wasm runtime
    ○ github.com/wasmerio/wasmer-go
    ○ github.com/tetratelabs/wazero
    ○ …
    Go has nice Wasm supports

    View Slide

  14. 14
    End of Slides
    EOS

    View Slide