Method Handles - Community Code Review - Java
Method Handles - Community Code Review - Java Method Handles - Community Code Review - Java
New JVM Plumbing:Method Handles and More• April 15, 2009• John R. Rose, Sr. Staff Engineer• john.rose@sun.com• http://blogs.sun.com/jrose1
- Page 2: Method Handles are…• Anonymous
- Page 5 and 6: What Invokedynamic wanted to be:•
- Page 7 and 8: Invokedynamic call site contents:
- Page 9 and 10: Method Handles are…• Anonymous
- Page 11 and 12: Method Handles, common structure11
- Page 13 and 14: Like (Inner) Classes? Yes/no.• Fa
- Page 15 and 16: Direct Method Handles15
- Page 17 and 18: Adapter Method Handles17
- Page 20 and 21: More details about calling…• in
- Page 22 and 23: Direct Method Handle call details22
- Page 24 and 25: Cool! What else is cooking?Da Vinci
- Page 26 and 27: JSR 292 Status• First JVM support
- Page 28 and 29: Injected methods• Presented as a
- Page 30 and 31: Interfaces, the old fashioned way30
- Page 32 and 33: A MOP interface, injected32
New JVM Plumbing:<strong>Method</strong> <strong>Handles</strong> and More• April 15, 2009• John R. Rose, Sr. Staff Engineer• john.rose@sun.com• http://blogs.sun.com/jrose1
<strong>Method</strong> <strong>Handles</strong> are…• Anonymous references to JVM methods• Like methods, can have any function type• Unlike methods, not named• Unlike (other) objects, signature-polymorphic• Called like methods: <strong>Method</strong>Handle.invoke(…)2
We were aiming at Invokedynamic• We first tried reflective methods for linkage• But the indirect representation was VM-hostile• The right design avoids reflection on fast pathsKey question: What does a normal method callsite look like, after you make it user-linkable?4
What Invokedynamic wanted to be:• Reflectively programmable linkage,• Resulting in (almost) regular method calls:• fast use of any signature,• type-safe,• inlinable, optimizable,• To a flexibly specified target.5
How flexible is the target method?<strong>Method</strong> calls with useful degrees of freedom:• Direct linkage to any <strong>Java</strong> API• Adjustment of minor type mismatches• Currying (e.g., over runtime control info)• Inline cache combinators (e.g., type guards)6
Invokedynamic call site contents:• A method signature (immutable, type-safe)• A method name (arbitrary pass-through string)• The enclosing caller class (modularity, access)• A class-specific bootstrap method (-handle)• A CallSite object which reifies it all.It is all immutable, except for linkage state.7
Invokedynamic linkage state:• A <strong>Method</strong>Handle property: CallSite.getTarget• When the site is unlinked, the target is null.• The target is mutable, may be set at any time.• Changing a target may affect compilation, etc.(Compare DLR call sites, which contain compiled trees.)8
<strong>Method</strong> <strong>Handles</strong> are…• Anonymous references to JVM methods• Like methods, can have any function type• Unlike methods, not named• Unlike (other) objects, signature-polymorphic• Called like methods: <strong>Method</strong>Handle.invoke(…)
<strong>Method</strong> <strong>Handles</strong>• Function Pointers for the JVM!// interface <strong>Method</strong>Handle// { T type(); public R invoke(A...); }public class <strong>Method</strong>Handle extends <strong>Method</strong>HandleImpl {public <strong>Method</strong>Type type();}10
<strong>Method</strong> <strong>Handles</strong>, common structure11
Like Reflection? No.• No boxing, no varargs, no exception wrapping• Access checks performed on creation, not call(thus, a conferrable call capability)• Calls are (typically) routed direct to the callee• Small, lightweight• Totally opaque: no symbol table info• Can be bound (curried) or adapted12
Like (Inner) Classes? Yes/no.• Fast to call, using any signature• Lightweight instances, can fold in data• Can export private capabilitiesBut:• Low-level token-based structural typing• Fixed API, drab names (type, invoke)• No checked exceptions13
Like C# Delegates? Yes/no.• Lightweight, immutable, functional• Direct linkage via a “magic” VM pointer• A few indirections slower than a regular callBut:• Structural type tokens, not nominal classes• No multicast or coroutine operations• No VM support for direct creation (yet!)14
Direct <strong>Method</strong> <strong>Handles</strong>15
Bound <strong>Method</strong> <strong>Handles</strong>16
Adapter <strong>Method</strong> <strong>Handles</strong>17
Invokedynamic call sites18
More details about calling…• invokevirtual MH.invoke uses no vtable• The call must check a signature type token• The call indirects through a trampoline field• Invokedynamic can fold those steps• But it must monitor the call site for changes20
Virtual call site details21
Direct <strong>Method</strong> Handle call details22
Bound <strong>Method</strong> Handle call details23
Cool! What else is cooking?Da Vinci Machine Project patch repository:• Anonymous classes done• Tailcalls prototype (includes permission checks)• Continuations prototype ([un]bounded, serializable)• Interface injection early code• (Value types need a sponsor)24
OK! Who’s using it?• JRuby simplifying call paths• Jython plans to do the same• New “mlvm-scheme” project at JKU/Linz• See traffic at mlvm-dev@openjdk.java.net25
JSR 292 Status• First JVM support for MH committed 4/2009• Invokedynamic RI under code review• Minor <strong>Java</strong> language changes under review• JSR 292 specification moving slowly & surelyNext Stop: <strong>Java</strong>One JDK 7 Preview26
Interface Injection• Alternative to “monkey-patching”• Structured, modular, monotonic semantics• Interfaces can be marked injectable• Each class is examined exactly once, and• either the interface injects itself,• or it permanently refuses to do so.27
Injected methods• Presented as a tuple of method handles• Constructed (or not) by an injection handler• Handle is specified by the injectable interface• Not by the subject class• No private access to the subject class• Invisible except via the interface,• …which could well be package-private28
Applications• Traits, categories, protocols, patterns• Class-customized behaviors• Metaobject protocols (multiply coexisting)• Retrofitting jobs29
Interfaces, the old fashioned way30
A retrofit interface, injected31
A MOP interface, injected32
Questions?Let's talk...John Rosejohn.rose@sun.com