guardWithTest

Fredrik Öhrström fredrik.ohrstrom at oracle.com
Mon May 25 07:13:50 PDT 2009


Arthur Peters skrev:
> I'm a lurker, but I have a question.
>
> What makes guardWithTest better or faster than simply using a method
> handle of a method with an if statement in it?
>
> Like:
> Object meth(...args...)  {
>   if(pred)
>     guard;
>   else
>     callTargetMethod;
> }
>
> Can the JVM optimize the guardWithTest version better? Does it handle
> primitive types better?
>
> -Arthur
Well, I agree with you. It should be possible for you to use your own
GuardWithTest implementation and it should compile efficiently.

However you will find that it is impossible for you to create a generic
GuardWithTest that works on any method since the combinatorial
explosion of possible method types cannot be anticipated practically
beforehand.

The factory creates the secret code that can adapt to any method type.

However with a single backwards compatible change to JSR292 it would
be possible for you to implement your own generic GuardWithTest!

And in, fact, as an example I have used exactly the kind of if statement
solution that you propose. You can read about it here:
http://blogs.oracle.com/ohrstrom/2009/05/pulling_a_machine_code_rabbit.html

//Fredrik




More information about the mlvm-dev mailing list