1. われわれは、GCをX
倍遅くできる
We can make the GC X times slower
than the original
nari/中村 成洋
ネットワーク応用通信研究所
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
5. 自己紹介
✓ nari/中村 成洋
✓ @nari3, id:authorNari
✓ NaCl勤務
4/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
6. 自己紹介
✓ CRubyコミッタ
✓ 主にGCについて考える
✓ 「俺がGCだ」
(こうですか、わかりません><)
5/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
22. 話さないこと
✓ CGのこと
✓ クールなGCクラスの使い方
✓ Rubyのこと
21/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
23. 想定外だったこと
✓ まさかの大ホール
✓ マニアックすぎる懸念
22/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
24. でも
✓ @kakutani「ぶちかましてくださ
い」
✓ やりましょう
✓ お父さん張り切っちゃうぞ系
23/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
37. 竹内郁雄先生曰く
GCを備えた言語での生
産性は、GCを備えてい
ない同等の言語に比べ
て30%は増加します。
[cited from `ガベージコレクションのアルゴリズムと実装、
前書き']
36/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
38. ってことは
✓ 1日 = 8時間
✓ 10年 = 142400時間
✓ 42720時間 = 365日
37/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
43. オブジェクトとは
42/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
44. オブジェクトとは
43/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
45. Rubyヒープブロック
44/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
46. Rubyヒープ
45/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
47. CRubyのGC
✓ マーク&スイープ
✓ 生きているオブジェクトに印付け
(マーク)
✓ マークがないオブジェクト(ゴミ)を
掃除(スイープ)
46/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
48. ルートとは
47/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
49. こんなプログラム
48/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
50. マークフェーズ
49/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
51. マーク完了後のRubyヒープ
50/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
52. スイープ
51/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
56. 保守的なGC
✓ 偽ポインタ(false pointer)
✓ 実際には数値などの非ポインタ
✓ たぶんポインタじゃね?(保守的
思想)
55/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
57. 問題
✓ エアオブジェクトが増える
✓ 死んでいるのに生きていると見なされ
る
56/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
59. 現実的保守的GC問題
✓ エア老人問題
✓ 186歳だけど戸籍ある…
✓ 役人「何も言われないし、
きっと生きてるんだろうなぁ」
✓ 保守的思想 = 戸籍の保守的GC
58/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
60. 今日の話題
✓ CRubyGC
✓ LazySweepGC
✓ 未来的CRubyGC
59/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
64. Stop the world
✓ GCによるアプリの停止
✓ 停止時間が重要となるアプリでは
致命的
✓ ゲーム、ロボット
63/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
66. なぜ止まるか?
✓ アルゴリズム(マークスイープ)が
まずい
✓ 「GC最大停止時間」と
「データ量」が比例
✓ オブジェクトが増えれば増えるほ
ど止まる
65/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
71. LazySweep
✓ スイープの遅延
✓ オブジェクトが必要になってから
スイープ
✓ 空いたオブジェクトを割り当て
✓ スイープの負荷分散
70/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
87. revertの理由
✓ もう少し速くしたかった
✓ VM方面の方からもクレームが
✓ 根本的に実装がタコなんじゃ?
✓ 私の実力不足
86/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
97. なんやかんや作り直し
✓ 3月くらいに着手
✓ 執筆が一段落
✓ 5月くらいにコミット
✓ ruby1.9.2には間に合わなかった
96/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
98. trunkへのコミット
Fri May 28 19:37:47 2010 Narihiro Nakamura <authorNari@gmail.com>
* gc.c: use simple lazy sweep algorithm for response performance
gain. See [ruby-dev:41067].
* object.c: FL_MARK of some objects by lazy sweep is copied when
RVALUE is cloned. These objects are not marked in the mark phase.
So delete FL_MARK.
* class.c: ditto.
97/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
118. あれ、バグか…?
✓ 黒が残るのはファイナライズ待ち
オブジェクト
✓ Sweepが完全終わってからファイ
ナライズ
✓ 一瞬なので残っているように見え
る
117/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
128. LazySweep
127/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
129. LazySweep
128/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
130. メリット&デメリット
✓ メリット
✓ 大量に割り当て要求があったときの最
大停止時間の緩和
✓ マーク後すぐに大量のオブジェクト要求があると
マークスイープと一緒
129/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
131. メリット&デメリット
✓ デメリット
✓ カウント分、マークに少しだけ負荷
✓ マーク済みオブジェクトはカウントしな
いのでまだマシ
✓ スイープ時にカウント処理しなくていい
✓ malloc_limitの計算に使用(いらないかも?)
130/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
132. 実装上の工夫
✓ 最大停止時間の緩和
✓ Rubyヒープ構造改革
131/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
138. 問題
✓ 追加されるRubyヒープブロックが
どこに入るか分からない
✓ どこまでスイープしたか管理する
のが大変
✓ indexを持つ => 変わる…。
✓ アドレスを持つ => 変わる…。
137/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
140. 分離
139/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
141. スイープ対象がスッキリ
140/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
142. 追加も平気
141/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
143. メリット
✓ アドレスが変わらないので
LazySweepしやすい
✓ 前のパッチはフラグとか使って大変
だった
✓ 今後の改善にも生かせそう
142/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
144. その他、細かい仕様
✓ GC.startは今までと同じ
✓ M&Sでやる
✓ LazySweepとは別に用意
143/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
156. ビットマップマーキングの場合
✓ 空きヒープブロックの検知が速い
のでnon-freelistできるかもしれな
い
155/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
159. LazySweepの怖さ
✓ マークがオブジェクトに残ってい
る
✓ ※コレを消してしまうとスイープ時
に解放
158/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
161. オブジェクトのコピー
160/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
162. マークの時に問題が…
161/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
163. スイープ時に…
162/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
164. 時間差SEGV
✓ スイープ時にオブジェクトC, Dが
解放されてしまう
✓ 時間差でSEGVェ...
163/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
167. ベンチマーク
✓ bm_gc_flagmentation.rb
✓ 疑似的なフラグメンテーション
✓ 狙い:長時間動作のプログラムでどれ
くらいの改善?
166/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
168. GC総停止時間
167/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
169. GC最大停止時間
168/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
173. LazySweepはいつから使える?
✓ trunkにはコミット済み
✓ ruby 1.9.3(??)
172/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
174. 今日の話題
✓ CRubyGC
✓ LazySweepGC
✓ 未来的CRubyGC
173/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
179. 保守的GCを実装した言語処理
系はレア
✓ Ruby
✓ Gauche
✓ …?
178/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
180. 言語処理系を作るときの二択
✓ 保守的GC(Dead)か…
✓ 正確なGC(or Alive)か…
✓ 負の遺産?
179/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
181. よく言われる問題
✓ 偽ポインタ問題(別名:非実在オ
ブジェクト問題)
✓ コピーGC使えないよ問題
180/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
184. 偽ポインタは本当に多い?
✓ Rubyのオブジェクトは5ワード固
定
✓ チェックで大分弾ける
✓ 実アプリで問題になった例を聞か
ない
✓ どんくらいあるんだろう?
183/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
185. コピーGCって本当にいいことば
かり?
✓ CoWに悪い
✓ ※JRubyとかRubiniusとかどうよ?
✓ キャッシュに悪いかも
✓ GC後はキャッシュミス連発?
184/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
187. メリット
✓ コアのコード書くの楽
✓ C拡張ライブラリ書くのが楽
✓ コアの開発をドライブするために
生産性は大事
186/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
190. 近未来
✓ 大容量化
✓ 大量のオブジェクト生成
✓ Ruby≒Rails(サーバサイド)
189/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
194. ライトバリア
✓ ライトバリアは必須
✓ C拡張ライブラリに手を入れる
✓ 今までの資産が使えなくなる恐
れ?
✓ Ruby2.0で対応?
193/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
196. ライトバリア挿入支援
✓ ココにライトバリア入ってません
よ…的な警告を出す
✓ スムーズな移行
✓ GC本共著者の相川さんの研究が
あった
195/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
198. CRubyGCをどうしたいか?
✓ CRubyにあった進化をしたい
✓ 保守的GCでどれだけいけるか?
197/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
204. 賢い
オブジェクトアロケート
203/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
205. 考え方
✓ よく使われるものをよく使わせる
✓ あまり使われないものを後に使わ
せる
✓ 解放されやすくなる
✓ 本当か?(CPythonだからうまく
いった?)
204/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
207. 案
✓ マーク並列化
✓ マークの再帰をやめる
✓ シンボルなどをマーク対象から外
す
206/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
209. その他
✓ 特定の型を特別にアロケート(文
字列専用ヒープとか)
✓ BoehmGC化
208/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
212. 情報が欲しい
✓ こんなアプリでGCが遅いとか
✓ とにかく公開してくれるといい
✓ ご家庭のブログでも、MLでも、どこでも
211/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
214. いろいろ公開して欲しい
✓ Rejectされた論文とか
✓ せめてソースコードとか…
✓ 見える所に情報を書いてもらえる
と嬉しい
213/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
216. 大チャンス
✓ RubyGCはまだまだ途上です
✓ Rubyの中でも狙い目
✓ 未踏的なネタで一つどうですか?
215/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
217. 暇つぶしに
✓ やってみては
✓ 全然うまくいかない
✓ だから楽しい
✓ 最高の暇つぶし
216/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5
219. 今日のまとめ
✓ GCを遅く(遅延)できたよー
✓ 今後もコツコツ改善(コツコツ大
事)
✓ GCの妄想楽しいです(^q^)
218/221
われわれは、GCをX倍遅くできる - We can make the GC X times slower than the original Powered by Rabbit 0.6.5