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でつくるガーベジコレクタ
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
monochrome
October 20, 2020
Programming
760
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rustでつくるガーベジコレクタ
shinjuku.rs #12
monochrome
October 20, 2020
More Decks by monochrome
See All by monochrome
RubyKaigi2026: Invariants in my own Ruby
sisshiki1969
0
17
Improving my own Ruby thereafter
sisshiki1969
1
250
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
1k
Improve my own Ruby
sisshiki1969
1
590
My own Ruby, thereafter
sisshiki1969
0
410
Running Optcarrot (faster) on my own Ruby.
sisshiki1969
1
320
仮想マシンにおけるスタックの管理
sisshiki1969
0
240
Rustでゴミ集め
sisshiki1969
1
380
RustでつくるRubyのFiber
sisshiki1969
0
330
Other Decks in Programming
See All in Programming
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
130
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
140
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
290
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
200
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
350
FDEが実現するAI駆動経営の現在地
gonta
2
240
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
1.1k
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
440
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
1
260
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.3k
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
130
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
160
Featured
See All Featured
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
The Cult of Friendly URLs
andyhume
79
7k
Unsuck your backbone
ammeep
672
58k
Paper Plane
katiecoart
PRO
2
52k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
780
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
190
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
640
Google's AI Overviews - The New Search
badams
0
1.1k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
330
Transcript
Rustでつくるガーベジコレクタ monochrome twitter: @s_isshiki1969 Slack: プログラミング言語処理系が好きな人の集まり https://prog-lang-sys-ja-slack.github.io/wiki/
ruruby (https://github.com/sisshiki1969/ruruby) • 純Rust製のRuby実装 • 仮想マシンインタプリタ • 他の既存実装・仮想マシンへの依存なし • Rubyの標準ライブラリはRustで実装
• ガーベジコレクタを独自実装 • 2万行ぐらい
• 動的型付けのオブジェクト指向言語 • クラス定義、メソッド定義など、全てが動的 • 豊富なメタ言語機能 • Cで書かれている(50万行ぐらい)。 Ruby
構成 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’
オブジェクトの内部表現
Garbage collector Mutator (VM) Allocator ヒープ割り当て Collector 不要なオブジェクトの回収 割り当て要求 GC起動
*mut RValue
Page 0 7 8 15 16 20 23 24 0x4_0000
20 mark bitmap 0 7 64b x 4032 slot ≒ 256Kb
Allocator Free list 64b x 4032 slot ≒ 256Kb
Allocator Free list
Collector 起動 Free list
Garbage collection: Mark local variables method Array local variables method
Hash K V K V K V K V Object Instance variables Free list
None
Garbage collection: Sweep local variables method Array local variables method
Hash K V K V K V K V Object Instance variables Free list
None
Collector 処理終了 Free list
Allocator Free list
benchmark: aobench.rb (GC on / off)
バグりがちな点 var = [o1, o2, o3].each { |x| x.to_s }
配列オブジェクトをイテレートし、 各要素を関数で処理して戻り値を集め 配列を作って返すRubyスクリプト
バグりがちな点
①VM実行中にGCが起動すると… バグりがちな点 GC! ②作成中のオブジェクト が回収されてしまう ③VMは気づかず処理続行 → いつの間にか上書きされてバグる
まとめ • GCつくりました。 • GCバグるとつらい。