1 / 47

N ew JVM Plumbing: Method Handles and More

N ew JVM Plumbing: Method Handles and More. April 15, 2009 John R. Rose, Sr. Staff Engineer john.rose@sun.com http://blogs.sun.com/jrose. N ew JVM Plumbing: Method Handles and More. April 15, 2009 John R. Rose, Sr. Staff Engineer john.rose@sun.com http://blogs.sun.com/jrose.

anila
Download Presentation

N ew JVM Plumbing: Method Handles and More

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. New JVM Plumbing:Method Handles and More • April 15, 2009 • John R. Rose, Sr. Staff Engineer • john.rose@sun.com • http://blogs.sun.com/jrose

  2. New JVM Plumbing:Method Handles and More • April 15, 2009 • John R. Rose, Sr. Staff Engineer • john.rose@sun.com • http://blogs.sun.com/jrose

  3. 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(…)

  4. Why Method Handles? • Dynamic languages need programmable linkage • The subject of a linkage step is (still) a method • Reflection, adapter classes are too indirectIndirection  bulky, awkward to work with, slow to call.

  5. New JVM Plumbing:Method Handles and More • April 15, 2009 • John R. Rose, Sr. Staff Engineer • john.rose@sun.com • http://blogs.sun.com/jrose

  6. 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?

  7. 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.

  8. 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)

  9. 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.

  10. 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.)

  11. New JVM Plumbing:Method Handles and More • April 15, 2009 • John R. Rose, Sr. Staff Engineer • john.rose@sun.com • http://blogs.sun.com/jrose

  12. 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(…)

  13. Method Handles • Function Pointers for the JVM! • // interface MethodHandle<T extends MethodType<R,A...>>// { T type(); <R,A...> public R invoke(A...); } • publicclass MethodHandle extends MethodHandleImpl {public MethodType type();}

  14. Method Handles, common structure

  15. 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

  16. 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

  17. 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!)

  18. Direct Method Handles

  19. Bound Method Handles

  20. Adapter Method Handles

  21. Invokedynamic call sites

  22. Invokedynamic, inline cache

  23. 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

  24. Virtual call site details

  25. Virtual call site details

  26. Virtual call site details

  27. Virtual call site details

  28. Direct Method Handle call details

  29. Direct Method Handle call details

  30. Direct Method Handle call details

  31. Direct Method Handle call details

  32. Bound Method Handle call details

  33. Bound Method Handle call details

  34. Bound Method Handle call details

  35. Bound Method Handle call details

  36. Bound Method Handle call details

  37. 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)

  38. 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.net

  39. 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

  40. 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.

  41. 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. • (Hey, no need to needle me here!)

  42. 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

  43. Applications • Traits, categories, protocols, patterns • Class-customized behaviors • Metaobject protocols (multiply coexisting) • Retrofitting jobs

  44. Interfaces, the old fashioned way

  45. A retrofit interface, injected

  46. A MOP interface, injected

  47. Questions?Let's talk... John Rose john.rose@sun.com

More Related