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
RustでつくるRubyのFiber
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
monochrome
May 30, 2021
Programming
330
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RustでつくるRubyのFiber
プログラミング言語が好きな人が集まるSlack
定期ミートアップ 2021/5/30
monochrome
May 30, 2021
More Decks by monochrome
See All by monochrome
RubyKaigi2026: Invariants in my own Ruby
sisshiki1969
0
28
Improving my own Ruby thereafter
sisshiki1969
1
260
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
1.1k
Improve my own Ruby
sisshiki1969
1
600
My own Ruby, thereafter
sisshiki1969
0
420
Running Optcarrot (faster) on my own Ruby.
sisshiki1969
1
340
仮想マシンにおけるスタックの管理
sisshiki1969
0
250
Rustでゴミ集め
sisshiki1969
1
390
Shinjuku.rs#15 Rustでつくるx86アセンブラ
sisshiki1969
0
1.8k
Other Decks in Programming
See All in Programming
業務時間外もAIに働いてもらう話
colorful12
3
9.9k
自分的「カンファレンスの楽しみ方」
syumai
0
190
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
1
260
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
0
400
バグを直したら useEffect が消えた
colorful12
3
830
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
260
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
4.3k
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
120
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
100
新卒PdEのリアル
ryu1013
1
340
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
170
Featured
See All Featured
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
Six Lessons from altMBA
skipperchong
29
4.5k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
290
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
We Have a Design System, Now What?
morganepeng
55
8.3k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
690
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
510
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Designing for humans not robots
tammielis
254
26k
Transcript
プログラミング言語Slack 定期ミートアップ 2021/05/30 RustでつくるRubyのFiber monochrome twitter: @s_isshiki1969
Agenda • RustでRubyの処理系を作ってます。 • 大きなアプリケーションやライブラリを動かせるよう頑張ってます。 • その過程で協調スレッド(コルーチン)を実装しました。
ruruby (https://github.com/sisshiki1969/ruruby) • Rust製のRuby実装 • 仮想マシンインタプリタ • 他の既存実装・仮想マシンへの依存なし • ガーベジコレクタを独自実装
• 3万行ぐらい
構成 virtual machine (VM) Rubyコード 抽象構文木 (AST) バイトコード parser codegen
a = 1 + 2 PUSH_INT 1 PUSH_INT 2 ADD SET_LVAR ‘a’ Add 1 1 1 2 3 Assign 2 LVAR ’a’
Optcarrot • Rubyで書かれたNESエミュレータ • 「Ruby3をRuby2の3倍速くする」目標のための準公式ベンチマーク • 実際にゲームができるモードと、ベンチマーク専用のモードがある • ベンチマークモードでは外部ライブラリを使用しない •
≒6500 LOC repo: https://github.com/mame/optcarrot • 紹介スライド: https://www.slideshare.net/mametter/optcarrot-a-pureruby-nes-emulator
https://www.slideshare.net/mametter/optcarrot-a-pureruby-nes-emulator NESの仕組み 本来はパラレルに動くCPUとGPUの処理を簡潔に記述するためにコルーチンを使用
Fiber (coroutine in Ruby) f = Fiber.new { .. }
f.resume(200) A B Fiber.yield ←100 f.resume(100) C f (child fiber) f = Fiber.new {|x| [ A ] Fiber.yield [ C ] } f.resume [ B ] f.resume [ D ] parent fiber D x ←200 〇2つの実行コンテキストが切り替わりながら処理が進む。 〇親は複数の子を生成できるが、親は1つのみ。
仮想マシンにおけるFiberの実装 f = Fiber.new { .. } f.resume A B
Fiber.yield f.resume C f (child fiber) parent fiber D • Fiberの生成 • resume(親Fiber→子Fiber) • yield(子Fiber→親Fiber)
OSスレッドによるFiberの実装 https://github.com/sisshiki1969/ruruby/bl ob/aa72942d46337574448dafb493ee27 bb4defb69d/src/value/fiber.rs#L124 欠点:重い、遅い
アセンブリによるFiber切り替えの実装 • Fiberの生成 ◦ Fiber用のマシンスタック領域を確保 • resume(親Fiber→子Fiber) ◦ レジスタを親スタックに保存 ◦
子のスタックへ切替 ◦ レジスタを子スタックから復帰 ◦ ret • yield(子Fiber→親Fiber) ◦ レジスタを子スタックに保存 ◦ 親のスタックへ切替 ◦ レジスタを親スタックから復帰 ◦ ret Fiber情報へのポインタ ガード関数ポインタ VM起動関数ポインタ レジスタ保存領域 子Fiberスタック -8 -16 -24 -80 中継関数ポインタ -32 ←sp
nix / x86-64 darwin / aarch64