Wednesday, December 26, 2007

Inlining closure

I did some manual experiments of inlining the body of closure from the previous post to make it fast. So far the inlining technique would work only for anonymous closures as it won't be referred by other codes.

For example, if a$a is an anonymous closure:

DefaultMethods.times(100, a$a);


can be inlined as the private method inlined_DefaultMethods_times:

inlined_DefaultMethods_times(100);



Then the body of DefaultMethods#times will be copied to construct the above private method. After that the callsite of Closure a$a will be replaced with its body, the method a$a__0.

The original DefaultMethods#times:

public static void times(int num, Closure closure) {
for(int i=0;i<num;i++) {
closure.call(i);
}
}



The inlined method inlined_DefaultMethod_times:

private void inlined_DefaultMethod_times(int num) {
for(int i=0;i<num;i++) {
a$a__0(i);
}
}



The result is reallly impressive, 22 sec, which is comparable to original Java code.

No comments: