Make Ruby 2.6 Faster with JIT

Make Ruby 2.6 Faster with JIT

An introduction to MJIT in Ruby 2.6 and up. What does it do? How do you use it? How well does it work? What can go wrong?


Noah Gibbs

August 02, 2018


  1. Make Ruby 2.6 Faster with JIT Noah Gibbs, AppFolio @codefolio

    / Pictures are by Arthur Rackham, and are in the Public Domain.
  2. Thank You, AppFolio! AppFolio pays me to write and speak.

    Thank you, AppFolio! If you’re looking for a Ruby job in Southern California, talk to us!
  3. I Often Talk Too Fast. If you call out “hey,

    slow down!”, I’ll try to go slower. And it will help your fellow audience members!
  4. JIT: What Is It? “Effortlessly Optimizing Ruby”

  5. Compiling Ruby interprets your code as it runs. C++ (and

    others) compile to machine code before. “Compiling a C++ Program”
  6. JIT “Just-In-Time” compiling translates part of your app to fast

    machine code. “Running Faster Without Effort”
  7. Does JIT Work? TruffleRuby JIT gives 10x+ speedup over interpreted.

  8. Does JIT Work? (2) Ruby 2.6 experimental JIT is still

    prerelease. But it gets 2.5x+ for the same benchmark. “Ruby JIT Shows Early Promise”
  9. Tradeoffs of JIT Nothing is perfect. JIT has two main

    disadvantages: memory usage and warmup. “Considering JIT’s Tradeoffs”
  10. Memory Usage (1) JIT tracks method calls: • How many

    calls? • How much runtime? • With what argument types?
  11. Memory Usage (2) JIT keeps 2+ copies of a method

    and switches back to un-optimized if required. “De-optimizing a Method”
  12. Warmup JITted programs speed up as they compile methods. There

    is “warmup time” as new programs start out slower.
  13. Warmup vs Startup If compiling is high priority, early program

    speed is very slow. JVM is infamous for this. “Ruby Dancing with the JVM”
  14. Long Programs Servers and batch jobs benefit from JIT. Small

    quick programs get little or nothing. “Rake task dodging a JIT penalty”
  15. Why Didn’t Ruby Get JIT Sooner? “Ruby 2.5 Mocked for

    No JIT”
  16. JIT and Ruby Core JIT’s disadvantages are Ruby advantages -

    memory, startup. JIT was a tough sell. “Core Dev Inspects JIT Attempt”
  17. JIT and Ruby Core After early JIT prototypes looked good,

    there was a bake-off. MJIT won. “Newborn Hope for Ruby Speed”
  18. MJIT: Ruby’s JIT “MJIT In a Hurry”

  19. How Does MJIT Work? MJIT uses a C compiler from

    a background job with a Ruby-to-C translator.
  20. MJIT - the Basics (1)

  21. MJIT - the Basics (2)

  22. How MJIT Differs (1) Each language’s JIT picks the CPU

    priority for compiling. MJIT is conservative. “MJIT’s Compiler at App Start”
  23. How MJIT Differs (2) MJIT compiles a bit at a

    time. So it has normal fast Ruby startup but long warmup. “JIT’s Usual Troubles”
  24. How MJIT Differs (3) JVM-style JIT compiles in-process. That runs

    faster, but can’t easily shed excess memory. “Attempting to Free Memory”
  25. How MJIT Differs (4) MJIT’s background job lets it discard

    memory after compile. But it pays a compiler speed penalty. “Invoking the Compiler”
  26. Using JIT in Ruby 2.6 In Ruby 2.6, you can

    turn JIT on with “--jit”. Try “ruby --help” to get args for JIT debugging and tuning.
  27. Ruby 2.6? With rvm, you can install “ruby-2.6.0-preview2”. Ruby-install is

  28. JIT With Rails? Takashi’s working on it. But for now,

    it slows Rails down. “Rails, Broken on JIT”
  29. JIT Problems What trouble can JIT cause? “Debugging JIT Deoptimization”

  30. Want JIT Off? In 2.6, JIT is off by default.

    Later, you can disable with “--disable-jit”. “Ruby JIT, Told to Go Away”
  31. Ahead-of-Time AoT is compiling fully at app start. Can Ruby

    JIT do that? Not really, no. “Minor Problems in Ruby AoT”
  32. Questions? “If You Have a Question, Somebody Else Wants to

    Know Too” These slides: