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?
Make Ruby2.6 Fasterwith JITNoah Gibbs, AppFolio@codefolio / engineering.appfolio.comPictures are by Arthur Rackham, and are in the Public Domain.
View Slide
Thank You, AppFolio!AppFolio pays me to write andspeak. Thank you, AppFolio! Ifyou’re looking for a Ruby job inSouthern California, talk to us!
I Often Talk Too Fast.If you call out “hey, slowdown!”, I’ll try to go slower.And it will help your fellow audience members!
JIT:What Is It?“Effortlessly Optimizing Ruby”
CompilingRuby interprets yourcode as it runs. C++(and others) compile tomachine code before.“Compiling a C++ Program”
JIT“Just-In-Time” compilingtranslates part of yourapp to fast machinecode.“Running Faster Without Effort”
Does JIT Work?TruffleRuby JIT gives 10x+speedup over interpreted.
Does JIT Work? (2)Ruby 2.6 experimentalJIT is still prerelease.But it gets 2.5x+ forthe same benchmark.“Ruby JIT Shows Early Promise”
Tradeoffs of JITNothing is perfect. JIThas two maindisadvantages: memoryusage and warmup.“Considering JIT’s Tradeoffs”
Memory Usage (1)JIT tracks method calls:● How many calls?● How much runtime?● With what argument types?
Memory Usage (2)JIT keeps 2+ copies ofa method and switchesback to un-optimized ifrequired.“De-optimizing a Method”
WarmupJITted programs speed up asthey compile methods. Thereis “warmup time” as newprograms start out slower.
Warmup vs StartupIf compiling is highpriority, early programspeed is very slow. JVMis infamous for this.“Ruby Dancing with the JVM”
Long ProgramsServers and batch jobsbenefit from JIT. Smallquick programs getlittle or nothing.“Rake task dodging a JIT penalty”
Why Didn’tRuby GetJIT Sooner?“Ruby 2.5 Mocked for No JIT”
JIT and Ruby CoreJIT’s disadvantages areRuby advantages -memory, startup. JITwas a tough sell.“Core Dev Inspects JIT Attempt”
JIT and Ruby CoreAfter early JITprototypes lookedgood, there was abake-off. MJIT won.“Newborn Hope for Ruby Speed”
MJIT:Ruby’s JIT“MJIT In a Hurry”
How Does MJIT Work?MJIT uses a C compiler from abackground job with aRuby-to-C translator.
MJIT - the Basics (1)
MJIT - the Basics (2)
How MJIT Differs (1)Each language’s JITpicks the CPU priorityfor compiling. MJIT isconservative.“MJIT’s Compiler at App Start”
How MJIT Differs (2)MJIT compiles a bit at atime. So it has normalfast Ruby startup butlong warmup.“JIT’s Usual Troubles”
How MJIT Differs (3)JVM-style JIT compilesin-process. That runsfaster, but can’t easilyshed excess memory.“Attempting to Free Memory”
How MJIT Differs (4)MJIT’s background joblets it discard memoryafter compile. But it paysa compiler speed penalty.“Invoking the Compiler”
Using JIT in Ruby 2.6In Ruby 2.6, you can turn JITon with “--jit”. Try “ruby --help”to get args for JIT debuggingand tuning.
Ruby 2.6?With rvm, you can install“ruby-2.6.0-preview2”.Ruby-install is similar.
JIT With Rails?Takashi’s working onit. But for now, itslows Rails down.“Rails, Broken on JIT”
JIT ProblemsWhat trouble canJIT cause?“Debugging JIT Deoptimization”
Want JIT Off?In 2.6, JIT is off bydefault. Later, you candisable with“--disable-jit”.“Ruby JIT, Told to Go Away”
Ahead-of-TimeAoT is compiling fully atapp start. Can Ruby JITdo that?Not really, no.“Minor Problems in Ruby AoT”
Questions?“If You Have a Question, Somebody Else Wants to Know Too”These slides: https://bit.ly/southeast2018-gibbs