Skip to content

Apache Lucene open-source search software

optimize BKDWriter's offline comparator a bit

Mike McCandless251cdbcee367
diff --git a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
index c733e157e8d..6d958bace50 100644
--- a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
+++ b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
@@ -377,6 +377,7 @@ public abstract class StringHelper {
* big-endian unsigned values. Returns positive int if a > b,
* negative int if a < b and 0 if a == b */
public static int compare(int count, byte[] a, int aOffset, byte[] b, int bOffset) {
+ // TODO: dedup this w/ BytesRef.compareTo?
for(int i=0;i<count;i++) {
int cmp = (a[aOffset+i]&0xff) - (b[bOffset+i]&0xff);
if (cmp != 0) {
diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
index d7e19543f5c..4397f2e1ab1 100644
--- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
@@ -665,34 +665,35 @@ public class BKDWriter implements Closeable {
// Offline sort:
assert tempInput != null;
- final ByteArrayDataInput reader = new ByteArrayDataInput();
Comparator<BytesRef> cmp = new Comparator<BytesRef>() {
- private final ByteArrayDataInput readerB = new ByteArrayDataInput();
+
+ final ByteArrayDataInput reader = new ByteArrayDataInput();
@Override
public int compare(BytesRef a, BytesRef b) {
- reader.reset(a.bytes, a.offset, a.length);
- reader.readBytes(scratch1, 0, scratch1.length);
- final int docIDA = reader.readVInt();
- final long ordA = reader.readVLong();
-
- reader.reset(b.bytes, b.offset, b.length);
- reader.readBytes(scratch2, 0, scratch2.length);
- final int docIDB = reader.readVInt();
- final long ordB = reader.readVLong();
- int cmp = StringHelper.compare(bytesPerDim, scratch1, bytesPerDim*dim, scratch2, bytesPerDim*dim);
+ // First compare the bytes on the dimension we are sorting on:
+ int cmp = StringHelper.compare(bytesPerDim, a.bytes, a.offset + bytesPerDim*dim, b.bytes, b.offset + bytesPerDim*dim);
if (cmp != 0) {
return cmp;
}
- // Tie-break
+ // Tie-break by docID and then ord:
+ reader.reset(a.bytes, a.offset + packedBytesLength, a.length);
+ final int docIDA = reader.readVInt();
+ final long ordA = reader.readVLong();
+
+ reader.reset(b.bytes, b.offset + packedBytesLength, b.length);
+ final int docIDB = reader.readVInt();
+ final long ordB = reader.readVLong();
+
cmp = Integer.compare(docIDA, docIDB);
if (cmp != 0) {
return cmp;
}
+ // TODO: is this really necessary? If OfflineSorter is stable, we can safely return 0 here, and avoid writing ords?
return Long.compare(ordA, ordB);
}
};