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

Ruby JIT Hacking Guide / RubyKaigi 2023

Ruby JIT Hacking Guide / RubyKaigi 2023

Ruby JIT Hacking Guide
https://rubykaigi.org/2023/

Takashi Kokubun

May 13, 2023
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

  1. Railsbench Speedup relative to 2.5 No JIT (s/s) 0 0.5

    1 1.5 2 Ruby 2.5 Ruby 2.6 Ruby 2.7 Ruby 3.0 Ruby 3.1 Ruby 3.2 Ruby 3.3 1.93 1.85 1.54 1.59 1.2 1.25 1.08 1.05 1.07 1.26 1.24 1.12 1.09 1.05 1.07 1 No JIT MJIT / RJIT YJIT
  2. Using YJIT • Install Rust and then build Ruby •

    Use --yjit or export RUBY_YJIT_ENABLE=1
  3. How Ruby JIT works 1 + 2 putobject 1 putobject

    2 opt_plus leave Ruby Abstract 
 Syntax 
 Tree Instruction 
 Sequence 
 (Bytecode)
  4. How Ruby JIT works 1 + 2 putobject 1 putobject

    2 opt_plus leave Ruby Abstract 
 Syntax 
 Tree Instruction 
 Sequence 
 (Bytecode) Machine 
 Code
  5. x86_64 assembly • add,sub: arithmetic instruction • rax: temporary register

    stack[0] = 3 stack[1] = 5 temp = stack[0] temp -= 1 temp += stack[1]
  6. x86_64 assembly • jo: jump if over fl ow •

    rsi: register for stack[0] stack[0] = 3 stack[1] = 5 temp = stack[0] temp -= 1 temp += stack[1] jump if overflow stack[0] = temp
  7. Calling a custom JIT Ruby 3.2 Ruby 3.3+ 1. Run

    Ruby with --mjit=pause --rjit=pause 2. Override RubyVM::MJIT.compile RubyVM::RJIT#compile 3. Call RubyVM::MJIT.resume RubyVM::RJIT.resume
  8. Ruby JIT Challenge Hashtag: #ruby-jit-challenge Speedup relative to No JIT

    (s/s) 0 3 6 9 12 No JIT RJIT YJIT Ruby JIT 11.08 6.31 3.75 1 Fibonatti benchmark