SlideShare a Scribd company logo
invokedynamic
                               IN 45 MINUTES!!!




Wednesday, February 6, 13
Me
                    • Charles Oliver Nutter
                     • headius@headius.com, @headius
                     • blog.headius.com
                    • JRuby Guy at Sun, Engine Yard, Red Hat
                    • JVM enthusiast, educator, contributor
                    • Earliest adopter of invokedynamic
Wednesday, February 6, 13
27 April, 2011




Wednesday, February 6, 13
History
                    •       JVM authors mentioned non-Java languages
                    •       Language authors have targeted JVM
                            •   Hundreds of JVM languages now
                    •       But JVM was a mismatch for many of them
                            •   Usually required tricks that defeated JVM
                                optimizations
                            •   Or required features JDK could not provide


Wednesday, February 6, 13
JVM Languages
                             Through the Years
                    • Early impls of Python (JPython), JS (Rhino)
                            and many others
                    • No official backing by Sun until 2006
                    • JRuby team hired
                    • JSR-292 “invokedynamic” rebooted in 2007
                    • Java 7 shipped invokedynamic in 2011
Wednesday, February 6, 13
What is
                            invokedynamic


Wednesday, February 6, 13
New Bytecode?
                            Well, yes...but what does that mean?
                                        And is that all?



Wednesday, February 6, 13
Wednesday, February 6, 13
New form of
                                invocation?
                        That’s one use, but there are many others




Wednesday, February 6, 13
Wednesday, February 6, 13
Only for Dynamic
                               Languages?
                            Dynamic dispatch is a common use,
                                but there are many others



Wednesday, February 6, 13
Wednesday, February 6, 13
A User-definable
                                 Bytecode
                            You decide how the JVM implements it




Wednesday, February 6, 13
A User-definable
                                  Bytecode
                            You decide how the JVM implements it

                                     +
                               Method Pointers
                                and Adapters
                            Faster than reflection, with user-defined
                             argument, flow, and exception handling
Wednesday, February 6, 13
Wednesday, February 6, 13
invokedynamic



Wednesday, February 6, 13
user-def’d bytecode
                             invokedynamic



Wednesday, February 6, 13
user-def’d bytecode   method pointers
                             invokedynamic



Wednesday, February 6, 13
bytecode + bootstrap
                            user-def’d bytecode    method pointers
                                                   MethodHandles
                             invokedynamic



Wednesday, February 6, 13
https://github.com/headius/indy_deep_dive




Wednesday, February 6, 13
MethodHandles



Wednesday, February 6, 13
Method Handles

                    • Function/field/array pointers
                    • Argument manipulation
                    • Flow control
                    • Optimizable by the JVM
                     • This is very important

Wednesday, February 6, 13
java.lang.invoke
                    • MethodHandle
                     • An invokable target + adaptations
                    • MethodType
                     • Representation of args + return type
                    • MethodHandles
                     • Utilities for acquiring, adapting handles
Wednesday, February 6, 13
MethodHandles.Lookup
                    • Method pointers
                     • findStatic, findVirtual,
                            findSpecial, findConstructor
                    • Field pointers
                     • findGetter, findSetter,
                            findStaticGetter, findStaticSetter


Wednesday, February 6, 13
Why Casting?
                      value1 = (String)mh1.invoke("java.home");
                      mh2.invoke((Object)"Hello, world");

                    • invoke() is “signature polymorphic”
                     • Call-side types define signature
                     • At compile time
                    • Signature must match MethodHandle type
                     • Or use invokeWithArguments
Wednesday, February 6, 13
Adapters

                    • Methods on j.l.i.MethodHandles
                    • Argument manipulation, modification
                    • Flow control and exception handling
                    • Similar to writing your own command-
                            pattern utility objects



Wednesday, February 6, 13
Argument Juggling

                    • insert, drop, permute
                    • filter, fold, cast
                    • splat (varargs), spread (unbox varargs)


Wednesday, February 6, 13
Flow Control
                    • guardWithTest: boolean branch
                     • condition, true path, false path
                     • Combination of three handles
                    • SwitchPoint: on/off branch
                     • true and false paths
                     • Once off, always off
