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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
monochrome
May 30, 2021
Programming
0
310
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
210
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
960
Improve my own Ruby
sisshiki1969
1
460
My own Ruby, thereafter
sisshiki1969
0
370
Running Optcarrot (faster) on my own Ruby.
sisshiki1969
1
280
仮想マシンにおけるスタックの管理
sisshiki1969
0
230
Rustでゴミ集め
sisshiki1969
1
350
Shinjuku.rs#15 Rustでつくるx86アセンブラ
sisshiki1969
0
1.7k
fukuoka.rb#202 RustでつくるRuby
sisshiki1969
1
860
Other Decks in Programming
See All in Programming
AIプロダクト時代のQAエンジニアに求められること
imtnd
2
660
Go1.26 go fixをプロダクトに適用して困ったこと
kurakura0916
0
320
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
300
CSC307 Lecture 13
javiergs
PRO
0
310
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
240
AIとペアプロして処理時間を97%削減した話 #pyconshizu
kashewnuts
1
190
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
890
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
240
メタプログラミングで実現する「コードを仕様にする」仕組み/nikkei-tech-talk43
nikkei_engineer_recruiting
0
150
オブザーバビリティ駆動開発って実際どうなの?
yohfee
3
680
Raku Raku Notion 20260128
hareyakayuruyaka
0
430
Head of Engineeringが現場で回した生産性向上施策 2025→2026
gessy0129
0
210
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
240
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Believing is Seeing
oripsolob
1
68
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
190
The Invisible Side of Design
smashingmag
302
51k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
79
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
190
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
140
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