Array Initialization Optimization

Property settings

This optimization is turned ON by default.
In the property file,
OPT.ARRAY.INIT=ON turns on this optimization
OPT.ARRAY.INIT=OFF turns off this optimization

The Java compiler generates space inefficient code for array initialisation. Typically, it takes about 3-6 bytes to populate each element in an array.

class LevelData {
    // Some comments here
    static final int SNOW = 1;
    static final int HILL = 2;
    static final int POND = 3;

    static final int[][] MAP = { 
        {SNOW, SNOW, SNOW, HILL, HILL, HILL },
        {SNOW, POND, POND, SNOW, SNOW, HILL },
        {SNOW, POND, POND, HILL, HILL, HILL },
        ....
        {SNOW, POND, HILL, HILL, POND, HILL },
        {SNOW, SNOW, SNOW, HILL, HILL, HILL }
    };

    int[] intArray1 = {1, 2, 4, -1, 29, 100, 2, 9};
    int[][] enemyPositions;

    // Constructor
    void LevelData() {
        ....
        // Calls initEnemyPosition() to set up initial position of enemies
        initEnemyPositions();
        ....
    }
        
    // Method to set up initial position of enemy
    void initEnemyPositions() {
        enemyPositions = new int[][] { {
            4,3}, {5,4}, {3,1}, {5,2}, {10,1}, {8,3}, {10,0}, {1,1} };
    }
    ...
}

mBooster automatically optimizes array initialization that occurs within class initializers and constructors, and methods they call, where it is determined that it will be space efficient. mBooster optimized array initialization is

  • much faster than loading an external data file
  • typically more space efficient than loading an external file
  • has no impact on performance other than class initialization, and instantiation
  • works for multidimensional int, short, byte arrays

In the example above, mBooster will automatically optimize the initialization of MAP, intArray1, and enemyPositions.

Tips
  • Initialize arrays in Java source code, rather than loading from an external file
  • Keep the value range in an array as small as possible for best optimization
 

Previous pageContentsNext page