Your SlideShare is downloading. ×
0
invokedynamic  You Ain’t Seen Nothin’ Yet
Me
@headius
JRuby Guy
JVM Guy
I KNOW π
What isinvokedynamic?
Invoke?
Invoke?That’s one use, but there are many others
Dynamic?
Dynamic?Dynamic typing is a common reason,    but there are many others
User-defined Data    Endpoint
User-defined Data    Endpoint           Hä             ?!
JVM 101
JVM 101200 opcodes
JVM 101       200 opcodesTen (or 16) “data endpoints”
JVM 101                   200 opcodes            Ten (or 16) “data endpoints”   Invocation invokevirtualinvokeinterface in...
JVM 101                   200 opcodes            Ten (or 16) “data endpoints”   Invocation       Field Access invokevirtua...
JVM 101                   200 opcodes            Ten (or 16) “data endpoints”   Invocation       Field Access     Array Ac...
JVM 101                   200 opcodes            Ten (or 16) “data endpoints”   Invocation       Field Access      Array A...
JVMOpcodes
JVM              Opcodes Invocation       Field Access   Array Access invokevirtual      getfield                         ...
JVM              Opcodes Invocation       Field Access   Array Access invokevirtual      getfield                         ...
JVM              Opcodes Invocation       Field Access   Array Access invokevirtual      getfield                         ...
JVM              Opcodes Invocation       Field Access   Array Access invokevirtual      getfield                         ...
Invocation
Invocation
InvocationCall Site   VM Operations
Invocation Call Site      VM OperationsMethod Name  SignatureTarget + Args
Invocation Call Site      VM OperationsMethod Name      Method Lookup  Signature      Type CheckingTarget + Args       Bra...
Invocation                          Target                          Object                                   instanceofobj...
InvocationVM Operations                              Target                              Object                           ...
InvocationVM Operations                              Target                              Object                           ...
InvocationVM Operations  Method Lookup                                  Target                                  Object    ...
InvocationVM Operations  Method Lookup  Type Checking                   Target                                  Object    ...
InvocationVM Operations  Method Lookup  Type Checking                   Target     Branch                       Object    ...
InvocationVM Operations  Method Lookup  Type Checking                    Target     Branch                        Object  ...
Invocation
Invocation  Virtual        InterfaceMethod Lookup   Method LookupType Checking   Type Checking   Branch          BranchMet...
Invocation   Virtual       InterfaceMethod Lookup   Method LookupType Checking   Type Checking   Branch          BranchMet...
Invocation   Virtual        InterfaceMethod Lookup   Method LookupType Checking   Type Checking   Branch          BranchMe...
Field Access
Field Access  Call Site  Field Name   SignatureTarget [+ Value]
Field Access  Call Site        VM Operations  Field Name        Get Field Offset   Signature         Type CheckTarget [+ V...
Array Access
Array Access      Call SiteTarget + Offset [+ Value]
Array Access      Call Site             VM OperationsTarget + Offset [+ Value]     Type Check                             ...
Deja Vu?• Similar call site• Similar VM operations• Why can’t we compose our own?
invokedynamic
Call Siteinvokedynamic
Call Site   VM Operationsinvokedynamic
bytecode + Site      Call bootstrap   Method Handles                       VM Operationsinvokedynamic
bytecode
bytecode• invokedynamic • like invokevirtual, invokestatic, etc• Name and signature, like the others• Additional bootstrap...
Method Handles• Function/field/array pointers• Argument manipulation• Flow control• Optimizable by the JVM • This is very i...
invokedynamic bytecode
invokedynamic bytecodebo  ot     stra         p             m              et                ho                  d
invokedynamic bytecodebo  ot     stra         p             m              et                ho                  d        ...
invokedynamic bytecode                                  target methodbo  ot     stra         p             m              ...
invokedynamic bytecode                                  target methodbo  ot     stra         p             m              ...
invokedynamic bytecode                                  target methodbo  ot     stra         p             m              ...
You DecideHow to reach some data endpoint
You DecideWhat transformations to perform on arguments
You DecideWhen and why the endpoint should change
You DecideHow to make the JVM work for you!
Use Cases
Dynamic Invocation• The obvious one• Method lookup based on runtime types • Potentially mutable types• Type check specific ...
Dynamic Invocation                  Target                  Object                         associated withobj.foo()   JVM ...
Dynamic InvocationVM Operations                      Target                      Object                             associ...
Dynamic InvocationVM Operations                      Target                      Object                             associ...
Dynamic InvocationVM Operations  Method Lookup                 Target                                Object               ...
Dynamic InvocationVM Operations  Method Lookup                 Target     Branch                                Object    ...
Dynamic InvocationVM Operations  Method Lookup              Target     Branch  Method Cache               Object          ...
Lazy Constants• Call site just produces a value• Value calculated once• Subsequent access is direct, optimizable
Lazy Constant                  JVM       LazyLAZY_CONST                         Computation  Call Site
Lazy ConstantVM Operations                   JVM       LazyLAZY_CONST                          Computation   Call Site
Lazy ConstantVM Operations                   JVM       LazyLAZY_CONST                          Computation   Call Site
Lazy ConstantVM Operations  Calculate Value                    JVM         LazyLAZY_CONST                             Comp...
Lazy ConstantVM Operations  Calculate Value Bind Permanently                     JVM      LazyLAZY_CONST             value...
Delegates• Arbitrary function pointers • No anon inner classes • No bridge methods• Java 8 “Lambda” will do this
Lambda Expression                    SurroundingString s ->   JVM      ClasstoString()             lambda                 ...
Lambda ExpressionVM Operations                      Surrounding String s ->    JVM      Class toString()              lamb...
Lambda ExpressionVM Operations                      Surrounding String s ->    JVM      Class toString()              lamb...
Lambda ExpressionVM OperationsRetrieve Lambda Obj                            Surrounding String s ->     lambda           ...
Lambda ExpressionVM OperationsRetrieve Lambda Obj  Bind and Invoke                            Surrounding String s ->     ...
Multi-dispatch• Dispatch based on runtime types • Java is single-dispatch• Call site looks like normal invoke• VM side exa...
How Do You Benefit?
Future• New language impls • JavaScript: Dyn.js and Nashorn • Redline Smalltalk• Improved language performance • JRuby, Gr...
JRuby/Java 6   JRuby/Java 7
JRuby/Java 6                      JRuby/Java 7                          Times Faster than Ruby 1.9.3  53.75 2.51.25  0    ...
JRuby/Java 6                        JRuby/Java 7                             Times Faster than Ruby 1.9.3  53.75 2.5      ...
JRuby/Java 6                        JRuby/Java 7                                 Times Faster than Ruby 1.9.3  5          ...
invokedynamicis the future of Java!
Thank You!• @headius• blog.headius.com • invokedynamic post coming soon• headius@headius.com• Come to my deep dive at 16:0...
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
Upcoming SlideShare
Loading in...5
×

InvokeDynamic - You Ain't Seen Nothin Yet

4,541

Published on

Keynote from JAX 2012

Published in: Technology
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,541
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
107
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Perhaps even more transcendental...\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • Also loading constants, which are read-only; not as interesting\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Type check for instances; statics are known\nField offset remains static\n
  • Type check for instances; statics are known\nField offset remains static\n
  • Type check for instances; statics are known\nField offset remains static\n
  • Type check for instances; statics are known\nField offset remains static\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "InvokeDynamic - You Ain't Seen Nothin Yet"

    1. 1. invokedynamic You Ain’t Seen Nothin’ Yet
    2. 2. Me
    3. 3. @headius
    4. 4. JRuby Guy
    5. 5. JVM Guy
    6. 6. I KNOW π
    7. 7. What isinvokedynamic?
    8. 8. Invoke?
    9. 9. Invoke?That’s one use, but there are many others
    10. 10. Dynamic?
    11. 11. Dynamic?Dynamic typing is a common reason, but there are many others
    12. 12. User-defined Data Endpoint
    13. 13. User-defined Data Endpoint Hä ?!
    14. 14. JVM 101
    15. 15. JVM 101200 opcodes
    16. 16. JVM 101 200 opcodesTen (or 16) “data endpoints”
    17. 17. JVM 101 200 opcodes Ten (or 16) “data endpoints” Invocation invokevirtualinvokeinterface invokestatic invokespecial
    18. 18. JVM 101 200 opcodes Ten (or 16) “data endpoints” Invocation Field Access invokevirtual getfieldinvokeinterface setfield invokestatic getstatic invokespecial setstatic
    19. 19. JVM 101 200 opcodes Ten (or 16) “data endpoints” Invocation Field Access Array Access invokevirtual getfield *aloadinvokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic
    20. 20. JVM 101 200 opcodes Ten (or 16) “data endpoints” Invocation Field Access Array Access invokevirtual getfield *aloadinvokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic All Java code revolves around these endpoints Remaining ops are stack, local vars, flow control allocation, and math/boolean/bit operations
    21. 21. JVMOpcodes
    22. 22. JVM Opcodes Invocation Field Access Array Access invokevirtual getfield *aloadinvokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic
    23. 23. JVM Opcodes Invocation Field Access Array Access invokevirtual getfield *aloadinvokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic Stack Local Vars Flow Control Allocation Boolean and Numeric
    24. 24. JVM Opcodes Invocation Field Access Array Access invokevirtual getfield *aloadinvokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic Stack Local Vars Flow Control Allocation Boolean and Numeric
    25. 25. JVM Opcodes Invocation Field Access Array Access invokevirtual getfield *aloadinvokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic Stack Local Vars Flow Control Allocation Boolean and Numeric
    26. 26. Invocation
    27. 27. Invocation
    28. 28. InvocationCall Site VM Operations
    29. 29. Invocation Call Site VM OperationsMethod Name SignatureTarget + Args
    30. 30. Invocation Call Site VM OperationsMethod Name Method Lookup Signature Type CheckingTarget + Args Branch Method Cache
    31. 31. Invocation Target Object instanceofobj.foo() JVM Object’s Class void foo() static void bar()
    32. 32. InvocationVM Operations Target Object instanceof obj.foo() JVM Object’s Class Call Site void foo() static void bar()
    33. 33. InvocationVM Operations Target Object instanceof obj.foo() JVM Object’s Class Call Site void foo() static void bar()
    34. 34. InvocationVM Operations Method Lookup Target Object instanceof obj.foo() JVM Object’s Class Call Site void foo() void foo() static void bar()
    35. 35. InvocationVM Operations Method Lookup Type Checking Target Object instanceof obj.foo() JVM Object’s Class Call Site void foo() void foo() static void bar()
    36. 36. InvocationVM Operations Method Lookup Type Checking Target Branch Object instanceof obj.foo() JVM Object’s Class Call Site void foo() void foo() static void bar()
    37. 37. InvocationVM Operations Method Lookup Type Checking Target Branch Object Method Cache instanceof obj.foo() JVM Object’s Class void foo() Call Site void foo() static void bar()
    38. 38. Invocation
    39. 39. Invocation Virtual InterfaceMethod Lookup Method LookupType Checking Type Checking Branch BranchMethod Cache Method Cache
    40. 40. Invocation Virtual InterfaceMethod Lookup Method LookupType Checking Type Checking Branch BranchMethod Cache Method Cache StaticMethod Lookup BranchMethod Cache
    41. 41. Invocation Virtual InterfaceMethod Lookup Method LookupType Checking Type Checking Branch BranchMethod Cache Method Cache Static Special Method LookupMethod Lookup Access Checking Branch BranchMethod Cache Method Cache
    42. 42. Field Access
    43. 43. Field Access Call Site Field Name SignatureTarget [+ Value]
    44. 44. Field Access Call Site VM Operations Field Name Get Field Offset Signature Type CheckTarget [+ Value] Access Field
    45. 45. Array Access
    46. 46. Array Access Call SiteTarget + Offset [+ Value]
    47. 47. Array Access Call Site VM OperationsTarget + Offset [+ Value] Type Check Access Element
    48. 48. Deja Vu?• Similar call site• Similar VM operations• Why can’t we compose our own?
    49. 49. invokedynamic
    50. 50. Call Siteinvokedynamic
    51. 51. Call Site VM Operationsinvokedynamic
    52. 52. bytecode + Site Call bootstrap Method Handles VM Operationsinvokedynamic
    53. 53. bytecode
    54. 54. bytecode• invokedynamic • like invokevirtual, invokestatic, etc• Name and signature, like the others• Additional bootstrap method • JVM defers to you
    55. 55. Method Handles• Function/field/array pointers• Argument manipulation• Flow control• Optimizable by the JVM • This is very important
    56. 56. invokedynamic bytecode
    57. 57. invokedynamic bytecodebo ot stra p m et ho d
    58. 58. invokedynamic bytecodebo ot stra p m et ho d method handles
    59. 59. invokedynamic bytecode target methodbo ot stra p m et ho d method handles
    60. 60. invokedynamic bytecode target methodbo ot stra p m et ho d method handles
    61. 61. invokedynamic bytecode target methodbo ot stra p m et ho d method handles
    62. 62. You DecideHow to reach some data endpoint
    63. 63. You DecideWhat transformations to perform on arguments
    64. 64. You DecideWhen and why the endpoint should change
    65. 65. You DecideHow to make the JVM work for you!
    66. 66. Use Cases
    67. 67. Dynamic Invocation• The obvious one• Method lookup based on runtime types • Potentially mutable types• Type check specific to language
    68. 68. Dynamic Invocation Target Object associated withobj.foo() JVM Method Table def foo ... def bar ...
    69. 69. Dynamic InvocationVM Operations Target Object associated with obj.foo() JVM Method Table Call Site def foo ... def bar ...
    70. 70. Dynamic InvocationVM Operations Target Object associated with obj.foo() JVM Method Table Call Site def foo ... def bar ...
    71. 71. Dynamic InvocationVM Operations Method Lookup Target Object associated with obj.foo() JVM Method Table Call Site def foo ... def foo ... def bar ...
    72. 72. Dynamic InvocationVM Operations Method Lookup Target Branch Object associated with obj.foo() JVM Method Table Call Site def foo ... def foo ... def bar ...
    73. 73. Dynamic InvocationVM Operations Method Lookup Target Branch Method Cache Object associated with obj.foo() JVM def foo ... Method Table Call Site def foo ... def bar ...
    74. 74. Lazy Constants• Call site just produces a value• Value calculated once• Subsequent access is direct, optimizable
    75. 75. Lazy Constant JVM LazyLAZY_CONST Computation Call Site
    76. 76. Lazy ConstantVM Operations JVM LazyLAZY_CONST Computation Call Site
    77. 77. Lazy ConstantVM Operations JVM LazyLAZY_CONST Computation Call Site
    78. 78. Lazy ConstantVM Operations Calculate Value JVM LazyLAZY_CONST Computation value Call Site
    79. 79. Lazy ConstantVM Operations Calculate Value Bind Permanently JVM LazyLAZY_CONST value Computation Call Site
    80. 80. Delegates• Arbitrary function pointers • No anon inner classes • No bridge methods• Java 8 “Lambda” will do this
    81. 81. Lambda Expression SurroundingString s -> JVM ClasstoString() lambda body Call Site
    82. 82. Lambda ExpressionVM Operations Surrounding String s -> JVM Class toString() lambda body Call Site
    83. 83. Lambda ExpressionVM Operations Surrounding String s -> JVM Class toString() lambda body Call Site
    84. 84. Lambda ExpressionVM OperationsRetrieve Lambda Obj Surrounding String s -> lambda JVM Class body toString() Call Site
    85. 85. Lambda ExpressionVM OperationsRetrieve Lambda Obj Bind and Invoke Surrounding String s -> lambda JVM Class body toString() Call Site
    86. 86. Multi-dispatch• Dispatch based on runtime types • Java is single-dispatch• Call site looks like normal invoke• VM side examines target, args• Multiple targets cached with branches
    87. 87. How Do You Benefit?
    88. 88. Future• New language impls • JavaScript: Dyn.js and Nashorn • Redline Smalltalk• Improved language performance • JRuby, Groovy, Jython, Clojure, Scala • Java features too!
    89. 89. JRuby/Java 6 JRuby/Java 7
    90. 90. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 53.75 2.51.25 0 base64 richards neural redblack
    91. 91. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 53.75 2.5 1.914 1.538 1.5651.25 1.346 0 base64 richards neural redblack
    92. 92. JRuby/Java 6 JRuby/Java 7 Times Faster than Ruby 1.9.3 5 4.323.75 3.66 3.44 2.5 2.658 1.914 1.538 1.5651.25 1.346 0 base64 richards neural redblack
    93. 93. invokedynamicis the future of Java!
    94. 94. Thank You!• @headius• blog.headius.com • invokedynamic post coming soon• headius@headius.com• Come to my deep dive at 16:00• Have fun with invokedynamic!
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×