optimize the algorithm of jni