Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the JVM inline native methods?

Tags:

java

x86

jvm

jit

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?

For example:

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();
        }
    }
}

Is it possible for the JVM to inline the code of func into doLoop or will it just compile it as call func

like image 242
en4bz Avatar asked Aug 14 '26 17:08

en4bz


1 Answers

No, JVM basically can't.

The implementation of a native function is a binary blackbox; the only thing JVM knows about it is an entry point address.

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.

Furthermore, a native method call is not so simple operation. A special procedure is required to address all aspects of a JNI call.

like image 199
apangin Avatar answered Aug 16 '26 05:08

apangin