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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Rikito Taniguchi
March 13, 2025
Technology
990
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
170
Capture Checking / Separation Checking 入門
tanishiking
0
580
Implementing and Evaluating a High-Level Language with WasmGC and the Wasm Component Model: Scala’s Case
tanishiking
0
730
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
100
Other Decks in Technology
See All in Technology
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development with AI-DLC
yoshidashingo
0
140
TypeScript Compiler APIとPHP-Parserを活用し、TypeScriptとPHPで型を共有する
shuta13
0
360
AI Testing Talks: Challenges of Applying AI in Software Testing: From Hype to Practical Use
exactpro
PRO
1
140
AIプラットフォームを運用し続けるための可観測性
tanimuyk
4
1.1k
Agentic Web
dynamis
1
160
Diagnosing performance problems without the guesswork
elenatanasoiu
0
170
個人最適 から 全体最適 へ AI情報共有会・AIギルド・AI-DLC で進める カンリーの組織展開
rfdnxbro
0
1.7k
はじめてのDatadog
kairim0
0
290
AI フレンドリーなエラー監視を TypeScript で実現する
shinyaigeek
2
270
チームで実践する AI-DLC 思考の軌跡を残すチェックポイント設計
belongadmin
0
2.8k
SIer20年! 培ったスキルがスタートアップで輝く時
shucho0103
0
490
AI活用を推進するために ファインディが下した、一つの小さな決断
starfish719
0
260
Featured
See All Featured
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
350
Faster Mobile Websites
deanohume
310
31k
WCS-LA-2024
lcolladotor
0
620
Are puppies a ranking factor?
jonoalderson
1
3.5k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
280
Navigating Weather and Climate Data
rabernat
0
210
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
140
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
380
Building Flexible Design Systems
yeseniaperezcruz
330
40k
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 締め切り