Re: [Jamvm-general] Can JamVM optimize away static branches?
Brought to you by:
rlougher
From: Robert L. <rob...@gm...> - 2012-02-24 20:11:22
|
On 23 February 2012 15:12, Xerxes Rånby <xe...@za...> wrote: > I have attached a slightly more interesting testcase. > This time the logic gets passed to JamVM. > > Can JamVM optimize away these static branches when the public static final are set at runtime? > Short answer, no. Long answer, JamVMs JIT is pretty simplistic. It converts bytecode to native machine code but it doesn't really do any optimising of the bytecode. It's been my intention for several years to add some easy optimisations at the bytecode level, but other things always gets in the way. Optimisation of runtime initialised finals would also be pretty low on the list, as there's much more lower hanging fruit. Thanks, Rob. > Cheers > Xerxes > > xranby@xranby-ESPRIMO-P7935:~$ cat foo.java > public class foo { > public static boolean isDebug(){ > String value = System.getProperty("debug"); > if(value!=null) > return true; > return false; > } > > public static final boolean DEBUG=isDebug(); > > public static void main(String[] args) { > foo f = new foo(); > > if (f.DEBUG) { > System.out.print("I have a message to the user, "); > } > > System.out.print("hello"); > > if (f.DEBUG) { > System.out.print(", i love you all,"); > } > > System.out.println(" world"); > } > } > > > xranby@xranby-ESPRIMO-P7935:~$ java foo > hello world > xranby@xranby-ESPRIMO-P7935:~$ java -Ddebug foo > I have a message to the user, hello, i love you all, world > > xranby@xranby-ESPRIMO-P7935:~$ javap -c foo > Compiled from "foo.java" > public class foo extends java.lang.Object{ > public static final boolean DEBUG; > > public foo(); > Code: > 0: aload_0 > 1: invokespecial #1; //Method java/lang/Object."<init>":()V > 4: return > > public static boolean isDebug(); > Code: > 0: ldc #2; //String debug > 2: invokestatic #3; //Method java/lang/System.getProperty:(Ljava/lang/String;)Ljava/lang/String; > 5: astore_0 > 6: aload_0 > 7: ifnull 12 > 10: iconst_1 > 11: ireturn > 12: iconst_0 > 13: ireturn > > public static void main(java.lang.String[]); > Code: > 0: new #4; //class foo > 3: dup > 4: invokespecial #5; //Method "<init>":()V > 7: astore_1 > 8: aload_1 > 9: pop > 10: getstatic #6; //Field DEBUG:Z > 13: ifeq 24 > 16: getstatic #7; //Field java/lang/System.out:Ljava/io/PrintStream; > 19: ldc #8; //String I have a message to the user, > 21: invokevirtual #9; //Method java/io/PrintStream.print:(Ljava/lang/String;)V > 24: getstatic #7; //Field java/lang/System.out:Ljava/io/PrintStream; > 27: ldc #10; //String hello > 29: invokevirtual #9; //Method java/io/PrintStream.print:(Ljava/lang/String;)V > 32: aload_1 > 33: pop > 34: getstatic #6; //Field DEBUG:Z > 37: ifeq 48 > 40: getstatic #7; //Field java/lang/System.out:Ljava/io/PrintStream; > 43: ldc #11; //String , i love you all, > 45: invokevirtual #9; //Method java/io/PrintStream.print:(Ljava/lang/String;)V > 48: getstatic #7; //Field java/lang/System.out:Ljava/io/PrintStream; > 51: ldc #12; //String world > 53: invokevirtual #13; //Method java/io/PrintStream.println:(Ljava/lang/String;)V > 56: return > > static {}; > Code: > 0: invokestatic #14; //Method isDebug:()Z > 3: putstatic #6; //Field DEBUG:Z > 6: return > > } > > |