SlideShare a Scribd company logo
invokedynamic
  You Ain’t Seen Nothin’ Yet
Me
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
@headius
JRuby Guy
JVM Guy
I KNOW
 π
InvokeDynamic - You Ain't Seen Nothin Yet
What is
invokedynamic?
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 101
200 opcodes
JVM 101
       200 opcodes
Ten (or 16) “data endpoints”
JVM 101
                   200 opcodes
            Ten (or 16) “data endpoints”
   Invocation
 invokevirtual
invokeinterface
 invokestatic
 invokespecial
JVM 101
                   200 opcodes
            Ten (or 16) “data endpoints”
   Invocation       Field Access
 invokevirtual       getfield
invokeinterface      setfield
 invokestatic        getstatic
 invokespecial       setstatic
JVM 101
                   200 opcodes
            Ten (or 16) “data endpoints”
   Invocation       Field Access     Array Access
 invokevirtual       getfield          *aload
invokeinterface      setfield          *astore
 invokestatic        getstatic     b,s,c,i,l,d,f,a
 invokespecial       setstatic
JVM 101
                   200 opcodes
            Ten (or 16) “data endpoints”
   Invocation       Field Access      Array Access
 invokevirtual       getfield          *aload
invokeinterface      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
JVM
Opcodes
JVM
              Opcodes
 Invocation       Field Access   Array Access
 invokevirtual      getfield
                                     *aload
invokeinterface     setfield
                                     *astore
 invokestatic       getstatic
                                 b,s,c,i,l,d,f,a
 invokespecial      setstatic
JVM
              Opcodes
 Invocation       Field Access   Array Access
 invokevirtual      getfield
                                     *aload
invokeinterface     setfield
                                     *astore
 invokestatic       getstatic
                                 b,s,c,i,l,d,f,a
 invokespecial      setstatic




    Stack                            Local Vars
                  Flow Control


                                  Allocation
 Boolean and Numeric
JVM
              Opcodes
 Invocation       Field Access   Array Access
 invokevirtual      getfield
                                     *aload
invokeinterface     setfield
                                     *astore
 invokestatic       getstatic
                                 b,s,c,i,l,d,f,a
 invokespecial      setstatic




    Stack                            Local Vars
                  Flow Control


                                  Allocation
 Boolean and Numeric
JVM
              Opcodes
 Invocation       Field Access   Array Access
 invokevirtual      getfield
                                     *aload
invokeinterface     setfield
                                     *astore
 invokestatic       getstatic
                                 b,s,c,i,l,d,f,a
 invokespecial      setstatic




    Stack                            Local Vars
                  Flow Control


                                  Allocation
 Boolean and Numeric
InvokeDynamic - You Ain't Seen Nothin Yet
InvokeDynamic - You Ain't Seen Nothin Yet
Invocation
Invocation
Invocation
Call Site   VM Operations
Invocation
 Call Site      VM Operations
Method Name
  Signature
Target + Args
Invocation
 Call Site      VM Operations
Method Name      Method Lookup
  Signature      Type Checking
Target + Args       Branch
                 Method Cache
Invocation
                          Target
                          Object

                                   instanceof
obj.foo()     JVM
                         Object’s
                          Class
                            void foo()

                         static void bar()
Invocation
VM Operations
                              Target
                              Object

                                       instanceof
   obj.foo()      JVM
                             Object’s
                              Class
   Call Site                    void foo()

                             static void bar()
Invocation
VM Operations
                              Target
                              Object

                                       instanceof
   obj.foo()      JVM
                             Object’s
                              Class
   Call Site                    void foo()

                             static void bar()
Invocation
VM Operations
  Method Lookup
                                  Target
                                  Object

                                           instanceof
   obj.foo()         JVM
                                 Object’s
                                  Class
   Call Site                        void foo()
                    void foo()
                                 static void bar()
Invocation
VM Operations
  Method Lookup
  Type Checking                   Target
                                  Object

                                           instanceof
   obj.foo()         JVM
                                 Object’s
                                  Class
   Call Site                        void foo()
                    void foo()
                                 static void bar()
Invocation
VM Operations
  Method Lookup
  Type Checking                   Target
     Branch                       Object

                                           instanceof
   obj.foo()         JVM
                                 Object’s
                                  Class
   Call Site                        void foo()
                    void foo()
                                 static void bar()
Invocation
VM 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()
Invocation
Invocation
  Virtual        Interface
Method Lookup   Method Lookup
Type Checking   Type Checking
   Branch          Branch
Method Cache    Method Cache
Invocation
   Virtual       Interface
Method Lookup   Method Lookup
Type Checking   Type Checking
   Branch          Branch
Method Cache    Method Cache

   Static
Method Lookup
   Branch
Method Cache
Invocation
   Virtual        Interface
Method Lookup   Method Lookup
Type Checking   Type Checking
   Branch          Branch
Method Cache    Method Cache

   Static          Special
                Method Lookup
Method Lookup
                Access Checking
   Branch
                    Branch
