dcsimg
How expensive are callbacks?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Tanner_Davis
Posted On:   Monday, December 8, 2003 08:12 AM

In writing a java wrapper for a C based native API, I need to transfer a lot of data out of a C Struct into a Java object. In the end, I need to have all the data from the C Struct into a java hashmap for later consumption. I know there are a lot of ways to do this, however is there any reason I should NOT use a callback to the hashmap to put these values in (ie one at a time). Once the MID has been identified, are these calls very expensive? I guess the alternative is to push all the values over the JNI via a object array or use some kind of java proxy, however if callbacks are not that much more expenive, then I would rather manipulate the hashmap directly. Otherwise, I would have to do yet another mapping once I was back on the java side. (Assu   More>>

In writing a java wrapper for a C based native API, I need to transfer a lot of data out of a C Struct into a Java object. In the end, I need to have all the data from the C Struct into a java hashmap for later consumption. I know there are a lot of ways to do this, however is there any reason I should NOT use a callback to the hashmap to put these values in (ie one at a time). Once the MID has been identified, are these calls very expensive?

I guess the alternative is to push all the values over the JNI via a object array or use some kind of java proxy, however if callbacks are not that much more expenive, then I would rather manipulate the hashmap directly. Otherwise, I would have to do yet another mapping once I was back on the java side. (Assuming i do the findMethod call just once per hashmap).

As I do not have an abundance of C knowledge, I want get the data off the native and back into the java as fast as possible.
so.. callbacks or object array?

Thank for the help.
Tanner

   <<Less

Re: How expensive are callbacks?

Posted By:   Ganesh_Iyer  
Posted On:   Wednesday, December 10, 2003 05:40 PM

Callbacks from jni layer back to java are pretty expensive.


Al alternative would be to make use of java nio direct buffers.
Direct buffers allow u to share a physical memory between jni and java



eg: - A ByteBuffer allocated with ByteBuffer.allocateDirect(... ) fn would represent a piece of memory which corresponds to a memory address in the native layer
U could have a jni call which passes the ByteBuffer in the jni layer as part of some initialisation process.
Inside this call - extract the memory address of the shared memory using the jnienv->GetDirectBufferAddress(.. function as a char*.



Once u get this address, u can diretly typecast ur struct to this memory location.




eg: -
char* buf = env->GetDirectBufferAddress(..




struct ASTRUCT{
int a ;
int b;
int c;
};




ASTRUCT *astruct = (ASTRUCT*) buf;
astruct->a = 1;
astruct->b = 2;
astruct->c = 3;




Voila.
u can now extract the same values from the java side, by calling getInt() from the associated ByteBuffer object,.



u can store the ByteBuffers in a hashmap. Or do some other kind of optimised storage

About | Sitemap | Contact