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

Towards Ruby 4 JIT / RubyKaigi 2022

Towards Ruby 4 JIT / RubyKaigi 2022

RubyKaigi 2022

Takashi Kokubun

September 08, 2022
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

  1. How does Ruby JIT work? 1 + 2 putobject 1

    putobject 2 opt_plus leave Ruby Abstract 
 Syntax 
 Tree Instruction 
 Sequence 
 (Bytecode)
  2. How does Ruby JIT work? 1 + 2 putobject 1

    putobject 2 opt_plus leave Ruby Abstract 
 Syntax 
 Tree Instruction 
 Sequence 
 (Bytecode) Machine 
 Code
  3. Current CRuby JITs • YJIT • Available since Ruby 3.1

    • --jit or --yjit • MJIT • Available since Ruby 2.6 • --mjit
  4. Current CRuby JITs • YJIT • Ruby 3.1: x86_64 only,

    no code GC, written in C • Ruby 3.2: arm64 support, (hopefully) code GC, written in Rust • MJIT • Ruby 3.1: Stable-ish, portable, native threads, written in C • Ruby 3.2: Experimental, fork + SIGCHLD, written in Ruby
  5. mjit.rb: Secret "standard library" in Ruby 3.2 • mjit.rb •

    Even more powerful than TracePoint • You can monkey-patch CRuby JIT • No compatibility guarantee • Every module is private, so const_get is required
  6. BYOJ: Bring Your Own JIT • Load and pause MJIT

    with --mjit=pause • Define RubyVM::MJIT.compile • Use RubyVM::MJIT.const_get(:C) to hack RubyVM • Call RubyVM::MJIT.resume to start JIT With Ruby 3.2:
  7. yjit-bench • yjit-bench has three kinds of benchmarks: 1. Headlining

    Benchmarks 2. Other Benchmarks 3. Micro Benchmarks
  8. 2. Other Benchmarks • binarytrees, fankuchredux, nbody • chunky_png •

    erubi, erubi_rails • lee • optcarrot • rubykon
  9. 3. Micro Benchmarks • 30k_ifelse, 30k_methods • cfunc_itself, str_concat •

    fib • getivar, setivar • keyword_args • respond_to
  10. My wish on Ruby 4 JIT • I want Ruby

    4 to be as fast as Java or JavaScript • Ruby 4's performance should be a reason to leave Python
  11. Ruby 4 Canary • true is mov-ed (immediate) • No

    opt_* VM instruction • Constant folding • Ruby / C method inlining
  12. Ruby 4 Canary’ • Single branch instruction to access @one

    • Single register to access two • No heap allocation • No stack frame
  13. 3. Method calls • Code locality • Method inlining: C

    㱻 Ruby • Pass arguments with native ABI • Deoptimization on redefinition or interruption (or TracePoint)
  14. Next Steps • We still have a lot of rooms

    for improvements on yjit-bench • More cross-instruction optimizations • More method inlining over Ruby and C
  15. Conclusion • Build your own JIT with Ruby 3.2 •

    Benchmark your JIT with yjit-bench