Wednesday, February 6, 13
Exception Handling

                    • catchException
                     • body, exception type, handler
                    • throwException
                     • Throws Throwable in argument 0

Wednesday, February 6, 13
bytecode



Wednesday, February 6, 13
Goals of JSR 292

                    • A user-definable bytecode
                     • Full freedom to define VM behavior
                    • Fast method pointers + adapters
                    • Optimizable like normal Java code
                    • Avoid future modifications

Wednesday, February 6, 13
JVM 101




Wednesday, February 6, 13
JVM 101
                            200 opcodes




Wednesday, February 6, 13
JVM 101
                                 200 opcodes
                            Ten (or 16) “endpoints”




Wednesday, February 6, 13
JVM 101
                                   200 opcodes
                              Ten (or 16) “endpoints”
                 Invocation
       invokevirtual
      invokeinterface
       invokestatic
       invokespecial




Wednesday, February 6, 13
JVM 101
                                   200 opcodes
                              Ten (or 16) “endpoints”
                 Invocation         Field Access
       invokevirtual                getfield
      invokeinterface               setfield
       invokestatic                 getstatic
       invokespecial                setstatic




Wednesday, February 6, 13
JVM 101
                                   200 opcodes
                              Ten (or 16) “endpoints”
                 Invocation         Field Access        Array Access
       invokevirtual                getfield           *aload
      invokeinterface               setfield           *astore
       invokestatic                 getstatic      b,s,c,i,l,d,f,a
       invokespecial                setstatic




Wednesday, February 6, 13
JVM 101
                                   200 opcodes
                              Ten (or 16) “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
Wednesday, February 6, 13
The Problem
                    • JVM spec (pre-7) defined 200 opcodes
                    • All bytecode lives within these 200
                    • What if your use case does not fit?
                     • Dynamic language/dispatch
                     • Lazy initialization
                     • Non-Java features
Wednesday, February 6, 13
// Static
  System.currentTimeMillis()
  Math.log(1.0)
   
  // Virtual
  "hello".toUpperCase()
  System.out.println()
   
  // Interface
  myList.add("happy happy")
  myRunnable.run()
   
  // Special
  new ArrayList()
  super.equals(other)




Wednesday, February 6, 13
// Static
  invokestatic java/lang/System.currentTimeMillis:()J
  invokestatic java/lang/Math.log:(D)D

  // Virtual
  invokevirtual java/lang/String.toUpperCase:()Ljava/lang/String;
  invokevirtual java/io/PrintStream.println:()V

  // Interface
  invokeinterface java/util/List.add:(Ljava/lang/Object;)Z
  invokeinterface java/lang/Runnable.add:()V

  // Special
  invokespecial java/util/ArrayList.<init>:()V
  invokespecial java/lang/Object.equals:(java/lang/Object)Z




Wednesday, February 6, 13
invokestatic



  invokevirtual



  invokeinterface



  invokespecial




Wednesday, February 6, 13
invokevirtual                                  invokestatic
      1. Confirm object is of correct type            1. Confirm arguments are of correct type
      2. Confirm arguments are of correct type        2. Look up method on Java class
      3. Look up method on Java class                3. Cache method
      4. Cache method                                4. Invoke method
      5. Invoke method


      invokeinterface                                invokespecial
      1. Confirm object’s type implements interface   1. Confirm object is of correct type
      2. Confirm arguments are of correct type        2. Confirm arguments are of correct type
      3. Look up method on Java class                3. Confirm target method is visible
      4. Cache method                                4. Look up method on Java class
      5. Invoke method                               5. Cache method
                                                     6. Invoke method




Wednesday, February 6, 13
invokevirtual                                           invokestatic
      1. Confirm object is of correct type                     1. Confirm arguments are of correct type
      2. Confirm arguments are of correct type                 2. Look up method on Java class
      3. Look up method on Java class                         3. Cache method
      4. Cache method                                         4. Invoke method
      5. Invoke method


      invokeinterface                                         invokespecial
      1. Confirm object’s type implements interface            1. Confirm object is of correct type
      2. Confirm arguments are of correct type                 2. Confirm arguments are of correct type
      3. Look up method on Java class                         3. Confirm target method is visible
      4. Cache method                                         4. Look up method on Java class
      5. Invoke method                                        5. Cache method
                                                              6. Invoke method

                            invokedynamic
                            1. Call bootstrap handle (your code)
                            2. Bootstrap prepares CallSite + MethodHandle
                            3. MethodHandle invoked now and future (until CallSite changes)



