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

What is the use of the built-in function Intrinsics in JVM

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

Share

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

This article mainly introduces "what is the use of JVM built-in function Intrinsics". In daily operation, I believe many people have doubts about the use of JVM built-in function Intrinsics. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "what is the use of JVM built-in function Intrinsics?" Next, please follow the editor to study!

1. What is built-in?

Built-in functions are specially handled by the compiler or interpreter of our programming language. More specifically, this is a special case where a compiler or interpreter can replace a function with an alternative implementation for a variety of reasons.

Programming languages usually deal with this problem by understanding that a particular method call is special, and whenever we call this method, the results are different. In this way, our code looks no different from normal code, but the implementation of the programming language can intervene in special cases to provide additional benefits.

How it works varies depending on programming language, operating system, and hardware. However, since these are handled for us, we usually do not need to know any of these details.

Nature can bring all kinds of benefits. Replacing specific algorithms with native code can make them perform better, and even take advantage of specific features of the operating system or underlying hardware.

Built-in functions on 2.JVM

JVM implements the built-in function by replacing the exact method call on the exact class with an alternative version. JVM handles this problem itself, so it only applies to core classes and specific architectures. It also allows only certain methods to be exchanged, not the entire class.

The specific way it works varies from JVM to JVM. This includes not only different versions of JVM, such as Java 8 and Java 11. This also includes different JVM goals-- such as Linux and Windows--, especially JVM vendors-- Oracle and IBM. In some cases, some command line flags passed to JVM may affect them.

This diversity means that it is not possible to determine which methods will be replaced by built-in methods and which will not be based solely on the application. This will vary depending on the JVM on which the application is running. In some cases, however, this can lead to surprising results-including significant performance benefits gained by simply changing the JVM used.

3. Performance benefit

Intrinsic is often used to implement a more efficient version of the same code, for example, by taking advantage of the implementation details of a running OS or CPU. Sometimes this is because it can use a more efficient implementation, and sometimes it can use hardware-specific functionality.

For example, HotSpot JDK has an inherent implementation of java.lang.Math for many of the methods in java. Depending on the specific JVM, they may be implemented using CPU instructions to complete the required precise calculations.

A simple test will prove it. For example, take java.lang.Math.sqrt () as an example.

We can write a test:

For (int a = 0; a < 1000000; + + a) {double result = Math.sqrt (a);}

This test will perform 100000 square root operations, which will require approximately 123ms. However, if we replace this code with a copy of the Math implementation. Math.sqrt ():

Double result = StrictMath.sqrt (a)

This code does the same, but the execution time is 166ms. This is achieved by copying, rather than allowing JVM to replace it with a build, which increases by 35%.

4. Impossible realization

In other cases, intrinsic is used in situations where code cannot be implemented in Java. These are usually reserved for very low-level situations.

For example, let's look at the onSpinWait () method in the java. Lang. Thread thread class. This method indicates that this thread is not currently performing any work and that CPU time can be allocated to another thread. To achieve this, it needs to work at the lowest possible level.

The HotSpot JDK for x86 architecture does this directly on CPU using PAUSE opcodes. The only other way to do this is to use a JNI call to native code, and the overhead involved first offsets the benefits of the call.

5. Identify the Intrinsics language in Java

Unfortunately, there is no definite way to identify methods that may be replaced by internal versions. This is because different JVM, or even the same JVM on different platforms, implement this for different methods.

However, when using HotSpotJVM from Java 9, all methods that might be replaced use the @ HotSpotInTrensicAndidate annotation. Adding this comment does not automatically cause the method to be replaced. In fact, this happens in the underlying JVM. Instead, JVM developers know that these methods are special and need to be used with care.

If other JVM are identified, they may deal with the problem in different ways. This includes Hotspot JVM in Java8 or earlier.

At this point, the study on "what is the use of JVM built-in function Intrinsics" 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