2 messages in net.java.dev.jna.usersRe: [jna-users] Performance tuning...
FromSent OnAttachments
Drayton BrownApr 23, 2009 9:21 am 
Timothy WallApr 23, 2009 9:54 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Performance tuning...Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Apr 23, 2009 9:54:02 am
List:net.java.dev.jna.users

Once the native method starts executing, JNA has nothing to do with it. Using com.sun.jna.Memory is indistinguishable from using memory returned by malloc. Using a direct NIO buffer may help performance in Java code, but won't have any effect on the native code.

Disabling structure autosync will only have a noticeable effect on structures substantially larger than yours.

Does your native code expect its data to be aligned to a particular boundary?

Make sure you're timing what you think you're timing.

On Apr 23, 2009, at 12:22 PM, Drayton Brown wrote:

Hi all

I'm working with the following structure:

public static class myStruct extends Structure { public int depth; public int width; public int height; public int rowBytes; public int columnBytes; public Pointer[] bands = new Pointer[3]; }

I then write a load of bytes to the bands field of the structure like this:

for (int band = 0; band < bufferedImage.getRaster().getNumBands(); band++) { int sample = 0; myStruct.bands[band] = new Memory(stride * bufferedImage.getHeight()); for (int y = 0; y < bufferedImage.getHeight(); y++) { for (int x = 0; x < bufferedImage.getWidth(); x++) { myStruct.bands[band].setByte(sample++, (byte) bufferedImage.getRaster().getSample(x, y, band)); } } }

I then pass this structure into a native method. Something like this:

myNativeLib.nativeLibMethod(myStruct);

The native method then takes about 30 seconds to complete its data manipulation task. However this same task takes less than 5 seconds to execute in its natural (non java/jna) environment.

I'm aware that there is a large overhead for the JNA API, however I believe that this maybe more than I should expect to see. I have read up on performance tweaking and tried disabling the autoSync for this structure and doing it manually instead. This unfortunately did not help at all. I timed the writeField method calls, and the time they took was bearly measureable. The time to complete the call to the native method however did not change.

My question is, should I try a NIO buffer? Are these generally better than the Memory objects? Also I'm interested to know more detail on how the performance of a native call is affected by JNA. I would have thought that if I manually sync'd the structure I pass to the native method before calling the said method, then the native method's execution performance should be noticably increased. As this does not seem to be the case, what else is causing the performance hit? Once the native method starts to execute, is there any performance retardation when handling memory blocks passed in from Java?

Thanks, in advance, for your time!

Regards Drayton