invoke dynamic

20
InvokeDynamic Java 7 dynamically typed language support [email protected]

Upload: neueda

Post on 10-May-2015

2.806 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Invoke Dynamic

InvokeDynamicJava 7 dynamically typed language support

[email protected]

Page 2: Invoke Dynamic
Page 3: Invoke Dynamic

Statically typed languages

Java

Scala

C#

C

C++

F#

Haskel

Page 4: Invoke Dynamic

Dynamic languages

Groovy

Clojure

JavaScript

Perl

Python

Ruby

PHP

Page 5: Invoke Dynamic

Dynamic language

object runtime alteration (open program code)

metaobject protocol

eval

functional programming, closures, macros

Page 6: Invoke Dynamic

Dynamically typed language

type checking performed at runtime

variable does not have a type

value is associated with a type

duck typing

Page 7: Invoke Dynamic

The program must be compiled to bytecode to achieve Java level performance

Page 8: Invoke Dynamic

Use case

groovy:000> f = { it + it }===> groovysh_evaluate$_run_closure1@dc737begroovy:000> f(1) ===> 2groovy:000> f("qw")===> qwqwgroovy:000> [2, "qwe"].collect(f)===> [4, qweqwe]groovy:000>

Page 9: Invoke Dynamic

collect (list, func) { foreach (list) push (result, func (it))}

Page 10: Invoke Dynamic

The probleminvokevirtual

some/class/Name.func(Ljava/lang/String)Z,

objectref,

arg1, arg2, ...

invokespecial

invokestatic has similar calling convention

invokeinterface

Page 11: Invoke Dynamic

The solution

Reflection is (relatively) slow

There are others possibilities

Page 12: Invoke Dynamic

Java 7 approach - JSR292

invokedynamic (indy)

boostrapFuncRef,

NameLiteral(Lorg/jruby/runtime/builtin/IRubyObject)

Lorg/jruby/runtime/builtin/IRubyObject,

arg1, arg2, ...

Page 13: Invoke Dynamic

java.lang.invoke.*

MethodHandle

CallSite

ConstantCallSite

MutableCallSite

VolatileCallSite

MethodType

Page 14: Invoke Dynamic

public static CallSite bootstrap ( MethodHandles.Lookup lookup, String dynMethodName, MethodType dynMethodType) throws Throwable {

MethodHandle handle = lookup.findStatic( SomeClosure.class, "func", MethodType.methodType( Integer.class, Object.class, Object.class)); if (!dynMethodType.equals(handle.type())) handle = handle.asType(dynMethodType);

return new ConstantCallSite(handle);}

Page 15: Invoke Dynamic

MutableCallSite.setTarget(MethodHandle newTarget)

Page 16: Invoke Dynamic

Advantages

fast

JIT-ted

inlined

signature polymorphism, arguments adaptation

Page 17: Invoke Dynamic
Page 18: Invoke Dynamic
Page 19: Invoke Dynamic

Work in ProgressJRuby - works, sort of; Mirah

Rhino (JavaScript) - J.Rose experiment, V8 for performance freaks

Groovy - proposed for 1.9, no impl

Jython (Python) - different priorities: PyPy, Cython, language features upgrade

Clojure - many features not applicable to Indy

PHP.reboot

Page 20: Invoke Dynamic

Learn more

http://download.java.net/jdk7/docs/technotes/guides/vm/multiple-language-support.html

JDK 7 JSR-292 java.lang.invoke.* API

JRuby source: src/org/jruby/compiler/impl/*.java

http://code.google.com/p/jsr292-cookbook/

http://mail.openjdk.java.net/pipermail/mlvm-dev/

http://www.oracle.com/technetwork/issue-archive/2010/10-may/o30java-099612.html

http://blog.headius.com/2008/09/first-taste-of-invokedynamic.html