Method Cache
                 Method Cache
Field Access
Field Access

  Call Site
  Field Name
   Signature
Target [+ Value]
Field Access

  Call Site        VM Operations
  Field Name        Get Field Offset
   Signature         Type Check
Target [+ Value]     Access Field
Array Access
Array Access

      Call Site
Target + Offset [+ Value]
Array Access

      Call Site             VM Operations
Target + Offset [+ Value]     Type Check
                             Access Element
Deja Vu?

• Similar call site
• Similar VM operations
• Why can’t we compose our own?
InvokeDynamic - You Ain't Seen Nothin Yet
invokedynamic
Call Site
invokedynamic
Call Site   VM Operations
invokedynamic
bytecode + Site
      Call bootstrap   Method Handles
                       VM Operations
invokedynamic
bytecode
bytecode

• invokedynamic
 • like invokevirtual, invokestatic, etc
• Name and signature, like the others
• Additional bootstrap method
 • JVM defers to you
Method Handles

• Function/field/array pointers
• Argument manipulation
• Flow control
• Optimizable by the JVM
 • This is very important
InvokeDynamic - You Ain't Seen Nothin Yet
invokedynamic bytecode
invokedynamic bytecode




bo
  ot
     stra
         p
             m
              et
                ho
                  d
invokedynamic bytecode




bo
  ot
     stra
         p
             m
              et
                ho
                  d             method handles
invokedynamic bytecode

                                  target method



bo
  ot
     stra
         p
             m
              et
                ho
                  d             method handles
invokedynamic bytecode

                                  target method



bo
  ot
     stra
         p
             m
              et
                ho
                  d             method handles
invokedynamic bytecode

                                  target method



bo
  ot
     stra
         p
             m
              et
                ho
                  d             method handles
You Decide
How to reach some data endpoint
You Decide
What transformations to perform on arguments
You Decide
When and why the endpoint should change
You Decide
How 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 to language
Dynamic Invocation
                  Target
                  Object

                         associated with
obj.foo()   JVM
                  Method
                   Table
                  def foo ...

                  def bar ...
Dynamic Invocation
VM Operations
                      Target
                      Object

                             associated with
   obj.foo()    JVM
                      Method
                       Table
   Call Site          def foo ...

                      def bar ...
Dynamic Invocation
VM Operations
                      Target
                      Object

                             associated with
   obj.foo()    JVM
                      Method
                       Table
   Call Site          def foo ...

                      def bar ...
Dynamic Invocation
VM Operations
  Method Lookup                 Target
                                Object

                                       associated with
   obj.foo()       JVM
                                Method
                                 Table
   Call Site                    def foo ...
                  def foo ...
                                def bar ...
Dynamic Invocation
VM Operations
  Method Lookup                 Target
     Branch
                                Object

                                       associated with
   obj.foo()       JVM
                                Method
                                 Table
   Call Site                    def foo ...
                  def foo ...
                                def bar ...
Dynamic Invocation
VM Operations
  Method Lookup              Target
     Branch
  Method Cache               Object

                                    associated with
   obj.foo()           JVM
         def foo ...
                             Method
                              Table
   Call Site                 def foo ...

                             def bar ...
Lazy Constants

• Call site just produces a value
• Value calculated once
• Subsequent access is direct, optimizable
Lazy Constant



                  JVM       Lazy
LAZY_CONST
                         Computation

  Call Site
Lazy Constant
VM Operations



                   JVM       Lazy
LAZY_CONST
                          Computation

   Call Site
Lazy Constant
VM Operations



                   JVM       Lazy
LAZY_CONST
                          Computation

   Call Site
Lazy Constant
VM Operations
  Calculate Value




                    JVM         Lazy
LAZY_CONST
                             Computation
                          value

   Call Site
Lazy Constant
VM Operations
  Calculate Value
 Bind Permanently



                     JVM      Lazy
LAZY_CONST
             value         Computation

   Call Site
Delegates

• Arbitrary function pointers
 • No anon inner classes
 • No bridge methods
• Java 8 “Lambda” will do this
Lambda Expression


                    Surrounding
String s ->   JVM      Class
toString()             lambda
                         body
  Call Site
Lambda Expression
VM Operations


                      Surrounding
 String s ->    JVM      Class
 toString()              lambda
                           body
   Call Site
Lambda Expression
VM Operations


                      Surrounding
 String s ->    JVM      Class
 toString()              lambda
                           body
   Call Site
Lambda Expression
VM Operations
Retrieve Lambda Obj



                            Surrounding
 String s ->
     lambda           JVM      Class
       body
 toString()

    Call Site
Lambda Expression
VM Operations
Retrieve Lambda Obj
  Bind and Invoke


                            Surrounding
 String s ->
     lambda           JVM      Class
       body
 toString()

    Call Site
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
How Do You Benefit?
Future
• New language impls
 • JavaScript: Dyn.js and Nashorn
 • Redline Smalltalk