Wednesday, February 6, 13
Wednesday, February 6, 13
invokedynamic bytecode




Wednesday, February 6, 13
invokedynamic bytecode




               bo
                 ot
                    st      ra
                              p
                                  m
                                   et
                                     ho
                                       d



Wednesday, February 6, 13
invokedynamic bytecode




               bo
                 ot
                    st      ra
                              p
                                  m
                                   et
                                     ho
                                       d             method handles



Wednesday, February 6, 13
invokedynamic bytecode

                                                       target method



               bo
                 ot
                    st      ra
                              p
                                  m
                                   et
                                     ho
                                       d             method handles



Wednesday, February 6, 13
invokedynamic bytecode

                                                       target method



               bo
                 ot
                    st      ra
                              p
                                  m
                                   et
                                     ho
                                       d             method handles



Wednesday, February 6, 13
invokedynamic bytecode

                                                       target method



               bo
                 ot
                    st      ra
                              p
                                  m
                                   et
                                     ho
                                       d             method handles



Wednesday, February 6, 13
All Together Now...



Wednesday, February 6, 13
Tools
                    • ObjectWeb's ASM library
                     • De facto standard bytecode library
                    • Jitescript
                     • DSL/fluent wrapper around ASM
                    • InvokeBinder
                     • DSL/fluent API for building MH chains
Wednesday, February 6, 13
#1: Trivial Binding

                    • Simple binding of a method
                     • j.l.i.ConstantCallSite
                    • Method returns String
                    • Print out String

Wednesday, February 6, 13
#2: Modifying the Site
                    • Toggle between two targets each call
                     • j.l.i.MutableCallSite
                    • Call site sent into methods
                     • ...so they can modify it
                    • Trivial example of late binding
                    • InvokeBinder usage
Wednesday, February 6, 13
#3: Dynamic Dispatch


                            •   Target method not known at compile time

                            •   Dynamically look up after bootstrap

                            •   Rebind call site to proper method




Wednesday, February 6, 13
StupidScript
                    •       push <string>: push string on stack

                    •       send <number>: call function with n args

                            •   One arg call: ["print", "Hello, world"]

                    •       def <name> 'n' <op1> [ 'n' <op2> ...] 'n' end

                            •   define function with given ops

                    •       One builtin: print (System.out.println)


Wednesday, February 6, 13
StupidScript

                    •       push <string>: push string on stack

                    •       send <number>: call function with n args

                    •       def <name> 'n' <op1> [ 'n' <op2> ...] 'n' end

                    •       One builtin: print (System.out.println)




Wednesday, February 6, 13
Implementation

                    • Simple parser generates AST
                    • Compiler walks AST, emits .class
                    • Top level code goes into run() method
                    • defs create additional methods

Wednesday, February 6, 13
Hello, world!


                             push Hello, world!
                             push print
                             send 1




Wednesday, February 6, 13
Define Function

                              def hello
                              push print
                              push Hello, world!
                              send 1
                              end




Wednesday, February 6, 13
Call Function


                               push hello
                               send 0




Wednesday, February 6, 13
More Information

                    • My blog: http://blog.headius.com
                    • Follow me: @headius
                    • Code on Github
                      • https://github.com/headius/indy_deep_dive
                    • Slides posted online

Wednesday, February 6, 13

More Related Content

What's hot

TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011
Lance Ball
 
TorqueBox - When Java meets Ruby
TorqueBox - When Java meets RubyTorqueBox - When Java meets Ruby
TorqueBox - When Java meets RubyBruno Oliveira
 
Torquebox - O melhor dos dois mundos
Torquebox - O melhor dos dois mundosTorquebox - O melhor dos dois mundos
Torquebox - O melhor dos dois mundosBruno Oliveira
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
Nick Sieger
 
