Hotspot VM内联功能是否可以根据需要运行? [英] Will the Hotspot VM inline functions as necessary?

查看:114
本文介绍了Hotspot VM内联功能是否可以根据需要运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些C ++代码转换为Java,我想知道如何处理内联函数.我是否可以假定功能将由VM内联(必要时),而不必为此担心呢?我如何进行配置以观察此行为?假设有一个主要的外部函数,并且我在它周围抛出了一个for循环并引起了一百万次调用.随着VM内联的越来越多,我是否应该期望看到改进?

I am converting some C++ code to Java and I was wondering what I can do about the inlined functions. Can I assume that functions will be inlined by the VM (as an when necessary) and just not worry about this? How do I profile to observe this behaviour? Suppose there is a main outer function, and I throw a for loop around it and cause a million invocations. Should I expect to see an improvements as the VM inlines more and more?

推荐答案

是的,Java执行内联方法调用.内联由JIT编译器执行,因此您不会通过检查字节码文件来看到它.

Yes Java does inline method calls. The inlining is performed by the JIT compiler, so you won't see it by examining the bytecode files.

给定方法调用是否真正发生内联取决于该方法主体的大小以及该调用是否可内联. (如果方法调用涉及分派...在JVM具有一系列旨在消除不必要分派的全局优化之后...则无法内联.)

Whether inlining actually occurs for a given method call will depend on the size of the method body, and whether the call is inlineable. (If a method call involves dispatching ... after the JVM has a bunch of global optimization designed to remove unnecessary dispatching ... then it cannot be inlined.)

同样的情况也适用于带有外部main函数的示例.这取决于方法主体的大小.另一方面,如果该方法花费大量时间执行,则优化的相对重要性也会相应降低.

The same applies to your example with your outer main function. It depends on how big the method body is. On the other hand, if the method takes a significant time to execute, then the relative importance of the optimization decreases correspondingly.

我的建议是,在此阶段不要担心这样的事情.只需清楚,简单地编写代码,然后让JIT编译器处理优化问题.当您的应用程序运行时,您可以对其进行概要分析,并查看代码中是否有值得手动优化的热点".

My advice is to not worry about things like this at this stage. Just write the code clearly and simply, and let the JIT compiler deal with the problem of optimizing. When your application is working, you can profile it and see if there are any "hot spots" in the code that are worthwhile optimizing by hand.

但是我应该能够在类似Visual VM的环境中看到它,对不对?我的意思是最初没有内联,然后有更多的内联,所以外部方法的平均时间略有减少.

But I should be able to see this in something like Visual VM right? I mean initially no inlining, then more and more stuff is inlined so the average time for the outer method is slightly reduced.

它可能是可观察的,而可能不是,这取决于相对于执行方法主体进行调用所花费的时间. (分析通常依赖于对程序计数器进行采样.如果给定代码区域的采样数太少,则由于其他原因,报告的时间可能不准确.)

It may be observable and it may not, depending on the amount time spent in making the calls relative to executing the method bodies. (Profiling often relies on sampling the program counter. The reported times may be inaccurate if the number of samples for a given region of code is too small ... and for other reasons.)

这还取决于您使用的JVM.并非所有的JVM都会重新优化他们之前优化过的代码.

It also depends on the JVM you are using. Not all JVMs will re-optimize code that they have previously optimized.

最后,有一种方法可以使JVM转储JIT编译器输出的本机代码.如果您准备阅读机器说明,则将为您提供内联内容的明确答案.

Finally, there is a way to get the JVM to dump the native code output by the JIT compiler. That will give you a definitive answer as to what has been inlined ... if you are prepared to read the machine instructions.

这篇关于Hotspot VM内联功能是否可以根据需要运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