Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use method inlining in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use the method inline in Java". In the daily operation, I believe that many people have doubts about how to use the method inline in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use the method inline in Java". Next, please follow the editor to study!

1. What is method inlining?

For example, there is the following original code:

Static class B {int value; final int get () {return value;}} public void foo () {y = b.get (); / /... do stuff... Z = b.get (); sum = y + z;}

The first thing we need to do is to inline the method, which mainly has the following two purposes:

Remove the cost of method calls, such as finding method versions and establishing stack frames.

Establish a good foundation for other optimizations.

The inline code is as follows:

Public void foo () {y = b.value; / /... do stuff... Z = b.value; sum = y + z;}

In the follow-up, optimization operations such as redundant access elimination, replication propagation, useless code elimination and so on can be carried out.

two。 The importance of method inlining

Method inlining is the most important optimization means for compilers. Without inlining, most other optimizations cannot be carried out effectively. For example, the following example:

Public static void foo (Object obj) {if (obj! = null) {System.out.println ("do something");}} public static void testInline (String [] args) {Object obj = null; foo (obj);}

The testInline () method is full of useless code, but if you don't inline the method, you can't find any Dead Code, because if you look at it separately, the operations in both methods may make sense.

3. The difficulty of method inlining in Java

In JVM, only non-virtual methods, that is, private methods called with the invokespecial instruction, instance constructors, parent methods, and static methods called with the invokestatic instruction, are resolved at the compiler.

While other virtual methods are called by invokevirtual instructions, the polymorphic selection of the method receiver must be carried out when the method is called. For a virtual method, it is difficult for the compiler to determine which version of the method should be used when inlining statically, which makes it difficult for the method to inline.

Inheritance type relation analysis CHA

First, JVM introduces a technique called type inheritance relationship analysis CHA, which is used in loaded classes to determine whether an interface has more than one implementation, whether a class has a subclass, whether a subclass overrides a virtual method of the parent class, and so on.

When inlining, the compiler will take different actions according to different situations:

If it is a non-virtual method, then go inline directly.

If it is a virtual method, check with CHA to see if there are multiple target versions to choose from.

If there is only one version, it is directly inlined, which is called guardian inline. However, due to the dynamic connection of the Java program, we do not know when it will be loaded into a new type and change the conclusion of CHA, so we should keep a good escape door. if the new classes that lead to changes in inheritance relations are loaded in the subsequent execution of the program, then we must abandon the compiled code, return to the interpreted state for execution, or recompile.

If there are multiple versions to choose from, the immediate compiler uses inline caching to reduce the overhead of method calls. An inline cache is a cache established before the normal entry of the target method. When no method call occurs, the inline cache is empty. After the first call occurs, the cache records the version information of the method recipient and checks the version before each call.

If the receiver version of the method is the same for each call, it is called singleton inline cache, which is called through the cache and has only one more type judgment overhead than not inline. If the method receiver is inconsistent, it will degenerate into a hyperpolymorphic inline cache, and the overhead is equivalent to finding the virtual method table for method dispatch. When the cache misses, most JVM implementations degenerate into hyperpolymorphic inline caching, and some JVM choose to rewrite singleton inline caching, that is, update the cache to a new version. The advantage of this is that it may hit in the future, and the downside is that it may waste the cost of writing.

At this point, the study on "how to use inline methods in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report