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
200
Capture Checking / Separation Checking 入門
tanishiking
0
650
Implementing and Evaluating a High-Level Language with WasmGC and the Wasm Component Model: Scala’s Case
tanishiking
0
760
Why Wasm+WASI for Scala
tanishiking
0
150
Scala to WebAssembly: Exploring the How and Why
tanishiking
1
1.9k
Scala to WebAssembly #scala_waiwai
tanishiking
0
2k
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
侵入は突然に 〜 IoTマルウェアと悪用される家庭の機器 ~ / When Intrusion Strikes: IoT Malware and the Abuse of Home Devices
nttcom
0
1.2k
信頼できるテスティングAIをどう育てるか?
odan611
0
180
MIRU 2026 チュートリアル
keisuke198619
0
620
【CEDEC2026】『GRANBLUE FANTASY: Relink - Endless Ragnarok』のバトル制作事例 ~最高のキャラゲーを目指して~
cygames
PRO
0
110
「待ち時間」の消滅と「自我消耗」の加速:生成AI時代のエンジニアを救うメンタル・リソース管理
poropinai1966
0
400
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
53k
AIQAのナレッジ構築について
qatonchan
1
140
新しい SLO が良い感じにハマっている話
z63d
4
2k
PLaMo 3.0 Primeの構造化出力サポート
pfn
PRO
0
170
データ組織の転換期 一足飛びしない段階的戦略
leveragestech
PRO
0
140
変化の早いClaude Codeを 書籍に落とし込む
oikon48
4
610
システム監視入門
grimoh
5
800
Featured
See All Featured
Accessibility Awareness
sabderemane
1
170
Code Reviewing Like a Champion
maltzj
528
40k
First, design no harm
axbom
PRO
2
1.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
340
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.7k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
400
Git: the NoSQL Database
bkeepers
PRO
432
67k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
450
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 締め切り