• A compilation target for languages like C, C++ and Rust C、C++、Rust等の言語のコンパイル対象 • Enabling high performance web applications 高性能なウェブアプリケーションを実現する • Increasingly used on the server side サーバーサイドでの利用が増加している 4
that works on browsers? 🤔 WASMはブラウザでも動作する FFI ? • FFI lets code in one language directly call code in another language FFIによって一つの言語で書かれたコードが別の言語のコードを直接呼び出すことができる • FFI is useful when performance is crucial and the logic is implemented in low-level languages like C, C++ and Rust FFIはパフォーマンスが重要かつ低レベル言語( C、C++、Rust等)でロジックが実装されている場合に有用 12
WASM module size is 2 MB according to golang-wiki golang-wikiによれば、Goの最小WASMモジュールサイズは 2MB • Python’s WASM module size 25 MB for add(a, b) using py2wasm py2wasmを用いると、 PythonのWASMモジュールサイズは 25MB 22
WASM due to the lack of performance enhancements and development complexity Zaplibのポストモーテム – パフォーマンスの期待外れや開発の複雑さのため、 WASMを放棄したスタートアップ • Tree-shaking, the horticulturally misguided algorithm – A highlight in the immaturity of tree-shaking in WASM toolchains ツリーシェイキング、園芸的に誤った使い方のアルゴリズム – WASMのツリーシェイキングの未熟さを強調している 23
using a hexagonal grid. Buildings for Java, JavaScript, Python and others are available h3 – 六角形グリッドを使用した地理空間インデックス。 Java、JavaScript、Pythonなどで利用可能 • h3-js – JavaScript version of h3. The core library is compiled from C to WASM using emscripten h3-js – h3のJavaScriptバージョン。 emscriptenを使ってCからWASMでコンパイルされている 25
2 1 10 16 1. Start with any positive integer n 任意の正の整数nで開始 2. If n is even, divide it by 2 nが偶数なら2で割る 3. If n is odd, multiply it by 3 and add 1 nが奇数なら3倍して1を足す 4. Repeat the process until n becomes 1 nが1になるまでこのプロセスを繰り返す 28
u32, b: u32) -> u32 { a + b } JavaScript WASM Shared Linear Memory write a aを書き込む write b bを書き込む Read result 結果を読み込む Read a aを読み込む Read b bを読み込む Write result 結果を書き込む 33
unit tests from the original JavaScript implementation オリジナルの JavaScript実装から単体テストを借りてくる 2. Translate JavaScript to Rust JavaScriptをRustに翻訳する 3. Bridge JavaScript and Rust JavaScriptとRustのブリッジコードを書く 44
// Before let mut chars = Vec::with_capacity(len); for index in 0..len { chars.push(ENCODING.chars().nth(index).unwrap()); } // After let mut chars = Vec::with_capacity(len); let encoding_bytes = ENCODING.as_bytes(); for index in 0..len { chars.push(encoding_bytes[index] as char); } 47
TIME_LEN: usize = 10; // Before for i in 0..TIME_LEN { time += i as f64 * (ENCODING_LEN as u64).pow(index as u32) as f64; } // After let powers = [1.0, 32.0, ..., 35184372088832.0] for i in 0..TIME_LEN { time += i as f64 * powers[index]; } 48
works differently than FFI, designed to run faster and more efficiently 独自のアーキテクチャ – WASMがV8上で動作する際、 FFIとは異なりより速く効率的に動作するように設計されている • Backend efficiency – WASM is highly effective for backend today, where performance is critical バックエンドの効率 – WASMはバックエンドのパフォーマンス向上に効果的で、性能が重要な場面で有用 • Frontend potential – Currently limited by large binary sizes, but toolchains will address this バックエンドの効率 – WASMはバックエンドで非常に効果的であり、性能が重要な場合に有用 51