$30 off During Our Annual Pro Sale. View Details »

Going faster with InvokeDynamic

Going faster with InvokeDynamic

InvokeDynamic is a new instruction for the Java 7 VM which allows language implementers to handle method dispatch for any language quickly. This talk covers the basics of how it works and what it enables in JRuby 1.7


May 18, 2012

More Decks by tarcieri

Other Decks in Programming


  1. Going faster with InvokeDynamic Tony Arcieri JRuby SF Meetup May

    17th, 2012
  2. Why am I qualified?

  3. None
  4. I’m trying! InDy is a technical subject o_O

  5. HotSpot 101

  6. Java was slow Once upon a time...

  7. None
  8. HotSpot Just-In-Time Compiler

  9. Start by interpreting JVM “emulates” bytecodes in software BYTECODE

  10. Locate “hot spots” BYTECODE Using runtime profiling information

  11. 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
  12. Inlining Combine and optimize across calls Method 1 CALL Method

  13. Inlining Combine and optimize across calls Method 1 Method 2

  14. Ruby was slow Once upon a time...

  15. Ruby with JIT •JRuby •Rubinius •MacRuby

  16. JRuby JIT

  17. Architecture Diagram

  18. Start by interpreting Walk the parse tree RUBY

  19. Locate “hot spots” RUBY Over 50 method invocations

  20. 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
  21. 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
  22. JVM Limitations

  23. Made for Java •InvokeVirtual •InvokeInterface •InvokeStatic •InvokeSpecial

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

  25. JVM = old dog Can’t learn new tricks

  26. Y U NO InvokeRuby?

  27. Y U NO InvokeRuby? Y U NO InvokeGroovy? Y U

    NO InvokeJavaScript? Y U NO InvokeClojure?
  28. What if we could teach the JVM the fast path?

  29. What if it worked for any language?

  30. One Invoke to rule them all!

  31. How?

  32. InvokeDynamic

  33. InDy

  34. InDy •Shipped in Java 7 •Supported in JRuby 1.7 •JRuby

    1.7 release preview available next week!
  35. What is InDy?

  36. New JVM instruction

  37. method_missing for Invoke

  38. InDy is a “callback”

  39. Bootstrap method

  40. Bootstrap method Wire up the call site

  41. Bootstrap method Your own code!

  42. Bootstrap Method •Called on all classes that use InDy •Find

    MethodHandle to dispatch •Return CallSite object
  43. 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
  44. None
  45. InDy Review •Bootstrap method •MethodHandles •CallSite

  46. Result?

  47. Java speed! Seriously

  48. Inlining Combine and optimize across calls Method 1 Method 2

  49. None
  50. Will it inline?

  51. Method invocation? foo.bar baz

  52. YES

  53. Super? super(foo, bar, baz)

  54. YES

  55. Splats? foo(bar, *baz)

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

  57. YES

  58. InDy can convert arguments

  59. Argument Conversion = MethodHandle

  60. send

  61. YES

  62. method_missing

  63. YES

  64. Java code?

  65. YES

  66. JRuby runtime?

  67. YES

  68. Core types?

  69. YES

  70. Conclusion

  71. JVM can learn new tricks

  72. Ruby can be fast on the JVM

  73. Ruby can be Java speed on the JVM

  74. Any language can be Java speed on the JVM

  75. As long as you know InDy...

  76. One more thing...

  77. This is the greatest thing that has ever happened to

    the JVM
  78. Your wildest dreams

  79. ...are possible!

  80. Questions? Ask @headius on Twitter

  81. Questions? I can try o_O

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