Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

When Short Methods Pay Off: JIT Inlining

DZone's Guide to

When Short Methods Pay Off: JIT Inlining

Want a quick guide to see whether your methods are ready for inlining? Here are a couple of easy rules, with a snippet example, to help you out.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Among all Just-In-Time Java compiler optimizations, inlining methods are a powerful approach. Usually, when we write code following good object-oriented practices, we end up having lots of small objects with well-encapsulated attributes – most of them accessible via getters. But there is an overhead of making additional calls and increasing the callstack. Fortunately, with JIT inlining, we can follow good practices and benefit from performant code.

A method is eligible for inlining if:

  • It’s small – the bytecode size is smaller than 35 bytes (can be overridden by the -XX:MaxInlineSize=X flag).
  • It’s called frequently (it’s hot) and it’s smaller than 325 bytes (can be overridden by the -XX:MaxFreqInlineSize=X flag).

Consider the following snippet:

public class JitInlining {
    public static void main(String args[]) {
        Position position = new Position(25);
        for (int i = 0; i < 1000000; ++i) {
            int x = position.getX();
            position.setX(x + 1);
        }
        System.out.println(position.getX());
    }
}

class Position {
    private int x;

    public Position(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }
}


When inlining is applied, the position.setX(x + 1) operation will look like this:

position.x = x + 1;


To make sure that a particular piece of code is inlined, you can use the -XX:+PrintInlining flag (together with -XX:+PrintCompilation and -XX:+UnlockDiagnosticVMOptions):

java -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining com.performantcode.JitInlining



The result will be something like that:

111   19 %     4       com.performantcode.JitInlining::main @ 12 (47 bytes)

                      @ 19   com.performantcode.Position::getX (5 bytes)   accessor

                      @ 27   com.performantcode.Position::setX (6 bytes)   inline (hot)



Please notice that inline (hot) is displayed next to setX.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
compiler ,jit optimization ,java ,inline methods

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}