DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!
  • Legacy Code Refactoring: Tips, Steps, and Best Practices
  • Two Cool Java Frameworks You Probably Don’t Need
  • What Is Pydantic?

Trending

  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • How to Convert XLS to XLSX in Java
  1. DZone
  2. Coding
  3. Java
  4. When Short Methods Pay Off: JIT Inlining

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.

By 
Grzegorz Mirek user avatar
Grzegorz Mirek
·
Updated Jul. 26, 17 · Code Snippet
Likes (9)
Comment
Save
Tweet
Share
12.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Data Types Java compiler IT Snippet (programming) Java (programming language) optimization Overhead (computing) Attribute (computing)

Published at DZone with permission of Grzegorz Mirek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!
  • Legacy Code Refactoring: Tips, Steps, and Best Practices
  • Two Cool Java Frameworks You Probably Don’t Need
  • What Is Pydantic?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: