Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

tarcieri

May 18, 2012
Tweet

More Decks by tarcieri

Other Decks in Programming

Transcript

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

    View full-size slide

  2. Why am I qualified?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. HotSpot
    Just-In-Time Compiler

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. 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

    View full-size slide

  9. Inlining
    Combine and optimize across calls
    Method 1
    CALL Method 2

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. Ruby with JIT
    •JRuby
    •Rubinius
    •MacRuby

    View full-size slide

  13. Architecture Diagram

    View full-size slide

  14. Start by interpreting
    Walk the parse tree
    RUBY

    View full-size slide

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

    View full-size slide

  16. 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

    View full-size slide

  17. 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

    View full-size slide

  18. JVM Limitations

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. Y U NO InvokeRuby?

    View full-size slide

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

    View full-size slide

  24. What if we could
    teach the JVM the
    fast path?

    View full-size slide

  25. What if it worked for
    any language?

    View full-size slide

  26. One Invoke to rule
    them all!

    View full-size slide

  27. InvokeDynamic

    View full-size slide

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

    View full-size slide

  29. What is InDy?

    View full-size slide

  30. New JVM
    instruction

    View full-size slide

  31. method_missing
    for Invoke

    View full-size slide

  32. InDy is a
    “callback”

    View full-size slide

  33. Bootstrap method

    View full-size slide

  34. Bootstrap method
    Wire up the call site

    View full-size slide

  35. Bootstrap method
    Your own code!

    View full-size slide

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

    View full-size slide

  37. 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

    View full-size slide

  38. InDy Review
    •Bootstrap method
    •MethodHandles
    •CallSite

    View full-size slide

  39. Java speed!
    Seriously

    View full-size slide

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

    View full-size slide

  41. Will it inline?

    View full-size slide

  42. Method invocation?
    foo.bar baz

    View full-size slide

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

    View full-size slide

  44. Splats?
    foo(bar, *baz)

    View full-size slide

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

    View full-size slide

  46. InDy can convert
    arguments

    View full-size slide

  47. Argument Conversion
    =
    MethodHandle

    View full-size slide

  48. method_missing

    View full-size slide

  49. JRuby runtime?

    View full-size slide

  50. JVM can learn new tricks

    View full-size slide

  51. Ruby can be fast
    on the JVM

    View full-size slide

  52. Ruby can be Java speed
    on the JVM

    View full-size slide

  53. Any language can be
    Java speed
    on the JVM

    View full-size slide

  54. As long as you know
    InDy...

    View full-size slide

  55. One more thing...

    View full-size slide

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

    View full-size slide

  57. Your wildest dreams

    View full-size slide

  58. ...are possible!

    View full-size slide

  59. Questions?
    Ask @headius on Twitter

    View full-size slide

  60. Questions?
    I can try o_O

    View full-size slide

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

    View full-size slide