Create *real* modular Java applications - a brief introduction -
Create *real* modular Java applications - a brief introduction -Create *real* modular Java applications - a brief introduction -
Create *real* modular Java applications - a brief introduction -
Jeffrey Groneberg
 
JRuby and You
JRuby and YouJRuby and You
JRuby and You
Hiro Asari
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
bobmcwhirter
 
When Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of TorqueboxWhen Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of Torquebox
rockyjaiswal
 
TorqueBox for Rubyists
TorqueBox for RubyistsTorqueBox for Rubyists
TorqueBox for Rubyists
bobmcwhirter
 
Complex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBox
bobmcwhirter
 
Expert JavaScript Programming
Expert JavaScript ProgrammingExpert JavaScript Programming
Expert JavaScript Programming
Yoshiki Shibukawa
 
GOTO Night with Charles Nutter Slides
GOTO Night with Charles Nutter SlidesGOTO Night with Charles Nutter Slides
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
Charla ruby nscodermad
Charla ruby nscodermadCharla ruby nscodermad
Charla ruby nscodermad
nscoder_mad
 
Java Online Training
Java Online TrainingJava Online Training
Java Online Training
Srihitha Technologies
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMTom Lee
 
Java Classroom Training
Java Classroom TrainingJava Classroom Training
Java Classroom Training
Srihitha Technologies
 

What's hot (19)

TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011
 
TorqueBox - When Java meets Ruby
TorqueBox - When Java meets RubyTorqueBox - When Java meets Ruby
TorqueBox - When Java meets Ruby
 
Torquebox - O melhor dos dois mundos
Torquebox - O melhor dos dois mundosTorquebox - O melhor dos dois mundos
Torquebox - O melhor dos dois mundos
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
Devignition 2011
Devignition 2011Devignition 2011
Devignition 2011
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Create *real* modular Java applications - a brief introduction -
Create *real* modular Java applications - a brief introduction -Create *real* modular Java applications - a brief introduction -
Create *real* modular Java applications - a brief introduction -
 
JRuby and You
JRuby and YouJRuby and You
JRuby and You
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
When Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of TorqueboxWhen Ruby Meets Java - The Power of Torquebox
When Ruby Meets Java - The Power of Torquebox
 
TorqueBox for Rubyists
TorqueBox for RubyistsTorqueBox for Rubyists
TorqueBox for Rubyists
 
Complex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBox
 
Expert JavaScript Programming
Expert JavaScript ProgrammingExpert JavaScript Programming
Expert JavaScript Programming
 
GOTO Night with Charles Nutter Slides
GOTO Night with Charles Nutter SlidesGOTO Night with Charles Nutter Slides
GOTO Night with Charles Nutter Slides
 
Charla ruby nscodermad
Charla ruby nscodermadCharla ruby nscodermad
Charla ruby nscodermad
 
Java Online Training
Java Online TrainingJava Online Training
Java Online Training
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVM
 
Java Classroom Training
Java Classroom TrainingJava Classroom Training
Java Classroom Training
 
Railsconf 2010
Railsconf 2010Railsconf 2010
Railsconf 2010
 

Similar to Invokedynamic in 45 Minutes

Designing Puppet: Roles/Profiles Pattern
Designing Puppet: Roles/Profiles PatternDesigning Puppet: Roles/Profiles Pattern
Designing Puppet: Roles/Profiles Pattern
Puppet
 
5 Of Our Favorite Ruby Gems
5 Of Our Favorite Ruby Gems5 Of Our Favorite Ruby Gems
5 Of Our Favorite Ruby Gems
Dan Pickett
 
Yet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRepYet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRep
Denish Patel
 
Javascript in the cloud
Javascript in the cloudJavascript in the cloud
Javascript in the cloud
Mostafa Eweda
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
Martijn Verburg
 
Polyglot and functional (Devoxx Nov/2011)
Polyglot and functional (Devoxx Nov/2011)Polyglot and functional (Devoxx Nov/2011)
Polyglot and functional (Devoxx Nov/2011)
Martijn Verburg
 
Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)
Martijn Verburg
 
Distributed Fuzzing Framework Design
Distributed Fuzzing Framework DesignDistributed Fuzzing Framework Design
Distributed Fuzzing Framework Design
bannedit
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
Boulder Java User's Group
 
