Slide 1

Slide 1 text

Going faster with InvokeDynamic Tony Arcieri JRuby SF Meetup May 17th, 2012

Slide 2

Slide 2 text

Why am I qualified?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

I’m trying! InDy is a technical subject o_O

Slide 5

Slide 5 text

HotSpot 101

Slide 6

Slide 6 text

Java was slow Once upon a time...

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

HotSpot Just-In-Time Compiler

Slide 9

Slide 9 text

Start by interpreting JVM “emulates” bytecodes in software BYTECODE

Slide 10

Slide 10 text

Locate “hot spots” BYTECODE Using runtime profiling information

Slide 11

Slide 11 text

Generate machine code Compile JVM bytecode to native ISA BYTECODE 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

Slide 12

Slide 12 text

Inlining Combine and optimize across calls Method 1 CALL Method 2

Slide 13

Slide 13 text

Inlining Combine and optimize across calls Method 1 Method 2

Slide 14

Slide 14 text

Ruby was slow Once upon a time...

Slide 15

Slide 15 text

Ruby with JIT •JRuby •Rubinius •MacRuby

Slide 16

Slide 16 text

JRuby JIT

Slide 17

Slide 17 text

Architecture Diagram

Slide 18

Slide 18 text

Start by interpreting Walk the parse tree RUBY

Slide 19

Slide 19 text

Locate “hot spots” RUBY Over 50 method invocations

Slide 20

Slide 20 text

Compile to bytecode RUBY Ruby code becomes JVM instructions B Y T E C O D E B Y T E C O D E B Y T E C O D E B Y T E C O D E B Y T E C O D E

Slide 21

Slide 21 text

HotSpot JITs too RUBY Ruby code becomes machine code B Y T E C O D E B Y T E C O D E 1 0 1 0 1 0 1 0 1 0 1 0

Slide 22

Slide 22 text

JVM Limitations

Slide 23

Slide 23 text

Made for Java •InvokeVirtual •InvokeInterface •InvokeStatic •InvokeSpecial

Slide 24

Slide 24 text

JVM is smart! •C++ virtual methods: SLOW! •InvokeVirtual: FAST!

Slide 25

Slide 25 text

JVM = old dog Can’t learn new tricks

Slide 26

Slide 26 text

Y U NO InvokeRuby?

Slide 27

Slide 27 text

Y U NO InvokeRuby? Y U NO InvokeGroovy? Y U NO InvokeJavaScript? Y U NO InvokeClojure?

Slide 28

Slide 28 text

What if we could teach the JVM the fast path?

Slide 29

Slide 29 text

What if it worked for any language?

Slide 30

Slide 30 text

One Invoke to rule them all!

Slide 31

Slide 31 text

How?

Slide 32

Slide 32 text

InvokeDynamic

Slide 33

Slide 33 text

InDy

Slide 34

Slide 34 text

InDy •Shipped in Java 7 •Supported in JRuby 1.7 •JRuby 1.7 release preview available next week!

Slide 35

Slide 35 text

What is InDy?

Slide 36

Slide 36 text

New JVM instruction

Slide 37

Slide 37 text

method_missing for Invoke

Slide 38

Slide 38 text

InDy is a “callback”

Slide 39

Slide 39 text

Bootstrap method

Slide 40

Slide 40 text

Bootstrap method Wire up the call site

Slide 41

Slide 41 text

Bootstrap method Your own code!

Slide 42

Slide 42 text

Bootstrap Method •Called on all classes that use InDy •Find MethodHandle to dispatch •Return CallSite object

Slide 43

Slide 43 text

Bootstrap Method •Invoked the first time a call site is used •Resulting call site object is “bound” to where InDy was invoked and is used for subsequent calls •CallSites can designate their own caching behavior

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

InDy Review •Bootstrap method •MethodHandles •CallSite

Slide 46

Slide 46 text

Result?

Slide 47

Slide 47 text

Java speed! Seriously

Slide 48

Slide 48 text

Inlining Combine and optimize across calls Method 1 Method 2

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Will it inline?

Slide 51

Slide 51 text

Method invocation? foo.bar baz

Slide 52

Slide 52 text

YES

Slide 53

Slide 53 text

Super? super(foo, bar, baz)

Slide 54

Slide 54 text

YES

Slide 55

Slide 55 text

Splats? foo(bar, *baz)

Slide 56

Slide 56 text

Default arguments? def foo(bar, baz = Qux.new) ... end

Slide 57

Slide 57 text

YES

Slide 58

Slide 58 text

InDy can convert arguments

Slide 59

Slide 59 text

Argument Conversion = MethodHandle

Slide 60

Slide 60 text

send

Slide 61

Slide 61 text

YES

Slide 62

Slide 62 text

method_missing

Slide 63

Slide 63 text

YES

Slide 64

Slide 64 text

Java code?

Slide 65

Slide 65 text

YES

Slide 66

Slide 66 text

JRuby runtime?

Slide 67

Slide 67 text

YES

Slide 68

Slide 68 text

Core types?

Slide 69

Slide 69 text

YES

Slide 70

Slide 70 text

Conclusion

Slide 71

Slide 71 text

JVM can learn new tricks

Slide 72

Slide 72 text

Ruby can be fast on the JVM

Slide 73

Slide 73 text

Ruby can be Java speed on the JVM

Slide 74

Slide 74 text

Any language can be Java speed on the JVM

Slide 75

Slide 75 text

As long as you know InDy...

Slide 76

Slide 76 text

One more thing...

Slide 77

Slide 77 text

This is the greatest thing that has ever happened to the JVM

Slide 78

Slide 78 text

Your wildest dreams

Slide 79

Slide 79 text

...are possible!

Slide 80

Slide 80 text

Questions? Ask @headius on Twitter

Slide 81

Slide 81 text

Questions? I can try o_O

Slide 82

Slide 82 text

Twitter: @bascule Celluloid: celluloid.io Blog: unlimitednovelty.com