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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
Making sense of Google’s agentic dev tools
glaforge
1
280
Amazon EVS で VCF 9.0 / 9.1 のサポート開始まとめ
mtoyoda
0
310
最適な自走を最小限の支援で — M&Aで拡大する組織で少人数SREが挑んだ1年 / SRE NEXT 2026
genda
0
1.5k
AIレビューはどこまで任せられるのか?自動化と人が背負うレビューの境界
sansantech
PRO
3
1.1k
生成AI×AWS CDK×AWS FISで"振り返れる"ミニGameDayをつくろう
yoshimi0227
1
390
Devsumi 2026 Summer 人もAIも使える共通基盤を事業の加速装置にする~デザインシステム運用に学ぶ組織レバレッジ~ 渡辺 凌央
legalontechnologies
PRO
1
230
SoccerMaster: A Vision Foundation Model for Soccer Understanding
kzykmyzw
0
140
Gen3R: 3D Scene Generation Meets Feed-Forward Reconstruction
spatial_ai_network
0
130
LLM/Agent評価:トップ営業の発言を「正解」にする 〜暗黙的正解による評価を営業資産に変える〜
takkuhiro
1
230
壊して学ぶAWS CDK: そのcdk deployで消えるもの、残るもの
k_adachi_01
1
400
脱金融のフューチャー・デザイン / Future Design Beyond Finance
ks91
PRO
0
160
Empower GenAI with Agile - あなたのアジャイルが生成AIのバフになる仕組み
hageyahhoo
1
210
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
970
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
400
Game over? The fight for quality and originality in the time of robots
wayneb77
1
220
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
260
Art, The Web, and Tiny UX
lynnandtonic
304
22k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
45k
Speed Design
sergeychernyshev
33
1.9k
Amusing Abliteration
ianozsvald
1
230
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
380
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Navigating Weather and Climate Data
rabernat
0
370
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 締め切り