Back from Toronto


Angeline & I went up to the not-quite-frozen north last week to visit her fam for Christmas. I shall account for time spent there in a future entry a few days from now, but not right now because the pictures I want to post are on my camera that I left there.

Instead, I’ll continue to post about boring technical subjects. Today: one example of sucky Java performance (a topic which hardly anyone in the Java community cares about, apparently). I had a disagreement of opinion with a co-worker recently, as I was coming across code written by a 3rd person that went like:


public void getBar() { return this.bar; }
public void doSomething() {
getBar().frob();
getBar().xyzzy();
getBar().baz();
}

I remarked that this sort of programming is not only extra typing, but performs poorly because inside doSomething we are now making six method calls instead of only the three we need. My co-worker suggested the Java compiler was smart enough to inline getBar(). I was skeptical for two reasons: first that getBar is public, so in order to allow subclasses to override it, it has to be in the Java-equivalent of a vtable, like every other Java method. Second, I’ve looked at the output of javac and it is really quite dumb.

So, I decided to make it easy on the compiler in a test program to see (excerpt):


private static final String blah="blah";
private final String getBlah() { return blah; }
public void doit() { System.out.println(getBlah()); }

There’s no way for a subclass to override getBlah or the value it returns; however, the code generated is:


public void doit();
Code:
0:	getstatic	#3; //Field java/lang/System.out:Ljava/io/PrintStream;
3:	aload_0
4:	invokespecial	#4; //Method getBlah:()Ljava/lang/String;
7:	invokevirtual	#5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
10:	return

Obviously, the final keyword does nothing. Now, I don’t know much about how the JVM/JIT will further optimize this, but this can’t be more efficient than a simple load of the variable onto the stack. We have to store this on the stack for one thing, and the JVM needs to store a return address somewhere. A small effect, sure, but why throw away performance?

Maybe I’ll be evil and start asking why Java should have the ‘virtual’ keyword in interviews.

Arduino


Arduino
Originally uploaded by bluesterror.

I’ve had a few parts setting around a while for an upcoming electronics project, but it hasn’t gotten far off the ground because I was too lazy to spec out the control circuitry. I wanted it to be microcontroller based, but realizing that serial and parallel ports are ever-dwindling, I also wanted to make sure that I purchased/built a programmer that would still work in a few years’ time. Enter the Arduino, a perfect little development board based around the Atmel ATmega8; at only $30 for the board and chip, it’s a great deal. The chip comes pre-programmed with a bootloader that you can use over USB to upload your programs.

Programming the microcontroller is a snap. The Linux toolchain features a port of gcc and a small libc. There’s also watered down dialect of C and an IDE that noobs can use, but I just skipped to building with the arduino libraries and avr-gcc directly. Within 15 minutes of installing the necessary packages, I had my first program uploaded and blinking an LED. Pictured above is the second try, a simple RGB pixel consisting of 3 LEDs which fade among themselves (the effect is less than perfect without a good diffuser and smaller LEDs).

Here’s the code for the fade:

#include <WProgram.h>

int rpin = 9;
int gpin = 10;
int bpin = 11;

static int cols[3], i;

void setup()
{
pinMode(rpin, OUTPUT);
pinMode(gpin, OUTPUT);
pinMode(bpin, OUTPUT);
cols[0] = 255;
cols[1] = 0;
cols[2] = 0;
}

void next_color()
{
int before = i-1;

if (before < 0)
before = 2;

if (cols[i] < 255)
cols[i]++;
else if (cols[before] > 0)
cols[before]--;
else
i = (i+1) % 3;
}

void loop()
{
next_color();
analogWrite(rpin, cols[0]);
analogWrite(gpin, cols[1]);
analogWrite(bpin, cols[2]);
delay(10);
}

Aquarium

While back in Atlanta two weeks ago, Angeline and I toured the one year old Georgia Aquarium, billed as the World’s Largest, perfect for a land-locked city, I hunted around the net the night before for some audio podcasts and found a couple, but the free ones that are on the official aquarium website are definitely the best. We followed the audio tour, looked around at the many fish, got hungry, then ate burgers at the Vortex, and I brought back lousy pictures. Click below for the goods.