Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scala meets WebAssembly
Search
Rikito Taniguchi
March 13, 2025
Technology
1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Scala meets WebAssembly
Scala わいわい勉強会 #5 の発表資料
Rikito Taniguchi
March 13, 2025
More Decks by Rikito Taniguchi
See All by Rikito Taniguchi
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
190
Capture Checking / Separation Checking 入門
tanishiking
0
630
Implementing and Evaluating a High-Level Language with WasmGC and the Wasm Component Model: Scala’s Case
tanishiking
0
750
Why Wasm+WASI for Scala
tanishiking
0
140
Scala to WebAssembly: Exploring the How and Why
tanishiking
1
1.9k
Scala to WebAssembly #scala_waiwai
tanishiking
0
1.9k
Scala Days Madrid 2023 参加レポート
tanishiking
0
110
Tooling for Scala3
tanishiking
0
450
API-Based Code Search
tanishiking
0
110
Other Decks in Technology
See All in Technology
Genie Ontologyは銀の弾丸かを考える / Is Genie Ontology a Silver Bullet?
nttcom
0
400
アップデートで何が変わった?デモで学んで使いこなすIBM Bob2.0
muehara
0
120
Type-safe IaC for Dart
coborinai
0
160
AIコード生成×サプライチェーン攻撃 — PHPが直面する“二重の信頼問題
shinyasaita
0
200
「ちゃんとやっている」は独りよがりだった ― 不安に寄り添うインシデント対応へ / Towards incident response that addresses anxieties
chmikata
1
6k
生成AI×AWS CDK×AWS FISで"振り返れる"ミニGameDayをつくろう
yoshimi0227
1
400
SoccerMaster: A Vision Foundation Model for Soccer Understanding
kzykmyzw
0
140
SRE依存からの脱却 運用を開 発チームへ移す、 フルサイ クル開 発体制の実践
joooee0000
0
3.1k
SRE本の知られざる名シーン / The Hidden Gems of Google SRE Book
nari_ex
1
420
AIレビューはどこまで任せられるのか?自動化と人が背負うレビューの境界
sansantech
PRO
3
1.1k
Empower GenAI with Agile - あなたのアジャイルが生成AIのバフになる仕組み
hageyahhoo
1
210
CSに"SLO"は要らない、経営層に"99.9%"は伝わらない - SREを全社に"翻訳"する3原則
cscengineer
PRO
1
5k
Featured
See All Featured
Test your architecture with Archunit
thirion
1
2.3k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Context Engineering - Making Every Token Count
addyosmani
9
1k
The Limits of Empathy - UXLibs8
cassininazir
1
500
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
260
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
730
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.8k
Designing for Timeless Needs
cassininazir
1
370
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
460
Transcript
Scala meets WebAssembly Scala わいわい勉強会 #5
こんにちは〜 tanishiking (谷口力斗) Scala Compiler Engineer @ VirtusLab (ポーランドのIT企業) 最近は主に
Wasm backend の開発 Scalaわいわい勉強会主催の一人 大阪に住んでます
WebAssembly (Wasm) とは ブラウザやサーバー上で高速に動作するバイナリフォー マット (Java bytecode と似た立ち位置) C, Rust,
Go, Scala など様々な言語で書かれたコードを コンパイルし、Wasm VM上 で安全かつ効率的に実行 もともと Web の高速化 を目的に登場 Google meets の背景ぼかし・spreadsheet の計算ワーカーなど V8(Chrome) SpiderMonkey (firefox) WasmEdge (non-JS runtime)
Wasm beyond the browser 最近はブラウザにとどまらずクラウド・エッジコンピューティングなど幅広い用途で活躍 ✔ 高速: ネイティブ並みの実行速度 ※ ✔
起動早い: 従来のLinuxコンテナより高速に起動 ✔ 安全: サンドボックス化・ホスト環境への最小権限アクセス ✔ ポータブル : OS・アーキテクチャに非依存 Docker作者: 2008年にWasm+WASIがあった らDocker作ってなかった。 実際はコンテナのほうが優れているケースの方 が多いが、そう言わしめる程度のポテンシャル がある
Language agnostic な言語仕様 Wasmは多言語対応を目指し、特定の言語設計やプログラミングモデルに依存し ないミニマルな言語設計 例えばWasmGCではGC-managedなstructとarrayが追加されるだけ、classやvtableなどは コンパイラが頑張って実装する。 non-GCな機能に影響を与えない JVM (Java
bytecode) との対比 (In my opinion) Java BytecodeはJavaと同様のプログラミングモデルに最適化 /特化した設計(GC・クラス・オブ ジェクト・virtual dispatch…) コンパイラはVMの提供するリッチな機能を活用できるが、 “Java的でない”言語を Java bytecode にコンパイルするのは厳しい
最近のWasm動向: Wasm Component Model 異なる言語で書かれたWasmコードを簡単かつ安全に協働させるための仕組 • WIT (WebAssembly Interface Types):
高級なデータ型を使ったインタ フェースの記述ためのIDL • Wasm Component: 従来のWasm moduleと異なりインターフェース情報 (WIT)を持ち、言語間での相互運用が可能な形式 • CanonicalABI: Wasm コンポーネント間でデータを受渡すためのデータ表現 と変換ルール
何故 Scala を Wasm にコンパイルするのか ? • ブラウザ環境でのScala.jsのさらなる最適化 • サーバーサイドWasmにコンパイルしクラウドエッジ環
境でScalaを動かしたい • Component Model による他言語関数呼び出し
Scala の Wasm サポートの現状 (1/2) 2024年10月リリースの Scala.js 1.17.0 から実験的な Wasm
backend サポート WasmGC, Wasm Exception Handling を利用したJS依存なバイナリを生成 main.js と main.wasm を生成 JSにはwasmをinstantiateするのに 必要なコードが含まれる main.js を通してブラウザやnode.jsで 実行可能。 wasmtimeやwasmedgeは利用不可
Scala の Wasm サポートの現状 (2/2) JS backend vs Wasm backend
のベンチマーク比較 一部の(64bit)計算ヘビーなコードでは JS よりかなり高速 JS interop が遅く、現実的なほとんどのア プリケーションではJSの方が優位 Scala.js test-suite をビルドした コードサイズ 正直今のサポート状況では Wasmの 嬉しさはあまりない! 今後の開発への布石 size JS 14.5 MB JS + GCC 4.1 MB Wasm 6.5 MB wasm-optはGCコードのvalidationが まだうまく動かない...😭
Scala+Wasmの今後: 最適化・JS機能追加 Wasmバックエンドはまだまだ最適化の余地あり -> コード生成の最適化・wasm-optによる最適化など JS 単体では実現できない・難しかった機能の追加 JavaScript-promise integration による
async/await (将来)stack-switching (限定継続)による 効率的なgreen-threadsの実装
Scala+Wasmの今後: Server Side Wasm 現状言語コア・標準ライブラリにJS依存が含まれる。クラウドエッジ環境でのWasm活 用のためにJS依存を取り除きwasmtimeなどのランタイムで実行できるようにしてい く。 string実装・boxing・closure・浮動小数点の文字列化 , Regex,
Math, IO関連 ファイルシステムやネットワークなどのライブラリのためにはシステムコールへのアクセ スが必要 WASI (Wasm System Interface): ファイルシステムやネットワークなどのOS機能 へのアクセスを標準化するインターフェース WASI preview2 は Wasm Component Model proposal に依存
Scala+Wasmの今後: Wasm Component Model Wasm Component Model をサポー トすることで簡単に他言語関数呼び出 しできるように
WASI preview2 はこの Wasm Component Model に依存するので サーバーサイドWasmのためにはどの みち必要 ←開発中です!
Demo (1/3) 実は Component Model サポートの PoC はできている! Scala から
Rust のコードを実行してみよう #[allow(warnings)] mod bindings; use crate::bindings::exports::tanishiking::test::test::Guest; use ferris_says::say; struct Component; impl Guest for Component { fn ferris_say(content: String, width: u32) -> String { let mut buf = Vec::new(); say(content.as_str(), width.try_into().unwrap(), &mut buf).unwrap(); return String::from_utf8(buf).unwrap(); } } bindings::export!(Component with_types_in bindings); package tanishiking:
[email protected]
; world socket { // Scala import test; import wasi:cli/
[email protected]
; export wasi:cli/
[email protected]
; } world plug { // Rust export test; } interface test { ferris-say: func(content: string, width: u32) -> string; } WIT IDL 呼び出されるRust code (読まなくていいです)
Demo (2/3) @ComponentExport("wasi:cli/
[email protected]
") object Run extends component.Interface: def run(): component.Result[Unit,
Unit] = val out = Stdio.getStdout() val ferris = Test.ferrisSay("Hello Scala!", 80) out.blockingWriteAndFlush(ferris.getBytes()) component.Ok(()) @ComponentImport("tanishiking:test/
[email protected]
") object Test extends component.Interface: def ferrisSay(content: String, width: UInt): String = component.native wasi:cli/run interface を実装し てexportする (WASIp2 における start関数) 外部のcomponentで定義された ferris-say 関数を呼び出し ferris-say の facade
Demo (3/3) $ wasm-tools component embed wit main.wasm -o main.wasm
-w socket --encoding utf16 $ wasm-tools component new main.wasm -o main.wasm $ wac plug --plug plugin/target/wasm32-wasip1/release/plugin.wasm main.wasm -o out.wasm $ wasmtime -W function-references,gc out.wasm Scalaが生成したWasm moduleを component化 RustとScalaをcompose (link) wasmtime で実行
Demo (3/3) $ wasm-tools component embed wit main.wasm -o main.wasm
-w socket --encoding utf16 $ wasm-tools component new main.wasm -o main.wasm $ wac plug --plug plugin/target/wasm32-wasip1/release/plugin.wasm main.wasm -o out.wasm $ wasmtime -W function-references,gc out.wasm Scalaが生成したWasm moduleを component化 RustとScalaをcompose (link) wasmtime で実行 ______________ < Hello Scala! > -------------- \ \ _~^~^~_ \) / o o \ (/ '_ - _' / '-----' \
足りていないもの WasmVMの実装 (WasmGC, Exception, ComponentModel) WasmGC Exception Component Model WasmEdge
✔ ✔ ✘ WAMR ✔ ✔ ✘ wasmtime ▲experimental ✘ ✔ • Component Model サポートの安定化 • 言語コア・JDK API を pure Scala + WASI で再実装 • JUnit や sbt framework が wasmtime と一緒に動くように • JS interopを利用しているライブラリも再実装が必要 無限にやることある!
まとめ • Wasmはクラウド・エッジでも活用できる • Component Model による異言語間の関数呼び出し • Scala の
Wasm サポート ◦ 現状はブラウザ・JSでのみ利用可能 ◦ 今後は最適化・サーバーサイドWasm・Component Model サポートなどに注力
宣伝 ScalaDays 2025 開催!!! Google Summer of Code 2025 今年は2019年以来初のScalaの生まれ故郷EPFL
での開催 2019年も楽しかったので行きましょう Googleから給料もらいながら OSS開発できるプログラ ム。Scalaもやります! “gsoc scala 2025” Wasm開発手伝ってくれる人いたら相談ください (project list にはないけどなんとかなるかも ?) 4/8 締め切り