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
280
RustでつくるRubyのFiber
プログラミング言語が好きな人が集まるSlack
定期ミートアップ 2021/5/30
monochrome
May 30, 2021
Tweet
Share
More Decks by monochrome
See All by monochrome
Improving my own Ruby thereafter
sisshiki1969
1
130
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
740
Improve my own Ruby
sisshiki1969
1
330
My own Ruby, thereafter
sisshiki1969
0
330
Running Optcarrot (faster) on my own Ruby.
sisshiki1969
1
230
仮想マシンにおけるスタックの管理
sisshiki1969
0
200
Rustでゴミ集め
sisshiki1969
1
330
Shinjuku.rs#15 Rustでつくるx86アセンブラ
sisshiki1969
0
1.7k
fukuoka.rb#202 RustでつくるRuby
sisshiki1969
1
820
Other Decks in Programming
See All in Programming
TanStack DB ~状態管理の新しい考え方~
bmthd
2
360
ライブ配信サービスの インフラのジレンマ -マルチクラウドに至ったワケ-
mirrativ
2
270
MLH State of the League: 2026 Season
theycallmeswift
0
170
ゲームの物理
fadis
5
1.6k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
230
AHC051解法紹介
eijirou
0
640
パッケージ設計の黒魔術/Kyoto.go#63
lufia
2
310
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
200
20250808_AIAgent勉強会_ClaudeCodeデータ分析の実運用〜競馬を題材に回収率100%の先を目指すメソッドとは〜
kkakeru
0
210
サイトを作ったらNFCタグキーホルダーを爆速で作れ!
yuukis
0
690
旅行プランAIエージェント開発の裏側
ippo012
1
470
TDD 実践ミニトーク
contour_gara
1
250
Featured
See All Featured
BBQ
matthewcrist
89
9.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Practical Orchestrator
shlominoach
190
11k
How GitHub (no longer) Works
holman
315
140k
Designing for humans not robots
tammielis
253
25k
The Language of Interfaces
destraynor
160
25k
Building Applications with DynamoDB
mza
96
6.6k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
GitHub's CSS Performance
jonrohan
1032
460k
Scaling GitHub
holman
462
140k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
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