GitHub Notable OSS Project
GitHub  Notable OSS ProjectGitHub  Notable OSS Project
GitHub Notable OSS Projectroumia
 
High Performance Ruby - E4E Conference 2013
High Performance Ruby - E4E Conference 2013High Performance Ruby - E4E Conference 2013
High Performance Ruby - E4E Conference 2013
Charles Nutter
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
ajuckel
 
Persistence
PersistencePersistence
Persistence
Anish Thomas Alex
 
Smalltalk and ruby - 2012-12-08
Smalltalk and ruby  - 2012-12-08Smalltalk and ruby  - 2012-12-08
Smalltalk and ruby - 2012-12-08
Koan-Sin Tan
 
Direct memory jugl-2012.03.08
Direct memory jugl-2012.03.08Direct memory jugl-2012.03.08
Direct memory jugl-2012.03.08
Benoit Perroud
 
Red Dirt Ruby Conference
Red Dirt Ruby ConferenceRed Dirt Ruby Conference
Red Dirt Ruby Conference
John Woodell
 
Extending Spring for Custom Usage
Extending Spring for Custom UsageExtending Spring for Custom Usage
Extending Spring for Custom Usage
Joshua Long
 
jQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVCjQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVC
Troy Miles
 
Euruko 2012 - JRuby
Euruko 2012 - JRubyEuruko 2012 - JRuby
Euruko 2012 - JRuby
Charles Nutter
 
Debugging rails
Debugging railsDebugging rails
Debugging rails
Michael Denomy
 

Similar to Invokedynamic in 45 Minutes (20)

Designing Puppet: Roles/Profiles Pattern
Designing Puppet: Roles/Profiles PatternDesigning Puppet: Roles/Profiles Pattern
Designing Puppet: Roles/Profiles Pattern
 
5 Of Our Favorite Ruby Gems
5 Of Our Favorite Ruby Gems5 Of Our Favorite Ruby Gems
5 Of Our Favorite Ruby Gems
 
Yet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRepYet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRep
 
Javascript in the cloud
Javascript in the cloudJavascript in the cloud
Javascript in the cloud
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
 
Polyglot and functional (Devoxx Nov/2011)
Polyglot and functional (Devoxx Nov/2011)Polyglot and functional (Devoxx Nov/2011)
Polyglot and functional (Devoxx Nov/2011)
 
Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)
 
Distributed Fuzzing Framework Design
Distributed Fuzzing Framework DesignDistributed Fuzzing Framework Design
Distributed Fuzzing Framework Design
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
GitHub Notable OSS Project
GitHub  Notable OSS ProjectGitHub  Notable OSS Project
GitHub Notable OSS Project
 
High Performance Ruby - E4E Conference 2013
High Performance Ruby - E4E Conference 2013High Performance Ruby - E4E Conference 2013
High Performance Ruby - E4E Conference 2013
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
 
Persistence
PersistencePersistence
Persistence
 
Smalltalk and ruby - 2012-12-08
Smalltalk and ruby  - 2012-12-08Smalltalk and ruby  - 2012-12-08
Smalltalk and ruby - 2012-12-08
 
Direct memory jugl-2012.03.08
Direct memory jugl-2012.03.08Direct memory jugl-2012.03.08
Direct memory jugl-2012.03.08
 
Red Dirt Ruby Conference
Red Dirt Ruby ConferenceRed Dirt Ruby Conference
Red Dirt Ruby Conference
 
Extending Spring for Custom Usage
Extending Spring for Custom UsageExtending Spring for Custom Usage
Extending Spring for Custom Usage
 
jQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVCjQuery Mobile, Backbone.js, and ASP.NET MVC
jQuery Mobile, Backbone.js, and ASP.NET MVC
 
Euruko 2012 - JRuby
Euruko 2012 - JRubyEuruko 2012 - JRuby
Euruko 2012 - JRuby
 
Debugging rails
Debugging railsDebugging rails
Debugging rails
 

More from Charles Nutter

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

More from Charles Nutter (20)

The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandDown the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM Wonderland
 
Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016Ruby Performance - The Last Mile - RubyConf India 2016
Ruby Performance - The Last Mile - RubyConf India 2016
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVM
 
