Hi,

I've seen in the JNI aprImpl the JK_DIRECT_BUFFER_NIO flag that is newer
used.
IMO the purpose would be to use the java.nio. Package available in the
java 1.4 and JNI 1.4 specification.
Now, I've done some testing and found couple of ineresting things that I
would like to clear out.

Don't know if the testing code is valid but briefly here are some facts.

The direct buffering is slower (!) then standard byte array if there is
no copy, but faster about 20% if the array gets  copied.

The most important thing that bothers me is that the java is more then 2
times slower then the GetByteArrayRegion version.
Now, I allway tought that JNI calls imposes serious performance
degradation, but I'm not so sure now.

Any comments?

MT.
 

Attachment: jniperf.gif
Description: GIF image

Attachment: NioTest.java
Description: Binary data

#include <jni.h>

#include <stdlib.h>

jint standardSize = 0;
jint advancedSize = 0;
jbyte *advancedBuffer = NULL;
jbyte *standardBuffer = NULL;

JNIEXPORT void JNICALL Java_NioTest_initStandardNative
  (JNIEnv * env, jclass c, jint  nSize)
{
    
    standardSize = nSize;
    standardBuffer = (jbyte *)calloc(standardSize, sizeof(jbyte));
}

JNIEXPORT void JNICALL Java_NioTest_initAdvancedNative
  (JNIEnv * env, jclass c, jobject theBytes)
{
    if (theBytes) {
        advancedBuffer = (jbyte *)((*env)->GetDirectBufferAddress(env,theBytes));
    } 
    if (advancedBuffer) {
        advancedSize = (jint)((*env)->GetDirectBufferCapacity(env,theBytes));
        memset(advancedBuffer, 0, advancedSize);
    }
}

JNIEXPORT jint JNICALL Java_NioTest_callStandardNative
  (JNIEnv * env, jclass c, jbyteArray theBytes)
{
    jint i;
    jbyte *buffer=NULL;
    jboolean iscopy;
    jint rv = 0;
    buffer = (jbyte *)((*env)->GetByteArrayElements(env, theBytes, &iscopy));
    if (buffer) {
        for (i = 0; i < standardSize; i++) {
            buffer[i] += (jbyte)i;            
            rv += buffer[i];
        }
    }
    (*env)->ReleaseByteArrayElements(env, theBytes, buffer, 0);

    return rv;
}

JNIEXPORT jint JNICALL Java_NioTest_callStandardNativeC
  (JNIEnv * env, jclass c, jbyteArray theBytes)
{
    jint i;
    jint rv = 0;
    
    (*env)->GetByteArrayRegion(env,theBytes,0,standardSize,standardBuffer); 

    for (i = 0; i < standardSize; i++) {
        standardBuffer[i] += (jbyte)i;            
        rv += standardBuffer[i];
    }

    (*env)->SetByteArrayRegion(env,theBytes,0,standardSize,standardBuffer); 

    return rv;
}


JNIEXPORT jint JNICALL Java_NioTest_callAdvancedNative
    (JNIEnv * env, jclass c)
{

    jint i;
    jint rv = 0;
    for (i = 0; i < advancedSize; i++) {
        advancedBuffer[i] += (jbyte)i;            
        rv += advancedBuffer[i];
    }
    return rv;

}


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to