A quick jni4net performance hack

jni4net is a great library that creates Java proxies for Microsoft based C# libraries on the Windows platform. It makes Java JNI easy with good performance.  You follow this processes to use jni4net

  1. Create your C# library
  2. Run proxygen to generate Java native library source code and C# adapter source that shims in between Java and the original library.
  3. Run javac to compile the java code
  4. Create a jar file of the compiled java code
  5. Run the .Net csc.exe that comes with the default .Net installation to compile the C# code
  6. Create a new DLL of the just compiled C# code
  7. Create a Java program that uses the new generated Jar file, the generated C# dll and the original C# dll.


I found a quick hack that improves the performance of the jni4net bridge that involves inserting a line of code in each generate C# method. This hack improves the Java-to-DLL call time only.  It does not impact the performance of the Java code on one side or the C# code on the other.  It lowers the overhead of highly repetitive method calls by up to 100x for methods with no type marshaling.  Each method looks something like this:

I add one line of code that makes the method look like this:

This Powershell script modifies all jni4net generated methods to add the code shown above.  You can also find the script on github :

...

Comments

  1. These are great instructions for quick Jni4net performance improvement. All the essential info is there and I've good time studying such awesome demonstration. Thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Almost PaaS Document Parsing with Tika and AWS Elastic Beanstalk