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

Scala to WebAssembly: Exploring the How and Why

Scala to WebAssembly: Exploring the How and Why

#ScalaMatsuri 2024

Rikito Taniguchi

June 08, 2024
Tweet

More Decks by Rikito Taniguchi

Other Decks in Technology

Transcript

  1. 3 • What is Wasm • Current Wasm Support in

    Scala • Why Wasm (in future) • How to Wasm • Future
  2. 4 What is WebAssembly (Wasm) WebAssembly は安全・高速・環境や言語に非依存なプログラム形式 WebAssembly はバイナリのプログラム形式 高速な起動と、サンドボックス実行などによる安全性

    “WebAssembly is a binary instruction format for a stack-based virtual machine…” Fast (startup) • Decoding Wasm binary is faster than parsing JS • Decode, validate, compile in stream / in parallel Secure • Runs in a sandboxed environment • Wasm design eliminates dangerous features from its execution semantic
  3. 6 • What is Wasm • Current Wasm Support in

    Scala • Why Wasm (in future) • How to Wasm • Future
  4. 7 ScalaのWasmサポートは Scala.js のバックエンドとして実装 Scala.js の中間言語から JS ではなく Wasm +

    JS helper を生成する Dialect of Scala targeting JavaScript platform • Frontend compiles Scala code to Scala.js IR (SJSIR) • Linker backend generates Wasm from SJSIR (instead of JS) Wasm Linker backend in Scala.js NEW!
  5. 8 Scala.js により生成される Wasm は JS ヘルパーも含まれる JS 環境で実行できるが、non-js runtime

    では実行できない Generated Wasm runs on JS environment, but it doesn’t run on non-JS runtimes (wasmedge, wasmtime…) Wasm Linker backend in Scala.js NEW! Doesn’t work on non-JS runtime yet
  6. 9 • Fast startup time • Fast execution (depends on

    app) • Small code size • Sandboxed environment that’s all? WasmはScala.jsに 高速起動・(アプリの性質によるが)実行速度・小さいコート サイズ・サンドボックス環境を提供。それだけ? Wasm provides Scala.js…
  7. 10 • What is Wasm • Current Wasm Support in

    Scala • Why Wasm (in future) • Wasm is beyond browsers • Wasm component Model • Evolving Landsacape • How to Wasm • Future
  8. 11 • Serverless/FaaS (Fastly edge, Fermyon Spin, CF workers) •

    Containers/k8s (containerd-wasm-shim) • Plugins/Extensions (VSCode, Envoy, UDF in TiDB) Wasm is beyond browser 近年では Serverless, containers, plugins の実行環境など ブラウザに限らない環境で活躍 Why? - Wasm’s fast startup time, sandboxing, and portability
  9. 12 Wasm module access host environment through a Standardized interfaces

    (WASI) to access system resources WASI (WebAssembly System Interface) WASI はシステムリソースにアクセスするための標準化されたインタフェース
  10. 13 WebAssembly Component Model Wasm module は i32 などの限られた型のみを使って import

    / export 文字列などの高度な型を表現する標準的なABIがない There’s no standard way to serialize high-level types across modules Wasm modules import/export functions only with i32, i64, f32, f64
  11. 14 WebAssembly Component Model Wasm component model は Wasm component

    間で高度な型を使って直接 やり取りする方法を提供 Defines standardized way to directly communicate between Wasm components using high-level types.
  12. 15 Canonical ABI A standardized way to represent data types

    in binary WIT (Wasm Interface Types) A language for describing the interfaces and data types. Tools generate glue code that conforms to the ABI from WIT. Component Model under the hood ABI とインターフェース定義言語により構成される
  13. 16 Proposals • WebAssembly SIMD proposal • Stack Switching proposal

    ◦ Native Delimited Control support (for green threads, etc) Tools • Warg (Wasm component registry) • wasm-opt Evolving LandScape SIMD サポート・軽量スレッドなど component model 周りのツールの進化
  14. 17 • What is Wasm • Current Wasm Support in

    Scala • Why Wasm (in future) • How to Wasm • Future
  15. 18 Wasm native GC was not well supported in 2019

    Without Wasm native GC, there’s a problem with interoperability between JavaScript and WebAssembly. (cycle collection problem) Why Scala hadn’t support Wasm? G “Scala.js and WebAssembly a tale of the dangers of the sea” from ScalaDays 2019 Lausanne, by Sébastien Doeraene (author of Scala.js) https://www.youtube.com/watch?v=QsOHofFJpig Wasmは2019年から考えられていたが、当時WasmGCが未成熟。適切にJSと の相互呼び出しができない
  16. 19 Compile VM to Wasm • Module size tends to

    huge • Good for legacy applications Embed own GC • GC need to inspect the stack to find GC roots, which isn’t possible in Wasm, requires a shadow stack. • Cycle-collection problem Compile Scala to Wasm in 2019 WasmGC以前にWasm対応するとしたら? JVM on Wasm・GCをWasm moduleに埋め込む。どちらにも問題がある
  17. 20 GC-managed (by Wasm runtime) data structures and instructions Wasm

    Garbage Collection (WasmGC) Directly compile GC-languages to Wasm without embedding GC Kotlin, Java, OCaml, Dart Wasm1.0には数値型のみ。WasmGCはstructやarrayなどのWasm runtime にGC管理されたデータ型を導入する。
  18. 21 Many Wasm runtimes support WasmGC by default in 2024

    • JS engines ◦ V8 (Deno 1.38 / Node.js 22 / Chrome 119) ◦ SpiderMonkey (Firefox 120) ◦ 🚧 JavaScriptCore (Safari, soon?) • Stand-alone Wasm VMs ◦ WasmEdge (0.14.0) ◦ 🚧 Wasmtime ◦ 🚧 WAMR WasmGC support status 2019年当時はWasmGCはearly stageだった。 今では多くの Wasm runtime が WasmGC をサポートしている。 It’s right time to support Wasm in Scala!
  19. 23 • Support non-JS Wasm ◦ Get rid of JS

    interop semantics ◦ WASI support ◦ Re-implement JS-dependent libs • Wasm Component Model Support • More optimizations ◦ Scala.js optimizer ◦ Better code generation ◦ wasm-opt Future 現状の実装はJS環境でのみ動作。将来的にはクラウド環境での利用・Wasm Component による任意の言語との interoperability を考えている
  20. 24 • Wasm is fast, safe, polyglot, and portable binary

    program format • Scala is compiling to WasmGC via Scala.js IR ◦ Currently, Scala supports Wasm on browser • Wasm has potential in non-JS environment + inter-communicate with different source languages. Conclusion Wasmはブラウザ外でも活躍し言語間のinteropのための新しいレイヤ Scala.jsからWasmGCを使って効率の良いバイナリを生成
  21. 25 ScalaNative compiles Scala to LLVM IR • Relies on

    LLVM, Emscripten, and WASI-SDK to support Wasm! • ScalaNative libs work out of the box in stand-alone Wasm runtime but… • LLVM doesn’t support WasmGC ◦ Need to embed GC to Wasm module, that is expensive ◦ GC cycle-collection problem Appendix: How about Scala Native? ScalaNative は LLVM を活用、LLVM経由でWasmサポートの検討 LLVM は WasmGC 対応しておらず、GCを組み込むことになる。