Code Size

From Robowiki
(Redirected from Byte)
Jump to navigation Jump to search
Code Size redirects here, for other uses see Code Size (disambiguation)
Byte redirects here, for other uses see Byte (disambiguation)

General

It represents the amount of executing code in a .class file or .jar. Code size is important to you if you want to make Robots for the smaller weight classes. It can be challenging, educational, frustrating or all at once to reduce the code to the targeted level. It is not unusual that you spend hours just to find one measly byte or have to reshuffle your ideas because they won't fit the restricted weight class. This page gives you an overview of how to reduce your code, the utilities to measure the size, the weight classes and some common tricks.

How to measure

The Robocode client is one option to measure the size of your robot. You can do this if you "Package robot for upload" from the "Robots" menu. If you packed your robot successfully the client will show you the size with a little message window.

Another option to make the code size measuring a little more handy, is to use the Code Size Utility straight within your eclipse configuration or from the console.


Weight Classes

Weight classes apply for 1v1 and for melee rumble the same.

Class Code Size
General no restrictions
MiniBots 1499 bytes or less
MicroBots 749 bytes or less
NanoBots 249 bytes or less
Twin Duel 1999 bytes or less ( this means two instances of one bot 1999 bytes, or instance of bot one + instance of bot two = 1999 bytes)
Teams no restrictions

Common Tricks

Code Size Cheat sheet

Use Exceptions

Do this:

 LinkedList list;
  try{
   someFunction
     (list.getFirst());
  } catch(Exception ex){}

Don't do this:

 LinkedList list;
  if(list.size() != 0){
   someFunction
     (list.getFirst());
  }


Don't use if ... else ...

Do this:

 int i = 1;
  if (j > 2) {
   i = -1;
  }

Don't do this:

 int i;
  if (j > 2) {
   i = -1;
  } else {
   i = 1;
  }
Declare variables on their first use

Do this:

  double a;
  double t = getEnergy() * (a = e.getBearing());

Don't do this:

 double a = e.getBearing();
  double t = a * getEnergy();
The magic of registers

Every operation must be done from registers. Java has 4 registers to hold variables. If the method is not static, the first one is a this reference. The remaining registers will then be used for each parameter and variable inside the method. Every type of variable uses 1 register, except doubles, which use 2. When 3 registers are already used by other variable, a double value will then be assigned an extra register (and the codesize is as if there was only 1 used). If you do any operation on any variable that is not on the register, Java will need to move that variable to the register before doing any of your operations, which increases codesize. This means you should define most-used variables before any other variables.

Do this:

public void onScannedRobot(ScannedRobotEvent e) {
  int int_variable_if_you_want; 
  double bearing =  
    e.getBearingRadians() + getHeadingRadians();
  double distance = e.getDistance();
  // ...
}

Don't do this:

public void onScannedRobot(ScannedRobotEvent e) {
  double distance = e.getDistance();
  int int_variable_if_you_want; 
  double bearing = 
    e.getBearingRadians() + getHeadingRadians();
  // ...
}
Loop with while

You should always end up with less code size if you switch your for( ...) loops to while loops. This applies to Lists as well to Arrays, just put an try..catch.. around the while to save the null, zero or "array out of bounds" check.

Do this:

public void onScannedRobot(ScannedRobotEvent e) {
    try {
        Iterator<your_class> iter = yourList.iterator();
	while(true) {
            your_class obj = iter.next();
           ......   // your code
        }	
    } catch (Exception e) {}

}

Don't do this: (8 bytes more)

public void onScannedRobot(ScannedRobotEvent e) {
    for(your_class obj : yourList) {
       ...... // your code
    }
}
Use onStatus to turn Radar

Do this:

public void onStatus( StatusEvent e) {
    setTurnRadarRightRadians(1);
}

Don't do this (1 byte more):

public void run() {
    do {
        turnRadarRightRadians(1);
    } while (true);
}
Cost
Operation Cost in bytes
creating a class 5
creating a interface free
implementing a interface 1
1st constructor free
creating a method free
returning from a method 1
calling a method 3
storing a local (non-register) variable 1
loading a local (non-register) variable 1
storing a static variable 2
loading a static variable 2
loading integer literals -1 to 5 1
loading integer literals -128 to 127 2
loading integer literals -32768 to 32767 3
loading other integer literals 2
loading double literals 0, 1 1
loading double literals -1, 2, 3, 4, 5 2
loading other double literals 3
loading string literals 2
declaring a variable free
casting 1
promotion 1
arithmetic 1
++ and -- (register or not) 1

See Also