I was looking at this Java performance panel by Gil, Todd, Mike and Tal -
http://www.infoq.com/presentations/panel-java-performance. One of the things that struck me was Gil talking about flyweights and how they are more inefficient than just plain object field accesses.
I use flyweights usually like this.
class FooFlyweight {
private static final int INT_FIELD_OFFSET = 0;
private static final int LONG_FIELD_OFFSET = INT_FIELD_OFFSET + INT_SIZE; // 4
private static final int TOTAL_SIZE_PER_FOO = LONG_FIELD_OFFSET + LONG_SIZE; // 12
private long basePtr;
private int index
private int currentObjectStartPointer;
private resetPtr(long ptr) {
basePtr = ptr;
}
private resetIndex(int index) {
this.index = index;
this. currentObjectStartPointer = basePtr + TOTAL_SIZE_PER_FOO * index;
}
int getInfField() {
return Unsafe.getInt(currentObjectStartPointer + INT_FIELD_OFFSET);
}
long getLongField() {
return Unsafe.getLong(currentObjectStartPointer + LONG_FIELD_OFFSET);
}
}
And I use it to iterate through a chunk of off-heap memory and treat this like an array of structs.
Why is it that getInfField() and getLongField() are less efficient than a simple object field access. Also what is the difference between this and the equivalent code where we have a an array of structs like Struct Foo { int32_t a; int64_t b;};?