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
monochrome
May 30, 2021
Programming
0
260
RustでつくるRubyのFiber
プログラミング言語が好きな人が集まるSlack
定期ミートアップ 2021/5/30
monochrome
May 30, 2021
Tweet
Share
More Decks by monochrome
See All by monochrome
My own Ruby, thereafter
sisshiki1969
0
270
Running Optcarrot (faster) on my own Ruby.
sisshiki1969
1
180
仮想マシンにおけるスタックの管理
sisshiki1969
0
180
Rustでゴミ集め
sisshiki1969
1
300
Shinjuku.rs#15 Rustでつくるx86アセンブラ
sisshiki1969
0
1.6k
fukuoka.rb#202 RustでつくるRuby
sisshiki1969
1
780
RustでつくるRubyのFiber
sisshiki1969
0
460
Rustでつくるガーベジコレクタ
sisshiki1969
0
650
Other Decks in Programming
See All in Programming
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
320
15分で学ぶDuckDBの可愛い使い方 DuckDBの最近の更新
notrogue
3
830
Boos Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
620
AWS Step Functions は CDK で書こう!
konokenj
5
870
[JAWS DAYS 2025] 最近の DB の競合解決の仕組みが分かった気になってみた
maroon1st
0
170
Duke on CRaC with Jakarta EE
ivargrimstad
0
140
ABEMA iOS 大規模プロジェクトにおける段階的な技術刷新 / ABEMA iOS Technology Upgrade
akkyie
1
250
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
9
2.6k
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
260
Amazon Bedrockマルチエージェントコラボレーションを諦めてLangGraphに入門してみた
akihisaikeda
1
160
新宿駅構内を三人称視点で探索してみる
satoshi7190
2
130
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
7
4.2k
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
380
Thoughts on Productivity
jonyablonski
69
4.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Embracing the Ebb and Flow
colly
84
4.6k
Unsuck your backbone
ammeep
669
57k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
Adopting Sorbet at Scale
ufuk
75
9.2k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Agile that works and the tools we love
rasmusluckow
328
21k
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