From: Bryan T. <tho...@us...> - 2007-04-13 17:52:55
|
Update of /cvsroot/cweb/bigdata/src/java/com/bigdata/btree In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv8389/src/java/com/bigdata/btree Modified Files: BytesUtil.java Log Message: Improved options for using JNI in BytesUtil, but it is still slower in every case that I have tested. Index: BytesUtil.java =================================================================== RCS file: /cvsroot/cweb/bigdata/src/java/com/bigdata/btree/BytesUtil.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** BytesUtil.java 13 Apr 2007 17:03:09 -0000 1.4 --- BytesUtil.java 13 Apr 2007 17:52:52 -0000 1.5 *************** *** 3,6 **** --- 3,8 ---- import java.util.Comparator; + import org.apache.log4j.Logger; + /** * Class supporting operations on variable length byte[] keys. *************** *** 13,17 **** * positions in that byte[] is maintained. * <p> ! * See {@link #main(String[])} which provides a test for the JNI integration. * * @author <a href="mailto:tho...@us...">Bryan Thompson</a> --- 15,33 ---- * positions in that byte[] is maintained. * <p> ! * JNI methods are provided for unsigned byte[] comparison. However, note that ! * the JNI methods do not appear to be as fast as the pure Java methods - ! * presumably because of the overhead of going from Java to C. In order to ! * execute using the JNI methods you MUST define the optional boolean system ! * property, e.g., ! * ! * <pre> ! * java -Dcom.bigdata.btree.BytesUtil.jni=true ... ! * </pre> ! * ! * See BytesUtil.c in this package for instructions on compiling the JNI ! * methods. ! * </p> ! * See {@link #main(String[])} which provides a test for the JNI integration and ! * some pointers on how to get this running on your platform. * * @author <a href="mailto:tho...@us...">Bryan Thompson</a> *************** *** 20,23 **** --- 36,41 ---- public class BytesUtil { + protected static final Logger log = Logger.getLogger(BytesUtil.class); + /** * Flag set iff JNI linking succeeds. When this flag is false we run with *************** *** 25,29 **** * the JNI versions are used. */ ! static boolean linked; /** --- 43,47 ---- * the JNI versions are used. */ ! static boolean linked = false; /** *************** *** 39,60 **** static { ! try { ! System.loadLibrary("BytesUtil"); ! System.err.println("BytesUtil JNI linked"); ! linked = true; ! } catch (UnsatisfiedLinkError ex) { ! System.err.println("BytesUtil JNI NOT linked: " + ex); ! linked = false; } - - /* - * Strangly it appears that java is faster than JNI + C for byte[] - * comparisons! - */ - linked = false; } /** * True iff the two arrays compare as equal. This is somewhat optimized in * that it tests the array lengths first, assumes that it is being used on --- 57,125 ---- static { ! final boolean jni; ! ! String val = System.getProperty("com.bigdata.btree.BytesUtil.jni"); ! ! if (val != null) { ! ! jni = Boolean.parseBoolean(val); ! ! } else { ! ! jni = false; // Note: We will not even try to use JNI by default! ! ! } ! ! if (jni) { ! ! /* ! * Attempt to load the JNI library. ! */ ! ! loadJNILibrary(); ! } } /** + * Attempt to load the JNI library. + * <p> + * Note: this is done automatically if the optional boolean system property + * <code>com.bigdata.btree.BytesUtil.jni=true</code> is specified, e.g., + * using + * + * <pre> + * java -Dcom.bigdata.btree.BytesUtil.jni=true ... + * </pre> + * + * @return True iff the JNI library was successfully linked. + */ + public static boolean loadJNILibrary() { + + if (!linked) { + + try { + + System.loadLibrary("BytesUtil"); + + log.info("BytesUtil JNI linked"); + + linked = true; + + } catch (UnsatisfiedLinkError ex) { + + log.warn("BytesUtil JNI NOT linked: " + ex); + + linked = false; + + } + } + + return linked; + + } + + /** * True iff the two arrays compare as equal. This is somewhat optimized in * that it tests the array lengths first, assumes that it is being used on *************** *** 580,589 **** /** ! * This method tries to execute the JNI methods. ! * ! * See BytesUtil.c in this package for instructions on compiling the JNI ! * methods. However, note that the JNI methods do not appear to be as fast ! * as the pure Java methods - presumably because of the overhead of going ! * from Java to C. * <p> * In order to use the JNI library under Windows, you must specify the JNI --- 645,650 ---- /** ! * This method forces the load of the JNI library and tries to execute the ! * JNI methods. * <p> * In order to use the JNI library under Windows, you must specify the JNI *************** *** 591,597 **** * * <pre> ! * cd bigdata ! * set PATH=%PATH%;lib ! * java -cp bin com.bigdata.btree.BytesUtil * </pre> * --- 652,658 ---- * * <pre> ! * cd bigdata ! * set PATH=%PATH%;lib ! * java -cp bin com.bigdata.btree.BytesUtil * </pre> * *************** *** 601,605 **** * * <pre> ! * java -Djava.library.path=lib com.bigdata.btree.BytesUtil * </pre> * --- 662,666 ---- * * <pre> ! * java -Djava.library.path=lib com.bigdata.btree.BytesUtil * </pre> * *************** *** 613,616 **** --- 674,680 ---- public static void main(String[] args) { + // Force load of the JNI library. + loadJNILibrary(); + if( 0 != BytesUtil._compareBytes(3, new byte[]{1,2,3}, 3, new byte[]{1,2,3}) ) { |