Difference between revisions of "Code Size"

From Robowiki
Jump to navigation Jump to search
m (thanks mate - you are right)
(Remove empty section)
 
(2 intermediate revisions by one other user not shown)
Line 90: Line 90:
  
 
|-
 
|-
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Declare variables on its first use
+
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Declare variables on their first use
 
|-
 
|-
 
|style="text-align:justify"|
 
|style="text-align:justify"|
Line 105: Line 105:
  
 
|-
 
|-
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | A magic of registers
+
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | The magic of registers
 
|-
 
|-
 
|style="text-align:justify"|
 
|style="text-align:justify"|
Every operation to anything must be done from register. Java has 4 registers to hold variable. If the method is not static, the first one is <code>this</code> reference. The remaining registers will be use for each parameter and variable inside method. Every type of variable use 1 register, except double 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 operation, which costs codesize. This mean you should define most-used variables before any other variables.
+
Every operation must be done from registers. Java has 4 registers to hold variables. If the method is not static, the first one is <code>this</code> 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:
 
Do this:
Line 115: Line 115:
 
   int int_variable_if_you_want;  
 
   int int_variable_if_you_want;  
 
   double bearing =   
 
   double bearing =   
     e.getBearingRadians() + getHadingRadians();
+
     e.getBearingRadians() + getHeadingRadians();
 
   double distance = e.getDistance();
 
   double distance = e.getDistance();
 
   // ...
 
   // ...
Line 126: Line 126:
 
   int int_variable_if_you_want;  
 
   int int_variable_if_you_want;  
 
   double bearing =  
 
   double bearing =  
     e.getBearingRadians() + getHadingRadians();
+
     e.getBearingRadians() + getHeadingRadians();
 
   // ...
 
   // ...
 
}
 
}
Line 135: Line 135:
 
|-
 
|-
 
|style="text-align:justify"|
 
|style="text-align:justify"|
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 bound" check.
+
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:
 
Do this:
Line 151: Line 151:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Don't do this: (8 byte more)
+
Don't do this: (8 bytes more)
 
<syntaxhighlight>
 
<syntaxhighlight>
 
public void onScannedRobot(ScannedRobotEvent e) {
 
public void onScannedRobot(ScannedRobotEvent e) {
Line 160: Line 160:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
|-
 +
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Use onStatus to turn [[Radar]]
 +
|-
 +
|style="text-align:justify"|
 +
Do this:
 +
<syntaxhighlight>
 +
public void onStatus( StatusEvent e) {
 +
    setTurnRadarRightRadians(1);
 +
}
 +
</syntaxhighlight>
 +
 +
Don't do this (1 byte more):
 +
<syntaxhighlight>
 +
public void run() {
 +
    do {
 +
        turnRadarRightRadians(1);
 +
    } while (true);
 +
}
 +
</syntaxhighlight>
  
 
|}
 
|}
Line 225: Line 244:
  
 
== See Also ==
 
== See Also ==
* check the code size [[Talk:Code_Size|Discussion]] page and ask your question
+
* Check the code size [[Talk:Code_Size|Discussion]] page and ask your question
* many of the [[NanoBots]] and [[MicroBots]] are open source and can be used to learn more about code size reducing
+
* Many [[NanoBots]] and [[MicroBots]] are open source and can be studied to learn more about code size reduction.
 
* [[CodeSize/Old discussion]]
 
* [[CodeSize/Old discussion]]
 
== Credit ==
 
<!-- TODO: some of the veterans maybe -->
 
  
 
[[Category:Terminology|Code Size]]
 
[[Category:Terminology|Code Size]]

Latest revision as of 03:02, 13 August 2017

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