JRuby and Invokedynamic - Japan JUG 2015
JRuby and Invokedynamic - Japan JUG 2015JRuby and Invokedynamic - Japan JUG 2015
JRuby and Invokedynamic - Japan JUG 2015
 
JRuby 9000 - Taipei Ruby User's Group 2015
JRuby 9000 - Taipei Ruby User's Group 2015JRuby 9000 - Taipei Ruby User's Group 2015
JRuby 9000 - Taipei Ruby User's Group 2015
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
 
Open Source Software Needs You!
Open Source Software Needs You!Open Source Software Needs You!
Open Source Software Needs You!
 
InvokeBinder: Fluent Programming for Method Handles
InvokeBinder: Fluent Programming for Method HandlesInvokeBinder: Fluent Programming for Method Handles
InvokeBinder: Fluent Programming for Method Handles
 
Over 9000: JRuby in 2015
Over 9000: JRuby in 2015Over 9000: JRuby in 2015
Over 9000: JRuby in 2015
 
Doing Open Source the Right Way
Doing Open Source the Right WayDoing Open Source the Right Way
Doing Open Source the Right Way
 
JRuby: The Hard Parts
JRuby: The Hard PartsJRuby: The Hard Parts
JRuby: The Hard Parts
 
Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014
 
Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013Beyond JVM - YOW! Sydney 2013
Beyond JVM - YOW! Sydney 2013
 
Beyond JVM - YOW! Brisbane 2013
Beyond JVM - YOW! Brisbane 2013Beyond JVM - YOW! Brisbane 2013
Beyond JVM - YOW! Brisbane 2013
 
Beyond JVM - YOW Melbourne 2013
Beyond JVM - YOW Melbourne 2013Beyond JVM - YOW Melbourne 2013
Beyond JVM - YOW Melbourne 2013
 
Down the Rabbit Hole
Down the Rabbit HoleDown the Rabbit Hole
Down the Rabbit Hole
 
The Future of JRuby - Baruco 2013
The Future of JRuby - Baruco 2013The Future of JRuby - Baruco 2013
The Future of JRuby - Baruco 2013
 
Why JRuby? - RubyConf 2012
Why JRuby? - RubyConf 2012Why JRuby? - RubyConf 2012
Why JRuby? - RubyConf 2012
 
Aloha RubyConf 2012 - JRuby
Aloha RubyConf 2012 - JRubyAloha RubyConf 2012 - JRuby
Aloha RubyConf 2012 - JRuby
 

Recently uploaded

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 

Recently uploaded (20)

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 

