new jvm plumbingcr.openjdk.java.net/~jrose/pres/200906-langnetdvm.pdf7 invokedynamic call site...
TRANSCRIPT
![Page 1: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/1.jpg)
1
New JVM Plumbing:
Method Handles and More
• April 15, 2009 • John R. Rose, Sr. Staff Engineer • [email protected] • http://blogs.sun.com/jrose
![Page 2: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/2.jpg)
2
Method Handles 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: MethodHandle.invoke(…)
![Page 3: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/3.jpg)
3
Why Method Handles? • Dynamic languages need programmable linkage • The subject of a linkage step is (still) a method • Reflection, adapter classes are too indirect
Indirection bulky, awkward to work with, slow to call.
![Page 4: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/4.jpg)
4
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 paths
Key question: What does a normal method call site look like, after you make it user-linkable?
![Page 5: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/5.jpg)
5
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.
![Page 6: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/6.jpg)
6
How flexible is the target method? Method calls with useful degrees of freedom: • Direct linkage to any Java API • Adjustment of minor type mismatches • Currying (e.g., over runtime control info) • Inline cache combinators (e.g., type guards)
![Page 7: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/7.jpg)
7
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.
![Page 8: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/8.jpg)
8
Invokedynamic linkage state: • A MethodHandle 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.)
![Page 9: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/9.jpg)
Method Handles 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: MethodHandle.invoke(…)
![Page 10: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/10.jpg)
10
Method Handles • Function Pointers for the JVM!
// interface MethodHandle<T extends MethodType<R,A...>> // { T type(); <R,A...> public R invoke(A...); }
public class MethodHandle extends MethodHandleImpl { public MethodType type(); }
![Page 11: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/11.jpg)
11
Method Handles, common structure
![Page 12: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/12.jpg)
12
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 adapted
![Page 13: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/13.jpg)
13
Like (Inner) Classes? Yes/no. • Fast to call, using any signature • Lightweight instances, can fold in data • Can export private capabilities But: • Low-level token-based structural typing • Fixed API, drab names (type, invoke) • No checked exceptions
![Page 14: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/14.jpg)
14
Like C# Delegates? Yes/no. • Lightweight, immutable, functional • Direct linkage via a “magic” VM pointer • A few indirections slower than a regular call But: • Structural type tokens, not nominal classes • No multicast or coroutine operations • No VM support for direct creation (yet!)
![Page 15: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/15.jpg)
15
Direct Method Handles
![Page 16: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/16.jpg)
16
Bound Method Handles
![Page 17: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/17.jpg)
17
Adapter Method Handles
![Page 18: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/18.jpg)
18
Invokedynamic call sites
![Page 19: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/19.jpg)
19
Invokedynamic, inline cache
![Page 20: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/20.jpg)
20
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 changes
![Page 21: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/21.jpg)
21
Virtual call site details
![Page 22: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/22.jpg)
22
Direct Method Handle call details
![Page 23: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/23.jpg)
23
Bound Method Handle call details
![Page 24: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/24.jpg)
24
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)
![Page 25: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/25.jpg)
25
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 [email protected]
![Page 26: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/26.jpg)
26
JSR 292 Status • First JVM support for MH committed 4/2009 • Invokedynamic RI under code review • Minor Java language changes under review • JSR 292 specification moving slowly & surely
Next Stop: JavaOne JDK 7 Preview
![Page 27: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/27.jpg)
27
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.
![Page 28: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/28.jpg)
28
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-private
![Page 29: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/29.jpg)
29
Applications • Traits, categories, protocols, patterns • Class-customized behaviors • Metaobject protocols (multiply coexisting) • Retrofitting jobs
![Page 30: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/30.jpg)
30
Interfaces, the old fashioned way
![Page 31: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/31.jpg)
31
A retrofit interface, injected
![Page 32: New JVM Plumbingcr.openjdk.java.net/~jrose/pres/200906-LangNetDVM.pdf7 Invokedynamic call site contents: • A method signature (immutable, type-safe) • A method name (arbitrary](https://reader033.vdocument.in/reader033/viewer/2022050314/5f767195737513405a3d480b/html5/thumbnails/32.jpg)
32
A MOP interface, injected