Performance and scalability: Java compiler optimization

  1. Java compiler optimization (1 messages)

    I have a couple of questions regarding how the Java compiler optimizes Java code. I'll try to put each in a useful context, albeit your view of how useful the code is may vary. The first has to do with null object creation, in this case used with the toArray method for Collections. With the advent of generics in Java 5, we are given the collectionObject.toArray(T[] a) syntax, which returns an array of type T (and inserts it into a if it will fit). This is preferable (in my mind) to the older collectionObject.toArray() method, since the older method returns an array of type Object and thus must be coerced into the correct type, even though the Collection can only hold objects of its specified type. So, my question is: does the JVM incur any extra cycles executing the compiled version of String [] stringArray = stringCollection.toArray(new String[0]); vs. String [] stringArray = (String []) stringCollection.toArray(); My assumption is that the object creation is optimized away. I make this assumption in part based upon the JavaDoc from Sun regarding toArray: Note that toArray(new Object[0]) is identical in function to toArray(). It does say identical in function, not generates identical Java byte code or the like, hence the question. My second question involves optimizing decisions based upon static final constants. Does the Java complier calculate the truthiness of an if statement if there can be only one possible result due to a static final variable? What about a switch/case statement? For example... public class OptimizeConditionals { public static enum debugLevels {DEBUG_ALL, DEBUG_SOME, DEBUG_LITTLE, DEBUG_NONE}; private static final int tollerance = 17; private static final boolean debugging = true; private static final debugLevels currentDebugLevel = debugLevels.DEBUG_SOME; public static void main(String[] args) { if (tollerance < 10) System.out.println("Try it yourself, lazy person"); if (debugging) { switch (currentDebugLevel) { case DEBUG_ALL: System.err.println("Too hot"); break; case DEBUG_SOME: System.err.println("Just right"); break; case DEBUG_LITTLE: System.err.println("Too cold"); break; case DEBUG_NONE: break; } } } } </b> Does the compiler bother generating the code for the first if and the associated System.out.println call, since it can easily be determined not to execute at compile time? What about the (slightly) more complicated switch/case statement embedded in the second if? Does the compiler throw away all of the unnecessary code and just generate code for the specific System.err.println statement that will be executed?
  2. Re: Java compiler optimization[ Go to top ]

    Answer to first Question: Collection#toArray() method has fixed time run, and It might be slow compared to Collection#toArray(T[] a) because latter may not wait for GC-pause based on allocation of 'a'. As for if condition there has to be instruction and resulting cycles in case of toArray(T[] a) method. Also, if you look at the source of ArrayList implementation of toArray(T[] a) you'll notice two if conditions followed by assignment, that calls for few more cycles. 2nd Q: Modern *Dynamic* compilers can prune dead code paths through intelligent inspection algorithms in theory, Although I'm not sure if any of the existing VMs do that. Dynamic compilers are very smart, have a look at some of them http://www.program-transformation.org/Transform/JavaDynamicCompilers Nirav Thaker http://blog.nirav.name