Invokedynamic in 45 Minutes

  • 1. invokedynamic IN 45 MINUTES!!! Wednesday, February 6, 13
  • 2. Me • Charles Oliver Nutter • headius@headius.com, @headius • blog.headius.com • JRuby Guy at Sun, Engine Yard, Red Hat • JVM enthusiast, educator, contributor • Earliest adopter of invokedynamic Wednesday, February 6, 13
  • 3. 27 April, 2011 Wednesday, February 6, 13
  • 4. History • JVM authors mentioned non-Java languages • Language authors have targeted JVM • Hundreds of JVM languages now • But JVM was a mismatch for many of them • Usually required tricks that defeated JVM optimizations • Or required features JDK could not provide Wednesday, February 6, 13
  • 5. JVM Languages Through the Years • Early impls of Python (JPython), JS (Rhino) and many others • No official backing by Sun until 2006 • JRuby team hired • JSR-292 “invokedynamic” rebooted in 2007 • Java 7 shipped invokedynamic in 2011 Wednesday, February 6, 13
  • 6. What is invokedynamic Wednesday, February 6, 13
  • 7. New Bytecode? Well, yes...but what does that mean? And is that all? Wednesday, February 6, 13
  • 9. New form of invocation? That’s one use, but there are many others Wednesday, February 6, 13
  • 11. Only for Dynamic Languages? Dynamic dispatch is a common use, but there are many others Wednesday, February 6, 13
  • 13. A User-definable Bytecode You decide how the JVM implements it Wednesday, February 6, 13
  • 14. A User-definable Bytecode You decide how the JVM implements it + Method Pointers and Adapters Faster than reflection, with user-defined argument, flow, and exception handling Wednesday, February 6, 13
  • 17. user-def’d bytecode invokedynamic Wednesday, February 6, 13
  • 18. user-def’d bytecode method pointers invokedynamic Wednesday, February 6, 13
  • 19. bytecode + bootstrap user-def’d bytecode method pointers MethodHandles invokedynamic Wednesday, February 6, 13
  • 22. Method Handles • Function/field/array pointers • Argument manipulation • Flow control • Optimizable by the JVM • This is very important Wednesday, February 6, 13
  • 23. java.lang.invoke • MethodHandle • An invokable target + adaptations • MethodType • Representation of args + return type • MethodHandles • Utilities for acquiring, adapting handles Wednesday, February 6, 13
  • 24. MethodHandles.Lookup • Method pointers • findStatic, findVirtual, findSpecial, findConstructor • Field pointers • findGetter, findSetter, findStaticGetter, findStaticSetter Wednesday, February 6, 13
  • 25. Why Casting? value1 = (String)mh1.invoke("java.home"); mh2.invoke((Object)"Hello, world"); • invoke() is “signature polymorphic” • Call-side types define signature • At compile time • Signature must match MethodHandle type • Or use invokeWithArguments Wednesday, February 6, 13
  • 26. Adapters • Methods on j.l.i.MethodHandles • Argument manipulation, modification • Flow control and exception handling • Similar to writing your own command- pattern utility objects Wednesday, February 6, 13
  • 27. Argument Juggling • insert, drop, permute • filter, fold, cast • splat (varargs), spread (unbox varargs) Wednesday, February 6, 13
  • 28. Flow Control • guardWithTest: boolean branch • condition, true path, false path • Combination of three handles • SwitchPoint: on/off branch • true and false paths • Once off, always off Wednesday, February 6, 13
  • 29. Exception Handling • catchException • body, exception type, handler • throwException • Throws Throwable in argument 0 Wednesday, February 6, 13
  • 31. Goals of JSR 292 • A user-definable bytecode • Full freedom to define VM behavior • Fast method pointers + adapters • Optimizable like normal Java code • Avoid future modifications Wednesday, February 6, 13
  • 33. JVM 101 200 opcodes Wednesday, February 6, 13
  • 34. JVM 101 200 opcodes Ten (or 16) “endpoints” Wednesday, February 6, 13
  • 35. JVM 101 200 opcodes Ten (or 16) “endpoints” Invocation invokevirtual invokeinterface invokestatic invokespecial Wednesday, February 6, 13
  • 36. JVM 101 200 opcodes Ten (or 16) “endpoints” Invocation Field Access invokevirtual getfield invokeinterface setfield invokestatic getstatic invokespecial setstatic Wednesday, February 6, 13
  • 37. JVM 101 200 opcodes Ten (or 16) “endpoints” Invocation Field Access Array Access invokevirtual getfield *aload invokeinterface setfield *astore invokestatic getstatic b,s,c,i,l,d,f,a invokespecial setstatic Wednesday, February 6, 13
  • 38. JVM 101 200 opcodes Ten (or 16) “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 Wednesday, February 6, 13
  • 39. The Problem • JVM spec (pre-7) defined 200 opcodes • All bytecode lives within these 200 • What if your use case does not fit? • Dynamic language/dispatch • Lazy initialization • Non-Java features Wednesday, February 6, 13
  • 40. // Static System.currentTimeMillis() Math.log(1.0)   // Virtual "hello".toUpperCase() System.out.println()   // Interface myList.add("happy happy") myRunnable.run()   // Special new ArrayList() super.equals(other) Wednesday, February 6, 13
  • 41. // Static invokestatic java/lang/System.currentTimeMillis:()J invokestatic java/lang/Math.log:(D)D // Virtual invokevirtual java/lang/String.toUpperCase:()Ljava/lang/String; invokevirtual java/io/PrintStream.println:()V // Interface invokeinterface java/util/List.add:(Ljava/lang/Object;)Z invokeinterface java/lang/Runnable.add:()V // Special invokespecial java/util/ArrayList.<init>:()V invokespecial java/lang/Object.equals:(java/lang/Object)Z Wednesday, February 6, 13
  • 42. invokestatic invokevirtual invokeinterface invokespecial Wednesday, February 6, 13
  • 43. invokevirtual invokestatic 1. Confirm object is of correct type 1. Confirm arguments are of correct type 2. Confirm arguments are of correct type 2. Look up method on Java class 3. Look up method on Java class 3. Cache method 4. Cache method 4. Invoke method 5. Invoke method invokeinterface invokespecial 1. Confirm object’s type implements interface 1. Confirm object is of correct type 2. Confirm arguments are of correct type 2. Confirm arguments are of correct type 3. Look up method on Java class 3. Confirm target method is visible 4. Cache method 4. Look up method on Java class 5. Invoke method 5. Cache method 6. Invoke method Wednesday, February 6, 13
  • 44. invokevirtual invokestatic 1. Confirm object is of correct type 1. Confirm arguments are of correct type 2. Confirm arguments are of correct type 2. Look up method on Java class 3. Look up method on Java class 3. Cache method 4. Cache method 4. Invoke method 5. Invoke method invokeinterface invokespecial 1. Confirm object’s type implements interface 1. Confirm object is of correct type 2. Confirm arguments are of correct type 2. Confirm arguments are of correct type 3. Look up method on Java class 3. Confirm target method is visible 4. Cache method 4. Look up method on Java class 5. Invoke method 5. Cache method 6. Invoke method invokedynamic 1. Call bootstrap handle (your code) 2. Bootstrap prepares CallSite + MethodHandle 3. MethodHandle invoked now and future (until CallSite changes) Wednesday, February 6, 13
  • 47. invokedynamic bytecode bo ot st ra p m et ho d Wednesday, February 6, 13
  • 48. invokedynamic bytecode bo ot st ra p m et ho d method handles Wednesday, February 6, 13
  • 49. invokedynamic bytecode target method bo ot st ra p m et ho d method handles Wednesday, February 6, 13
  • 50. invokedynamic bytecode target method bo ot st ra p m et ho d method handles Wednesday, February 6, 13
  • 51. invokedynamic bytecode target method bo ot st ra p m et ho d method handles Wednesday, February 6, 13
  • 53. Tools • ObjectWeb's ASM library • De facto standard bytecode library • Jitescript • DSL/fluent wrapper around ASM • InvokeBinder • DSL/fluent API for building MH chains Wednesday, February 6, 13
  • 54. #1: Trivial Binding • Simple binding of a method • j.l.i.ConstantCallSite • Method returns String • Print out String Wednesday, February 6, 13
  • 55. #2: Modifying the Site • Toggle between two targets each call • j.l.i.MutableCallSite • Call site sent into methods • ...so they can modify it • Trivial example of late binding • InvokeBinder usage Wednesday, February 6, 13
  • 56. #3: Dynamic Dispatch • Target method not known at compile time • Dynamically look up after bootstrap • Rebind call site to proper method Wednesday, February 6, 13
  • 57. StupidScript • push <string>: push string on stack • send <number>: call function with n args • One arg call: ["print", "Hello, world"] • def <name> 'n' <op1> [ 'n' <op2> ...] 'n' end • define function with given ops • One builtin: print (System.out.println) Wednesday, February 6, 13
  • 58. StupidScript • push <string>: push string on stack • send <number>: call function with n args • def <name> 'n' <op1> [ 'n' <op2> ...] 'n' end • One builtin: print (System.out.println) Wednesday, February 6, 13
  • 59. Implementation • Simple parser generates AST • Compiler walks AST, emits .class • Top level code goes into run() method • defs create additional methods Wednesday, February 6, 13
  • 60. Hello, world! push Hello, world! push print send 1 Wednesday, February 6, 13
  • 61. Define Function def hello push print push Hello, world! send 1 end Wednesday, February 6, 13
  • 62. Call Function push hello send 0 Wednesday, February 6, 13
  • 63. More Information • My blog: http://blog.headius.com • Follow me: @headius • Code on Github • https://github.com/headius/indy_deep_dive • Slides posted online Wednesday, February 6, 13