• Improved language performance
 • JRuby, Groovy, Jython, Clojure, Scala
 • Java features too!
JRuby/Java 6   JRuby/Java 7
JRuby/Java 6                      JRuby/Java 7
                          Times Faster than Ruby 1.9.3
  5




3.75




 2.5




1.25




  0
       base64            richards              neural            redblack
JRuby/Java 6                        JRuby/Java 7
                             Times Faster than Ruby 1.9.3
  5




3.75




 2.5

                                               1.914

                          1.538                                       1.565
1.25   1.346




  0
          base64             richards              neural               redblack
JRuby/Java 6                        JRuby/Java 7
                                 Times Faster than Ruby 1.9.3
  5


                                                                                  4.32

3.75
                                                             3.66
                                       3.44



 2.5           2.658



                                                   1.914

                              1.538                                       1.565
1.25   1.346




  0
          base64                 richards              neural               redblack
invokedynamic
is the future of Java!
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!

More Related Content

PDF
Invoke dynamic your api to hotspot
Boundary
 
PDF
From dot net_to_rails
pythonandchips
 
PPTX
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
JVM: A Platform for Multiple Languages
Kris Mok
 
PPTX
Nashorn on JDK 8 (ADC2013)
Kris Mok
 
PDF
Intrinsic Methods in HotSpot VM
Kris Mok
 
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
Invoke dynamic your api to hotspot
Boundary
 
From dot net_to_rails
pythonandchips
 
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
Php extensions
Elizabeth Smith
 
JVM: A Platform for Multiple Languages
Kris Mok
 
Nashorn on JDK 8 (ADC2013)
Kris Mok
 
Intrinsic Methods in HotSpot VM
Kris Mok
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 

More from Charles Nutter (20)

PDF
The Year of JRuby - RubyC 2018
Charles Nutter
 
PDF
Down the Rabbit Hole: An Adventure in JVM Wonderland
Charles Nutter
 
PDF
Ruby Performance - The Last Mile - RubyConf India 2016
Charles Nutter
 
PDF
JRuby 9000 - Optimizing Above the JVM
Charles Nutter
 
PDF
JRuby and Invokedynamic - Japan JUG 2015
Charles Nutter
 
PDF
JRuby 9000 - Taipei Ruby User's Group 2015
Charles Nutter
 
PDF
Fast as C: How to Write Really Terrible Java
Charles Nutter
 
PDF
Open Source Software Needs You!
Charles Nutter
 
PDF
InvokeBinder: Fluent Programming for Method Handles
Charles Nutter
 
PDF
Over 9000: JRuby in 2015
Charles Nutter
 
PDF
Doing Open Source the Right Way
Charles Nutter
 
PDF
JRuby: The Hard Parts
Charles Nutter
 
PDF
Bringing Concurrency to Ruby - RubyConf India 2014
Charles Nutter
 
PDF
Beyond JVM - YOW! Sydney 2013
Charles Nutter
 
PDF
Beyond JVM - YOW! Brisbane 2013
Charles Nutter
 
PDF
Beyond JVM - YOW Melbourne 2013
Charles Nutter
 
PDF
Down the Rabbit Hole
Charles Nutter
 
PDF
The Future of JRuby - Baruco 2013
Charles Nutter
 
PDF
High Performance Ruby - E4E Conference 2013
Charles Nutter
 
PDF
Invokedynamic in 45 Minutes
Charles Nutter
 
The Year of JRuby - RubyC 2018
Charles Nutter
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Charles Nutter
 
Ruby Performance - The Last Mile - RubyConf India 2016
Charles Nutter
 
JRuby 9000 - Optimizing Above the JVM
Charles Nutter
 
JRuby and Invokedynamic - Japan JUG 2015
Charles Nutter
 
JRuby 9000 - Taipei Ruby User's Group 2015
Charles Nutter
 
Fast as C: How to Write Really Terrible Java
Charles Nutter
 
Open Source Software Needs You!
Charles Nutter
 
InvokeBinder: Fluent Programming for Method Handles
Charles Nutter
 
Over 9000: JRuby in 2015
Charles Nutter
 
Doing Open Source the Right Way
Charles Nutter
 
JRuby: The Hard Parts
Charles Nutter
 
Bringing Concurrency to Ruby - RubyConf India 2014
Charles Nutter
 
Beyond JVM - YOW! Sydney 2013
Charles Nutter
 
Beyond JVM - YOW! Brisbane 2013
Charles Nutter
 
Beyond JVM - YOW Melbourne 2013
Charles Nutter
 
Down the Rabbit Hole
Charles Nutter
 
The Future of JRuby - Baruco 2013
Charles Nutter
 
High Performance Ruby - E4E Conference 2013
Charles Nutter
 
Invokedynamic in 45 Minutes
Charles Nutter
 
Ad

Recently uploaded (20)

PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Doc9.....................................
SofiaCollazos
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Ad

InvokeDynamic - You Ain't Seen Nothin Yet

Editor's Notes