Project Lambda: Java Language Specification draft

Fredrik Öhrström fredrik.ohrstrom at oracle.com
Thu Jan 28 01:41:55 PST 2010


Zdenek Tronicek skrev:
> Of course, this is a perfect motivation. But, for me, it is so natural as
> if the compiler extract assignment to x out of the loop:
>
> int x;
> for (int i = 0; i < 10; i++) {
>   x = 42;
>   System.out.println(i);
> }
>   
Actually, in the snippet above the assignment to x is dead code and will
be completely removed by a reasonably clever JVM. But yes, I agree, if
you do something with x after the loop, then there should be no
assignment inside the loop.

Now, if x was not a primitive, then the JVM has to inline the
constructor et al, to be able to verify that the object does not escape
or have any side effects that will prevent us from moving it out of the
loop.

Should javac assist the JVM by moving things outside of the loop? I am
not sure, I think it would be preferable if the lambda-implementation is
designed in such a way that the JVM can figure this out with only a few
inlines. No premature optimizations in the bytecode please.

//Fredrik


More information about the lambda-dev mailing list