In a recent post I took a look at how Java 8 and Scala implemented Lambda expressions. As we know Java 8 is not only introducing improvements to the javac compiler, It’s also introducing a new one altogether – Nashorn.





This new engine is meant to replace Java’s existing JavaScript interpreter Rhino. This is supposed to bring the JVM to the forefront when it comes to executing JavaScript at speed, right there with V8s of the world (hopefully we’ll finally get past that car to carpet thing :)) So, I thought it would be a good time to bring Nashorn to the mix as well by taking a look under the hood, and see how it compiles Lambda expressions (especially compared to Java and Scala).

The lambda expression we’ll be looking at is similar to the one we tested with Java and Scala.

Here’s the code –

Seems innocent enough you’d think. But just wait and see…

Getting to the bytecode

Our first challenge is to get to the actual bytecode which the JVM sees. Unlike Java and Scala whose compilers are persistent (i.e. generate .class / jar files to disk), Nashorn compiles everything in memory and passes the bytecode to the JVM directly. Luckily enough we’ve got Java agents to come to our rescue. I wrote a simple Java agent to capture and persist the resulting bytecode. From there on out it’s a simple javap to print the code.

If you remember, I was pretty happy to see how the new Java 8 compiler uses the invokeDynamic instruction introduced in Java 7 to link to the Lambda function code. Well, with Nashorn they really went to the races with it. Everything now is completely based on it. Take a look below.

Reading the bytecode

invokeDynamic. Just so we’re all on the same page, the invokeDynamic instruction was added in Java 7 to allow folks writing their own dynamic languages to decide at runtime how to link code.

For static languages like Java and Scala, the compiler decides at compile time which method would be invoked (with some help from the JVM runtime for polymorphism). The runtime linking is done via standard ClassLoaders to lookup the class. Even things like method overload resolution are done at compile time.

Dynamic vs. static linkage. Unfortunately, for languages which are more dynamic in nature (and JS is a good example) static resolution may not be possible. When we say obj.foo() in Java, either the class of obj has a foo() method or it doesn’t. In a language like JS that will depend on the actual object referenced by obj at runtime – a nightmare scenario for a static compiler. A compile-time approach to linking in this case just doesn’t work. But invokeDynamic does.

InvokeDynamic enables deferring of linkage back to the writers of the language at run-time, so they can guide the JVM as to which method they would like to call, based on their own language semantics. This is a win-win situation. The JVM gets an actual method to link to, optimize and execute against, and the language makers control its resolution. Dynamic linking is something we’ve had to work hard to support in OverOps.

How Nashorn links

Nashorn really makes effective use of this. Let’s look at our example to understand how this works. Here’s the first invokeDynamic instruction in the Lambda code, that’s used to retrieve the value of the JS Array class –

Nashorn is asking the JVM to pass it this string at runtime, and in exchange it will return a handle to a method which accepts an Object and returns one. As long as the JVM gets a handle to such a method, it can link.

The method responsible for returning this handle (also known as a bootstrap method) is specified in a special section in the .class file which holds a list of available bootstrap methods. The 0 value you see is the index within that table of the method which the JVM will invoke to get the method handle to which it will link.

The Nashorn folks did a very cool thing in my opinion, and instead of writing their own library for resolving and linking code, they went ahead and integrated dynalink, an open source project aimed at helping dynamic languages link code based on a unified platform. That’s also why you see that “dyn:” prefix at the beginning of each string.

The actual flow

Now that we’ve gotten a hang of the approach used by Nashorn, let’s look at the actual flow. I’ve removed some of the instructions for brevity. The full code can be found here.

1. This first group of instructions loads the array map function into the script.

2. Next we allocate the names array

3. Find and load the Lambda function

4. Call map with names and the Lambda, and place the result in a

5. Find the print function and call it on a

The lambda function itself is compiled and placed in the same class as the script as a private function. This is very similar to what we’ve with Java 8 lambdas. The code itself is straightforward. We load the string, find its length function and call it.

Bonus round – the final bytecode

The code we’ve been dealing with so far isn’t really what the JVM will execute at run-time. Remember, each invokeDynamic instruction will be resolved to a physical bytecode method which the JVM will then compile into machine code and execute.

To see the actual bytecode which the JVM runs I used a simple trick. I wrapped the call to length() with a simple Java method call in my class. This enabled me to place a breakpoint and see the final call stack which the JVM executes to get into the Lambda.

Here’s the code –

Here’s wrap –

Now let’s play a game. How many frames will be in that stack? Think about it for a second. If you guessed less < 100 – you owe me a beer. The full call stack can be found here.

The reason why that is so is very interesting as well, and that’s a story for whole new post coming down the road.

Comments, questions? Hit me below.

Compiling Lambda Expressions: Scala vs. Java 8 – read more