O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

InvokeDynamic - You Ain't Seen Nothin Yet

5.807 visualizações

Publicada em

Keynote from JAX 2012

Publicada em: Tecnologia
  • Seja o primeiro a comentar

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!

×