JVM可以内联本机方法吗? [英] Can the JVM inline native methods?
问题描述
我写了一个小的静态JNI函数,它只有5条指令. JVM是否可以将此代码内联到经常调用它的方法的主体中,还是会始终在JITed方法中生成call
指令?
I wrote a small static JNI function which is only 5 instructions long. Is it possible for the JVM to inline this code into the body of a method which calls it frequently or will it always generate a call
instruction in the JITed method?
例如:
public class SomeClass {
private static native long func();
public void doLoop() {
for(int i = 0; i < 0xFFFFFF; i++) {
func();
}
}
public static void main(String[] args) {
for(int i = 0; i < 0xFFFFFF; i++) {
doLoop();
}
}
}
JVM是否可以将func
的代码内联到doLoop
中,还是将其编译为call func
Is it possible for the JVM to inline the code of func
into doLoop
or will it just compile it as call func
推荐答案
不,JVM基本上不能.
No, JVM basically can't.
本机函数的实现是一个二进制黑盒; JVM唯一了解的就是入口点地址.
The implementation of a native function is a binary blackbox; the only thing JVM knows about it is an entry point address.
本机代码不受虚拟机管理,因此无法在Java方法的上下文中执行. JVM区分处于'in_java'状态的线程与处于'in_native'状态的线程.例如,本机线程不会在JVM安全点处停止,仅仅是因为JVM无法执行此操作.
The native code is not managed by the Virtual Machine and cannot be executed in the context of Java method. JVM distinguishes between threads being 'in_java' state from threads being 'in_native'. For example, native threads are not stopped at JVM safepoint, simply because there is no way for JVM to do this.
此外,本机方法调用不是那么简单的操作. 需要特殊的过程来解决JNI调用的所有方面.
Furthermore, a native method call is not so simple operation. A special procedure is required to address all aspects of a JNI call.
这篇关于JVM可以内联本机方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!