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
The state of Swift for WebAssembly in 2022
Search
Yuta Saito
September 12, 2022
Technology
16k
7
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
The state of Swift for WebAssembly in 2022
https://fortee.jp/iosdc-japan-2022/proposal/fa87f700-1483-456b-9b28-ba7c119cfa69
Yuta Saito
September 12, 2022
More Decks by Yuta Saito
See All by Yuta Saito
Swift at Scale: Where Performance Really Comes From
kateinoigakukun
0
210
The Evolution of the CRuby Build System
kateinoigakukun
3
1.1k
Running Swift on WebAssembly Platforms
kateinoigakukun
0
240
What you can do with Ruby on WebAssembly
kateinoigakukun
1
1.9k
RubyGems on ruby.wasm
kateinoigakukun
1
6.1k
Building a Smaller App Binary
kateinoigakukun
2
710
The new linker in Xcode 15
kateinoigakukun
4
4.4k
Optimizing your Swift code
kateinoigakukun
0
2.6k
Ruby & WebAssembly Introduction
kateinoigakukun
0
3.1k
Other Decks in Technology
See All in Technology
Service Connect 上のサービスに ECS Service の外側から到達できなかった話
ota1022
1
120
LangfuseによるLLMOps基盤の構築と活用事例
zozotech
PRO
1
270
サイバー捜査員研修(後半)
nomizone
1
590
Data Hubグループ 紹介資料
sansan33
PRO
0
3.1k
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
8
7.6k
AI ネイティブな組織に Gemini Enterprise Agent Platform がなぜ必要なのか
asei
1
170
TypeScript入門 2026
recruitengineers
PRO
1
260
PLaMo 3.0 Primeの事後学習
pfn
PRO
0
270
[ChatGPT Work LT]事務作業が苦手な人のための バックオフィスの「半」自動化
chimaki_iot
0
190
初めてのGitHub Actions / GitHub Actions at First
tooppoo
0
140
JavaScript 研修 (2026)
recruitengineers
PRO
0
240
ウォーターフォール開発案件のPMとしてAI活用を模索している話
hatahata021
3
310
Featured
See All Featured
Tell your own story through comics
letsgokoyo
1
1k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Ruling the World: When Life Gets Gamed
codingconduct
0
290
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
270
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
460
How to Ace a Technical Interview
jacobian
281
24k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
460
My Coaching Mixtape
mlcsv
0
190
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.5k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Transcript
The state of Swift for WebAssembly in 2022 iOSDC 2022
@kateinoigakukun 1
About Me • @kateinoigakukun • Working at GoodNotes • SwiftWasm
ͷϝϯςφ • ৽ถ Swift / CRuby / LLVM ίϛολ • Love Swift ! 2
About this talk 1. Ϟνϕʔγϣϯ 2. SwiftWasm ͷϢʔεέʔε & σϞ
3. աڈ2ͷਐḿ 4. Future Work 3
Ϟνϕʔγϣϯ 4
Swiftͷ͖ͳͱ͜Ζ • ίϯύΠϧ͕௨Εେମಈ͘ • ਖ਼͍͠ίʔυΛ؆୯ʹॻͨ͘ΊͷIDEࢧԉ • ॊೈͳܕγεςϜ • ܕࢦ •
ConcurrencyͰਅՁΛൃش 5
SwiftͰΓ͍ͨ͜ͱ Run Swift anywhere • WebϒϥβͰಈ͘Swift • AndroidͰಈ͘Swift • ΫϥυϓϥοτϑΥʔϜͰಈ͘Swift
• SwiftͰॻ͚ΔϢʔβϓϥάΠϯ (e.g. VSCode Extension) 6
WebAssembly is a game changer A binary instruction format for
a stack-based machine Designed to be • Portable • Size- and Load-time efficient • Secure by Sandbox 7
WebAssemblyͷ࣮ߦϑϩʔ 8
SwiftWasm Project https://swiftwasm.org SwiftͷWebAssemblyରԠΛਐΊΔϓϩδΣΫτ • ίϯύΠϥΛͯ͠ύονΛૹͬͨΓɺ • WebAssembly͚ͷπʔϧνΣΠϯΛͨ͠Γɺ • JavaScriptKitͳͲͷपลϥΠϒϥϦͷ։ൃ
9
Demo: Run Swift on browser 10
11
12
ϓϩμΫγϣϯͰͷSwiftWasmͷϢʔεέʔε • GoodNotes1 • Note-taking app • Web Viewer for
sharing • Luminal2 • Python notebook on browser 2 https://getluminal.com/ 1 https://www.goodnotes.com/ 13
GoodNotesͷϨϯμϥ͕Web Ͱಈ͍ͯΔ༷ࢠ 14
WebAssembly is not only for Web 15
WebAssembly is not only for Web • αϯυϘοΫεʹΑΔ҆શੑɺϙʔλϒϧͳΞʔΩςΫνϟɺ ଟݴޠαϙʔτଞͷͰخ͍͠ •
ServerlessϓϥοτϑΥʔϜ • ϓϥάΠϯγεςϜ • and more… 16
WASI (WebAssembly System Interface) WebҎ֎ͰWasmΛಈ͔ͨ͢Ίͷඪ४γεςϜίʔϧن 17
https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ 18
https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ 19
WASI compatible things Platforms • Node.js / Deno / Wasmtime
• Fastly Compute@Edge • Cloudflare Workers • VSCode Extensions • Fluent Bit • Etc… Languages • C / C++ • Rust • Swift • Ruby • Etc… 20
Wasmಈ͔͢ڥίϯύΠϥخ͍͠ • ಈ͔͢ଆ: WebAssemblyϥϯλΠϜ͑͋͞Εɺ༷ʑͳݴޠΛ αϙʔτ͢Δ͜ͱ͕Ͱ͖Δ • ίϯύΠϥଆ: WebAssemblyʹίϯύΠϧͰ͖Εɺ༷ʑͳ ڥͰಈ͔ͤΔ 21
Demo: Swift CLI on WebAssembly 22
23
Web Ҏ֎ͷSwiftWasmͷϢʔεέʔε • Fastly Compute@Edge3 • Serverless platform based on
WebAssembly • Enarx4 • WebAssembly + Confidential Computing on TEE (Trusted Execution Environment) 4 https://enarx.dev/docs/webassembly/swift 3 https://www.fastly.com/jp/blog/introducing-serverless-swift-building-on-compute-edge-with-andrew-barba 24
Demo: Swift at Compute@Edge 25
26
For more info https://swift.cloud/blog/deploy-server-side-swift-to-fastly 27
ٕज़తͳਐḿ (2020~2022) • Relative PointerαϙʔτΛWasmʹՃ • 70Ҏ্ͷύονΛΞοϓετϦʔϜʹϚʔδ • Swift ConcurrencyʹରԠ
• Tokamak Fiber Renderer • μWASI 28
Relative Pointer SwiftͰΘΕ͍ͯΔϙΠϯλදݱͷςΫχοΫ • 64bitڥͰ32bitʹऩ·Δ • → όΠφϦαΠζخ͍͠ • Ґஔಠཱ
• → ʢେࡶʹݴ͏ͱʣ ΞϓϦىಈ͕ͯ͘خ͠ 29
Relative Pointer • Wasmͷ༷ͰදݱͰ͖ͳ͔ͬͨ template<typename BasePtrTy, typename Offset> uintptr_t applyRelativeOffset(BasePtrTy
*basePtr, Offset offset) { #ifdef __wasm__ // WebAssembly: hack: disable relative pointers return (uintptr_t)(intptr_t)offset; #endif auto base = reinterpret_cast<uintptr_t>(basePtr); auto extendOffset = (uintptr_t)(intptr_t)offset; return base + extendOffset; } 30
Relative Pointer ϋοΫਏ͍ • ίϯύΠϥͱϥϯλΠϜ྆ํʹେྔͷมߋ͕ඞཁ • ΞοϓετϦʔϜͰ৽͘͠Relative Pointerͷ༻͕૿͑Δͨͼ ʹέΞ͕ඞཁ 31
WasmʹRelative PointerαϙʔτΛՃ • WasmଆʹRelative Pointer ʹඞཁͳػೳΛఏҊ&࣮ 32
WebAssemblyͷͨΊʹSwiftΛ͢ 33
WebAssemblyͷͨΊʹSwiftΛ͢ SwiftͷͨΊʹWebAssemblyΛ͢ 34
ϋοΫ͕ফͤͯϋοϐʔ 35
Upstreaming patches • 44 patches for apple/swift • 16 patches
for apple/swift-corelibs-foundation • 14 patches for apple/swift-package-manager • 4 patches for LLVM They are made by only 2 people! 36
Swift Concurrency on Wasm JavaScriptKit provides a global executor implementation
based on JS event-loop import JavaScriptEventLoop import JavaScriptKit JavaScriptEventLoop.installGlobalExecutor() func printZen() async throws { let fetch = JSObject.global.fetch.function! let response = try await JSPromise(fetch("https://api.github.com/zen").object!)!.value let text = try await JSPromise(response.text().object!)!.value print(text) } 37
Tokamak Fiber Renderer • React 16Ҏ߱ͱಉͷΞϧΰϦζϜ • ਂ͘ωετͨ͠ViewͰελοΫΦʔόʔϑϩʔ͕ى͖ͳ͍ • ͜Ε·ͰͷStack
Reconciler30 viewsͷωετͰ ελοΫΦʔόʔϑϩʔ͍ͯͨ͠ • Fiber ReconcilerͰ700·ͰOK https://github.com/TokamakUI/Tokamak/pull/471 38
μWASI (swiftwasm/uwasi) • ܰྔWASI࣮ for ϒϥβ & Node.js • ैདྷͷ3rd-partyͷ࣮େ͖͗ͨ͢…
(65.6 KB) • େͷػೳͬͯͳ͔ͬͨ • SwiftWasmͰ৽͍͠ϞϊΛఏڙ: • Minimal: 3 KB • All features enabled: 6 KB https://github.com/swiftwasm/uwasi 39
Help wanted! • ·ͩ·ͩΔ͜ͱࢁ • ଟछଟ༷ͳқ/ϨΠϠ • ॿ͚͍ͯͩ͘͞ λεΫͷྖҬ 40
Future Work • KeyPathݺͼग़͠نΛSIL্ʹ੍ఆ • LLVM LTOʹΑΔίʔυݮΛnon-AppleϓϥοτϑΥʔϜʹҠ ২ • LLVMʹllvm.linker.usedΛՃͯ͠ɺϦϯΫ࣌GCΛ༗ޮԽ
• Web APIϕʔεͷFoundationNetworkingޓ࣮ • ܕ͖ͷJS <-> SwiftϒϦοδίʔυੜث 41
GoodNotes is hiring! https://www.goodnotes.com/careers • Senior Software Engineer, Cross Platform
(Swift, Wasm) • Senior Software Engineer, iOS • etc... 42
Thank you :) 43