Eliminate common subexpressions Eliminate unnecessary assignments Rename classes, fields, and methods Reorder or change bytecodes

- 67 - Both this technique and the next one are actually good coding practices.

3.4.2.14 Eliminate common subexpressions

Eliminating common subexpressions is similar to code motion. In this case, though, the compiler identifies an expression that is common to more than one statement and does not need to be calculated more than once. The following example uses the same calculation twice to map two pairs of variables: z1 = x Maths.absy + x; z2 = x Maths.absy + y; After a compiler has analyzed this code to eliminate the common subexpression, the code becomes: int t1 = x Maths.absy; z1 = t1 + x; z2 = t1 + y;

3.4.2.15 Eliminate unnecessary assignments

An optimizing compiler should eliminate any unnecessary assignments. The following example is very simplistic: int x = 1; x = 2; This should obviously be converted into one statement: int x = 2; Although you wont often see this type of example, it is not unusual for chained constructor s to repeatedly assign to an instance variable in essentially the same way. An optimizing compiler should eliminate all extra unnecessary assignments.

3.4.2.16 Rename classes, fields, and methods

Some compilers rename classes, fields, and methods for various reasons, such as for obfuscating the code making the code difficult to understand if it were decompiled. Renaming especially to one- character names [6] can make everything compiled much smaller, significantly reducing classloading times and network download times. [6] For example, the DashO optimizer renames everything possible to one-character names.

3.4.2.17 Reorder or change bytecodes

An optimizing compiler can reorder or change bytecode instructions to make methods faster. Normally, this reduces the number of instructions, but sometimes making an activity faster requires increasing the number of instructions. An example is where a switch statement is used with a list of unordered, nearly consecutive values for case statements. An optimizing compiler can reorder the case statements so that the values are in order, insert extra case s to make the values fully consecutive, and then use a faster switch bytecode to execute the switch statement. The optimization for switch statements is extensively covered in Chapter 7 . - 68 -

3.4.2.18 Generate information to help a VM