Slide 1

Slide 1 text

Make Ruby 2.6 Faster with JIT Noah Gibbs, AppFolio @codefolio / engineering.appfolio.com Pictures are by Arthur Rackham, and are in the Public Domain.

Slide 2

Slide 2 text

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!

Slide 3

Slide 3 text

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!

Slide 4

Slide 4 text

JIT: What Is It? “Effortlessly Optimizing Ruby”

Slide 5

Slide 5 text

Compiling Ruby interprets your code as it runs. C++ (and others) compile to machine code before. “Compiling a C++ Program”

Slide 6

Slide 6 text

JIT “Just-In-Time” compiling translates part of your app to fast machine code. “Running Faster Without Effort”

Slide 7

Slide 7 text

Does JIT Work? TruffleRuby JIT gives 10x+ speedup over interpreted.

Slide 8

Slide 8 text

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”

Slide 9

Slide 9 text

Tradeoffs of JIT Nothing is perfect. JIT has two main disadvantages: memory usage and warmup. “Considering JIT’s Tradeoffs”

Slide 10

Slide 10 text

Memory Usage (1) JIT tracks method calls: ● How many calls? ● How much runtime? ● With what argument types?

Slide 11

Slide 11 text

Memory Usage (2) JIT keeps 2+ copies of a method and switches back to un-optimized if required. “De-optimizing a Method”

Slide 12

Slide 12 text

Warmup JITted programs speed up as they compile methods. There is “warmup time” as new programs start out slower.

Slide 13

Slide 13 text

Warmup vs Startup If compiling is high priority, early program speed is very slow. JVM is infamous for this. “Ruby Dancing with the JVM”

Slide 14

Slide 14 text

Long Programs Servers and batch jobs benefit from JIT. Small quick programs get little or nothing. “Rake task dodging a JIT penalty”

Slide 15

Slide 15 text

Why Didn’t Ruby Get JIT Sooner? “Ruby 2.5 Mocked for No JIT”

Slide 16

Slide 16 text

JIT and Ruby Core JIT’s disadvantages are Ruby advantages - memory, startup. JIT was a tough sell. “Core Dev Inspects JIT Attempt”

Slide 17

Slide 17 text

JIT and Ruby Core After early JIT prototypes looked good, there was a bake-off. MJIT won. “Newborn Hope for Ruby Speed”

Slide 18

Slide 18 text

MJIT: Ruby’s JIT “MJIT In a Hurry”

Slide 19

Slide 19 text

How Does MJIT Work? MJIT uses a C compiler from a background job with a Ruby-to-C translator.

Slide 20

Slide 20 text

MJIT - the Basics (1)

Slide 21

Slide 21 text

MJIT - the Basics (2)

Slide 22

Slide 22 text

How MJIT Differs (1) Each language’s JIT picks the CPU priority for compiling. MJIT is conservative. “MJIT’s Compiler at App Start”

Slide 23

Slide 23 text

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”

Slide 24

Slide 24 text

How MJIT Differs (3) JVM-style JIT compiles in-process. That runs faster, but can’t easily shed excess memory. “Attempting to Free Memory”

Slide 25

Slide 25 text

How MJIT Differs (4) MJIT’s background job lets it discard memory after compile. But it pays a compiler speed penalty. “Invoking the Compiler”

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

Ruby 2.6? With rvm, you can install “ruby-2.6.0-preview2”. Ruby-install is similar.

Slide 28

Slide 28 text

JIT With Rails? Takashi’s working on it. But for now, it slows Rails down. “Rails, Broken on JIT”

Slide 29

Slide 29 text

JIT Problems What trouble can JIT cause? “Debugging JIT Deoptimization”

Slide 30

Slide 30 text

Want JIT Off? In 2.6, JIT is off by default. Later, you can disable with “--disable-jit”. “Ruby JIT, Told to Go Away”

Slide 31

Slide 31 text

Ahead-of-Time AoT is compiling fully at app start. Can Ruby JIT do that? Not really, no. “Minor Problems in Ruby AoT”

Slide 32

Slide 32 text

Questions? “If You Have a Question, Somebody Else Wants to Know Too” These slides: https://bit.ly/southeast2018-gibbs