Upgrade to Pro — share decks privately, control downloads, hide ads and more …

My own Ruby, thereafter

monochrome
September 01, 2024
240

My own Ruby, thereafter

monochrome

September 01, 2024
Tweet

Transcript

  1. monoruby 1. GitHub - sisshiki1969/monoruby: Ruby implementation with yet another

    JIT compiler. 2. Rustで書かれたRuby処理系(個人&趣味開発) 3. インタプリタ・JITコンパイラを装備 4. パーサ・GCも独自開発 5. x86-64 / Linux のみサポート 6. 結構速い 7. RubyGems(読み込み)をサポートした
  2. 0xaa %dst %lhs %rhs 左辺のクラスID 右辺のクラスID 0x01 %dst CallsiteId 前回の関数ID

    METHOD_CALL 0x82 %rcv %args pos ADD_RR 8 bytes 8 bytes バイトコード レシーバクラスID タイムスタンプ オペコード オペランド 実行時情報 inline method cache (前回実行時の情報)
  3. Floatの演算の最適化 • Floatクラスの演算は遅い • 左辺 + 右辺 ◦ メモリから両辺のRubyオブジェクトを取ってくる ◦

    両辺をdoubleに変換 ◦ doubleの加算 ◦ 結果をdoubleからRubyオブジェクトへ変換 ◦ メモリへ格納
  4. Float or Integer any 3.14 3.14 xmm-n Float 3.14 xmm-n

    3.14 any Literal(3.14) Stack-xmm(n) xmm(n) Stack クラス 状態 スタック 浮動小数点数 レジスタ (xmm) state of registers
  5. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [#<Class:main>] :00009 ret %2 PI = 3.14 radius = 10 #something(evil) puts PI * radius * radius sample.rb bytecode
  6. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [#<Class:main>] :00009 ret %2 Literal(10) %1 %2 Stack
  7. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [Object] :00009 ret %2 3.14 3.14 Literal(10) %1 %2 Stack-xmm 00054: mov rax,QWORD PTR [rip+0x1ffd69d9] 0005b: cmp rax,QWORD PTR [rip+0x1ffd8dc2] 00062: jne 0xfff324c 00068: movq xmm2,QWORD PTR [rip+0x114] 00070: movabs rax,0x48f5c28f5c28fa 0007a: mov QWORD PTR [r14-0x40],rax xmm2
  8. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [Object] :00009 ret %2 10 3.14 3.14 Stack-xmm %1 %2 Stack-xmm 0007e: mov QWORD PTR [r14-0x38],0x15 00086: movq xmm3,QWORD PTR [rip+0x106] 10.0 xmm2 xmm3
  9. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [Object] :00009 ret %2 10 3.14 3.14 Stack-xmm %1 %2 Stack-xmm 0007e: mov QWORD PTR [r14-0x38],0x15 00086: movq xmm3,QWORD PTR [rip+0x106] 0008e: movq xmm4,xmm2 00092: mulsd xmm4,xmm3 10.0 31.4 xmm2 xmm3 xmm4
  10. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [Object] :00009 ret %2 10 3.14 3.14 Stack-xmm %1 %2 xmm 10.0 31.4 xmm4 xmm2 xmm3 0007e: mov QWORD PTR [r14-0x38],0x15 00086: movq xmm3,QWORD PTR [rip+0x106] 0008e: movq xmm4,xmm2 00092: mulsd xmm4,xmm3
  11. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [Object] :00009 ret %2 10 3.14 Stack-xmm %1 %2 xmm 00096: mulsd xmm4,xmm3 10.0 314.0 xmm3 xmm4
  12. :00001 %2 = literal[3.14] :00002 const[PI] = %2 :00003 %1

    = 10: i32 :00004 %2 = const[PI] [3.14] :00005 %2 = %2 * %1 [Float][Integer] :00006 %2 = %2 * %1 [Float][Integer] :00007 %2 = %0.puts(%2;1) [Object] :00009 ret %2 10 314.0 Stack-xmm %1 %2 Stack-xmm ... 000cf: movq xmm0,xmm4 000d3: call 0xfffd6ad5 000d8: mov QWORD PTR [r14-0x40],rax ... 0014c: call 0xfffdb8d5 ... 0016b: test rax,rax 0016e: je 0xfff32f0 00174: mov r15,rax 10.0 314.0 xmm3 xmm4
  13. PI = 3.14 radius = 10 something(evil) puts PI *

    radius * radius something evil is coming... =>314
  14. PI = 3.14 radius = 10 something(evil) puts PI *

    radius * radius alias something eval evil = “PI = ‘!’” =>!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!
  15. enemies of optimization 1. ブロック付きメソッド呼び出し 2. evalとその仲間たち 3. Proc 4.

    Binding 5. 基本演算メソッド再定義(Integer#+, ...) 6. TracePoint 7. ObjectSpace
  16. enemies of optimization 1. ブロック付きメソッド呼び出し 2. evalとその仲間たち 3. Proc 4.

    Binding 5. 基本演算メソッド再定義(Integer#+, ...) 6. TracePoint 7. ObjectSpace 実行時に検出してJITコードのパージ・イン タプリタの書き換え 値をスタックへ書き戻し 実行時に検出してインタプリタへ戻る