The Black Magic of Java Method Dispatch
shipilev.net
shipilev.net
If this kind of "black magic" excites you, I highly recommend digging into both of these decades-old projects.
Terrific link, luu.
[1] http://www.cs.ucsb.edu/~urs/oocsb/self/papers/papers.html
mov 0xc(%r12,%r11,8),%eax
Interesting choice, won't this be a so-called "slow lea" on recent Intel hardware?I'm pretty sure the following replacement sequence for just that one instruction would be more expensive regardless:
shl 0x3, %r11 ; Multiply by 8
add %r12, %r11 ; Add (heap?) base address to r11
mov 0xc(%r11),%eax ; Indexed + displacement addressing mode
xor %r11, %r11 ; Make sure upper 32 bits are cleared for future r11 use
Last line (xor) is most likely not necessary, but might depend on how r11 is used in the future. If JVM assumes the upper bits to be always zero, it is needed.Also anonymous inner classes retain a pointer to the parent which can impact memory usage.
Additionally although not relevant to the code in question, the author's habit of using if/else vs. ternary (or if-only) can have performance and caching implications:
http://stackoverflow.com/questions/4706696/which-if-construc...
http://kennethxu.blogspot.com/2013/10/ifelse-statement-vs-te...
I fail to see why static methods should be better with respect to maintenance unfortunately the author lacks an explanation of this opinion. I would express the contrary opinion as you can exchange the implementation easier at selected spots.
If it's impure, run away.
But if you're concerned enough about performance to care about minute bytecode details (intended audience of the article), I think ease of testing is a lower priority.
If ternary and if-else statement don't perform same in both cases, I'd call it a bug in JVM. On C/C++ compilers I've tried, ternary has always compiled to exactly same as if-else-statement.
static int x[] = new int[1];
public static void main1(String[] args) {
x[1] = grow() ? 10 : 20; //array index out of bounds!
}
public static void main2(String[] args) {
if (grow()) {
x[1] = 10;
} else {
x[1] = 20;
}
//no exceptions thrown
}
private static boolean grow() {
x = new int[2];
return true;
}