logo       

Re: Java and Arrays: msg#00132

programming.swig

Subject: Re: Java and Arrays

Hoepfinger, Franz-Xaver (FH Rosenheim) wrote:
Hello


SWIG will produce code like this, when using "arrays_java.i":

/* signed char[] support */
int SWIG_JavaArrayInSchar (JNIEnv *jenv, jbyte **jarr, signed char **carr,
jbyteArray input) {
.
.
. sz = (*jenv)->GetArrayLength(jenv, input);
*jarr = (*jenv)->GetByteArrayElements(jenv, input, 0);
.
.
.
for (i=0; i<sz; i++)
(*carr)[i] = (signed char)(*jarr)[i];
return 1;
}

you can see: it does Copy with a "for" loop.

This may suffer from Performance.

WHY does SWIG not Use the Get<PrimitiveType>ArrayRegion functions ??

Described here:
http://java.sun.com/products/jdk/1.2/docs/guide/jni/spec/functions.doc.html


The reason is for platform independence. SWIG wraps C code and so for the example you list above, you are wrapping a signed char type. On my system jbyte is typedef'd to signed char in a platform specific header file. Other systems will have jbyte typedef'd to other C types. So in a nutshell there is no way to know which JNI type is being used, which is necessary for the Get<PrimitiveType>ArrayRegion functions as they require a pointer to an array of the JNI type. Additionally, if the JNI to C mapping is known, unpredictable results would still be possible if the C array used a different packing to that used when the JVM was compiled. The approach taken skirts these problems as it accesses one element at a time with appropriate casting.

I think the documentation mentions that this array library suffers performance problems and for better performance suggests using the main SWIG array library instead. The beauty of SWIG of course is that you can also customise the output by writing your own typemaps using a priori information about your platform.

Cheers
William

_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise