Allocation on the JVM: Down the rabbit hole

Lets say we have a simple function that allocates an object: public static long demo(long a, long b) { return new Long(a+b).longValue(); } How much memory does this function allocate per call? The number may vary based on the version of your JVM, the hardware, and various settings, but on...

Virtually free - JVM callsite optimization by example

Recently, this post by Swift creator Chris Lattner has been making the rounds, which does a nice job of detailing method dispatch in various languages. I thought I would add some color on how the Hotspot JIT can safely turn a virtual call into a static one. Let say we...

Fun C Micro-optimizations - restrict

Lets say we wanted to add a value to an every element of an array. Here’s a slightly contrived example: void add_to_array(int* arr, int* v, int len) { for(int i = 0; i < len; i++) { arr[i] += *v; } } int main(int argc, char** argv) { int arr[]...

More JVM Signal tricks - Thread control via mprotect

In my last post, I mentioned the JVM uses intentionally uses SIGSEGVS in other interesting ways. In this post I’ll give an overview of one of those other ways, synchronization for GC pauses. If you ever look at generated assembly from Hotspot, you will find methods ending with very odd-looking...

SIGSEGV as control flow - How the JVM optimizes your null checks

If you’ve ever written Java, you’ve almost certainly written null checks. For better or for worse, if (variable == null) shows up everwhere - Hadoop alone has over 6000 of them 1. In many cases, these are purely defensive - a null isn’t really expected to be passed in the...

Reading assembly from the JVM

The second important step on the way to Hotspot JIT understanding (after building a debug JVM) is to look at generated assembly - the code that java actually runs on your behalf. Unfortunately, few developers do this or even know that you can do this. It doesn’t have to be...

Building a debug JVM

If you’re interested in learning more about the Hotspot’s internals, theres no way around it - you have to read the code. If you really want to learn, you’ll want to build and run a debug build. This isn’t too bad to do, but a dearth of online documentation makes...