Interpreting Java-based communications performance

I am currently using JNA for Java-based communication and am pleased with its simplicity. However, I need to optimize performance and consider using other bindings.

My question is: what part of the Java native communication is the “expensive” part? Is it data transfer between them?

Let me put it another way. Right now, the functions that my JNA interface calls do not pass any data at all to Java, and the functions are not even called that often. In other words, Java calls the library call, and then the library call does its own work for a while and returns a primitive type. Will JNI / Swig / etc be faster than JNA in this situation?

+4
source share
1 answer

Given your use case, JNI will not be faster than JNA.

What is expensive for Java native interaction is the transfer of large amounts of memory. In particular, it can be very expensive to make Java memory available to native code; IIRC is partly because Java can choose a memory segment, but it loves, but native code will expect continuous chunks of memory - moving / copying memory takes some time.

If you are concerned about performance, you should make sure that your JNA code uses "direct" style access rather than the original interface style.

In addition, if you need to transfer large amounts of memory between Java and native code, you should consider using one initial direct allocation (if possible) and not reallocate this memory on a regular basis. Thus, you pay the distribution cost only once and at the beginning, therefore, for a large number of calls, the cost of which becomes insignificant.

+6
source

Source: https://habr.com/ru/post/1301234/