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
180
Capture Checking / Separation Checking 入門
tanishiking
0
600
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
Flow 不死:AI 時代 DevOps 的不變本質
cheng_wei_chen
2
500
クラウドファンディング版StackChan 3体(4体)をインタラクティブな体験型作品にして展示もした話 / スタックチャンお誕生日会2026
you
PRO
0
180
MySQL & MySQL HeatWave Report - June 2026
freshdaz
0
110
從開發到部署全都交給 AI:實作 AI 驅動的自動化流程
appleboy
0
160
本当の”仕事”を手放せる未来が見えた
mu7889yoon
0
110
AIに障害切り分けを全部やってもらった。 。 。 。
estie
0
110
AIネイティブな開発のサプライチェーンリスク対策 〜激動の開発現場でリスクに立ち向かう〜【ZennFes】
cscengineer
PRO
2
160
サイバーエージェントにおけるAI推進戦略と変革への取り組み
shotatsuge
0
530
フルAIで個人開発して学んだあれこれ / yuruai vol.1
isaoshimizu
0
110
GitHub Copilot app最速の発信の裏側
tomokusaba
1
250
新しいUbuntu/GNOMEが使いたいからXからWaylandへ移行頑張ってるの巻 2026-06-20
nobutomurata
0
160
「勝手に広まる」人気 AI エージェントを爆速で作ろう!(AWS Summit Japan 2026講演資料)
minorun365
PRO
10
2.5k
Featured
See All Featured
Balancing Empowerment & Direction
lara
6
1.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
210
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
240
The agentic SEO stack - context over prompts
schlessera
0
820
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
270
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
220
Technical Leadership for Architectural Decision Making
baasie
3
420
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Music & Morning Musume
bryan
47
7.2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
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 締め切り