Search |
||
Closure and performancePosted by forax on August 24, 2006 at 5:09 AM PDT
I've read the proposal about function types and closures and i have noticed that the invocation of a function type will use the newly introduced bytecode invokedynamic (the gilad blog about invokedynamic) and i think i know why !
So let me introduce a small example of closure, the method
transform applies a transformation
on each item of an array and store the result in the same array.
static CharSequence[] transform(CharSequence(CharSequence) transformation,CharSequence... array) {
for(int i=0;i<array.length;i++) {
array[i]=transformation(array[i]);
}
return array;
}
The following code applies an identity transformation :
CharSequence(CharSequence) identity = (CharSequence seq) {return seq; };
CharSequence[] seqs=transform(identity,"2","3");
System.out.println(Arrays.toString(seqs));
So why closure can't use a classical call (invokevirtual)
instead of a invokedynamic call,
because the proposal defines subtyping rules between closures.
String(Object) bang = (Object o) {return "!"+o; };
CharSequence[] seqs2=transform(bang,"2","3");
System.out.println(Arrays.toString(seqs2));
So in the method transform, the call to transformation can call a function type with a different signature that the one declared, that's why invokedynamic must be used here.
So why using invokedynamic is important, it's because it can
hurt performance, invoking a method without the same signature
is more complex than a simple call.
»
Related Topics >>
Open JDK Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|