Over the holidays, I got a Polar OH1+ as a Christmas present. Its an optical heart rate monitor with similar tech to those found in smart watches (including my Garmin running watch), but much more accurate (at least for running) due to being smaller & lighter, as well as fitting against the fleshier upper arm: Like any modern gadget these days, it supports Bluetooth (specifically Bluetooth Low…
We’ve all probably seen Java’s String comparison function before. It compares strings by the first differing character, falling back to the length difference when they are identical up to the end of the shorter string: public int compareTo ( String anotherString ) { int len1 = value . length ; int len2 = anotherString . value . length ; int lim = Math . min ( len1 , len2 ); char v1 [] = value ;…
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 recent versions of OpenJDK on x86-64 it’ll be 24 bytes (…probably). Now some more experienced…
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 have a simple class Foo public class Foo { protected int x ; public Foo ( int x ) { this . x = x ; } public int function () {…
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 [] = { 5 , 4 , 3 }; int v = - 1 ; add_to_array ( arr , & v , 3 ); for ( int i = 0 ; i < 3 ; i ++ ) { printf ( "%d \n " , arr [ i ]); }…
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 tests like this: test %eax,0x16e71fa4(%rip) # 0x00007fce84071000 At first glance, this doesn’t seem to…
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 normal flow of the code. In this post I’m going to run through a fun little trick that the JVM uses to optimizes such cases.…
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 this way - while this requires a little work, its very doable. Here are a few different ways to do it. A Simple example I’ve…
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 this much rougher than it should be. I recently reimaged my VPS and recorded every step along the way, which I have relayed here.…