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

Ruby 3 JIT's roadmap / RubyConf China 2020

Ruby 3 JIT's roadmap / RubyConf China 2020

RubyConf China 2020 http://www.rubyconfchina.org/

Takashi Kokubun

August 15, 2020
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

  1. Agenda 1. What can Ruby's JIT do? 2. Ruby 3

    JIT's roadmap 3. Recent progress in Ruby 3 4. Current challenges
  2. What we can do with Ruby's JIT • Optimize Ruby

    methods to native code for hot spots • Eliminate VM interpretation cost: SP / PC • Optimize based on what C compiler can know • Ruby VM-specific optimizations we implemented
  3. What we CAN'T do with Ruby's JIT • Optimize a

    short-running program • JIT needs time to optimize many methods • Things may be slower while a C compiler is running • Optimization based on native code generated by C compiler • Deoptimization based on native insn pointer / stack pointer
  4. Use case: Obviate micro optimizations OVN[FSP OVN JUFSBUJPOTFD 3VCZ 

                    7. +*5
  5. Ruby JIT's goals • Optcarrot: 3x faster than Ruby 2.0

    • Sinatra, Rails: 10% throughput increase vs VM
  6. mame/optcarrot 3VCZ 3VCZ 3VCZ GSBNFTTFD     

          7. +*5
  7. benchmark-driver/sinatra 3VCZ 3VCZ 3VCZ SFRVFTUTTFD     

                7. +*5
  8. What should we do? • Current status: • Programs like

    Optcarrot run faster • Sinatra, Rails are still slightly slower than no-JIT mode • Let’s take a look at each of major Ruby features and JIT core
  9. Ruby 3 JIT's roadmap 1. Variables / Constants 2. Method

    inlining 3. Constant folding 4. Object allocation 5. Deoptimization 6. Scalability
  10. 1. Variables / Constants • Local variables: ⚠ • Instance

    variables: ✅ • Global variables: ❌ • Constants: ❌
  11. 2. Method inlining • Ruby method: ✅ • C method:

    ✅ • super: ⚠ • yield: ⚠
  12. Decrease ICache misses • ICache: Instruction Cache • Sinatra /

    Rails spends a lot of time on ICache misses • And the amount is increased by JIT
  13. Merge type checks on ivar access • Instance variable index

    is class-specific • We can check class only once per method
  14. Merge type checks on ivar access 30CKFDU qBHT FNCFEGBMTF OVNJW

    JWQUSˠIFBQ JW@JOEFY@UCM 30CKFDU qBHT FNCFEUSVF BSZ<> BSZ<> BSZ<>
  15. Merge type checks on ivar access 3VCZ 3VCZ GSBNFTTFD 

            7. +*5
  16. Inline C method call • Inlining C method had been

    hard because of: • Difficulty of detecting whether it’s safe to omit a call frame or not • Lots of indirection between method call and actual C function
  17. Inline C method call • We introduced a new type

    of method definition in CRuby core, called “builtin method”
  18. Inline C method call • We also added a way

    to “annotate” a C function in “builtin method” • Now we can say it’s safe to inline a C function
  19. Allow exception on "inline" method • A method which raises

    an exception can't have “inline" • But many methods raise: • TypeError • NoMemoryError • We could lazily update backtrace and others?
  20. Optimize VM -> JIT call • VM -> JIT call

    is slower than VM -> VM call • We might be able to offset icache miss's slowness by this • Prepare a fastpath / VM insn specialized for JIT call
  21. Optimize VM -> JIT call def foo3 nil end def

    foo2 nil end def foo1 nil end Time to call a method returning nil (ns) 0 8 16 24 32 Number of called methods 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 VM JIT
  22. Improve inlining decision • Rails has polymorphic methods • If

    inlined by a specific caller, class can be specific for each caller
  23. … and many more things in the roadmap • Inline

    `super` and `yield` • Optimize local variables • Optimize constants • …
  24. Summary • We reviewed Ruby 3 JIT's roadmap and what

    we've implemented. • While it's not useful for Rails yet, we've had progress towards that.