diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java index 21b0a99..f294ec9 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java @@ -1542,24 +1542,45 @@ public class Bytes implements Comparable { /** * Returns true if x1 is less than x2, when both values are treated as * unsigned long. + * Both values are passed as is read by Unsafe. When platform is Little Endian, have to + * convert to corresponding Big Endian value and then do compare. We do all writes in + * Big Endian format. */ static boolean lessThanUnsignedLong(long x1, long x2) { + if (littleEndian) { + x1 = Long.reverseBytes(x1); + x2 = Long.reverseBytes(x2); + } return (x1 + Long.MIN_VALUE) < (x2 + Long.MIN_VALUE); } /** * Returns true if x1 is less than x2, when both values are treated as * unsigned int. + * Both values are passed as is read by Unsafe. When platform is Little Endian, have to + * convert to corresponding Big Endian value and then do compare. We do all writes in + * Big Endian format. */ static boolean lessThanUnsignedInt(int x1, int x2) { + if (littleEndian) { + x1 = Integer.reverseBytes(x1); + x2 = Integer.reverseBytes(x2); + } return (x1 & 0xffffffffL) < (x2 & 0xffffffffL); } /** * Returns true if x1 is less than x2, when both values are treated as * unsigned short. + * Both values are passed as is read by Unsafe. When platform is Little Endian, have to + * convert to corresponding Big Endian value and then do compare. We do all writes in + * Big Endian format. */ static boolean lessThanUnsignedShort(short x1, short x2) { + if (littleEndian) { + x1 = Short.reverseBytes(x1); + x2 = Short.reverseBytes(x2); + } return (x1 & 0xffff) < (x2 & 0xffff); } @@ -1603,40 +1624,30 @@ public class Bytes implements Comparable { * time is no slower than comparing 4 bytes at a time even on 32-bit. * On the other hand, it is substantially faster on 64-bit. */ - for (int i = 0; i < minWords * SIZEOF_LONG; i += SIZEOF_LONG) { + // This is the end offset of long parts. + int j = minWords << 3; // Same as minWords * SIZEOF_LONG + for (int i = 0; i < j; i += SIZEOF_LONG) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; - if(littleEndian){ - lw = Long.reverseBytes(lw); - rw = Long.reverseBytes(rw); - } if (diff != 0) { return lessThanUnsignedLong(lw, rw) ? -1 : 1; } } - int offset = minWords * SIZEOF_LONG; + int offset = j; if (minLength - offset >= SIZEOF_INT) { int il = theUnsafe.getInt(buffer1, offset1Adj + offset); int ir = theUnsafe.getInt(buffer2, offset2Adj + offset); - if(littleEndian){ - il = Integer.reverseBytes(il); - ir = Integer.reverseBytes(ir); - } - if(il != ir){ + if (il != ir) { return lessThanUnsignedInt(il, ir) ? -1: 1; } - offset += SIZEOF_INT; + offset += SIZEOF_INT; } if (minLength - offset >= SIZEOF_SHORT) { short sl = theUnsafe.getShort(buffer1, offset1Adj + offset); short sr = theUnsafe.getShort(buffer2, offset2Adj + offset); - if(littleEndian){ - sl = Short.reverseBytes(sl); - sr = Short.reverseBytes(sr); - } - if(sl != sr){ + if (sl != sr) { return lessThanUnsignedShort(sl, sr) ? -1: 1; } offset += SIZEOF_SHORT;