Skip to content

Apache Lucene open-source search software

Use ByteArrayComparator for PointInSetQuery#MergePointVisitor (#11876)

gf212105bd83dfe124
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 362bddd3ad5..9901dc0087d 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -150,6 +150,9 @@ Optimizations
given field to match a term (rather than all docs in a segment). This is consistent with
MultiTermQueryConstantScoreWrapper. (Greg Miller)
+* GITHUB#11876: Use ByteArrayComparator to speed up PointInSetQuery in single dimension case.
+ (Guo Feng)
+
Other
---------------------
* LUCENE-10423: Remove usages of System.currentTimeMillis() from tests. (Marios Trivyzas)
diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
index 8719f2f00c7..13533cf25c7 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
@@ -214,7 +214,7 @@ public abstract class PointInSetQuery extends Query implements Accountable {
private final DocIdSetBuilder result;
private TermIterator iterator;
private BytesRef nextQueryPoint;
- private final BytesRef scratch = new BytesRef();
+ private final ByteArrayComparator comparator;
private final PrefixCodedTerms sortedPackedPoints;
private DocIdSetBuilder.BulkAdder adder;
@@ -222,7 +222,7 @@ public abstract class PointInSetQuery extends Query implements Accountable {
throws IOException {
this.result = result;
this.sortedPackedPoints = sortedPackedPoints;
- scratch.length = bytesPerDim;
+ this.comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
this.iterator = this.sortedPackedPoints.iterator();
nextQueryPoint = iterator.next();
}
@@ -257,9 +257,8 @@ public abstract class PointInSetQuery extends Query implements Accountable {
}
private boolean matches(byte[] packedValue) {
- scratch.bytes = packedValue;
while (nextQueryPoint != null) {
- int cmp = nextQueryPoint.compareTo(scratch);
+ int cmp = comparator.compare(nextQueryPoint.bytes, nextQueryPoint.offset, packedValue, 0);
if (cmp == 0) {
return true;
} else if (cmp < 0) {
@@ -276,15 +275,15 @@ public abstract class PointInSetQuery extends Query implements Accountable {
@Override
public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
while (nextQueryPoint != null) {
- scratch.bytes = minPackedValue;
- int cmpMin = nextQueryPoint.compareTo(scratch);
+ int cmpMin =
+ comparator.compare(nextQueryPoint.bytes, nextQueryPoint.offset, minPackedValue, 0);
if (cmpMin < 0) {
// query point is before the start of this cell
nextQueryPoint = iterator.next();
continue;
}
- scratch.bytes = maxPackedValue;
- int cmpMax = nextQueryPoint.compareTo(scratch);
+ int cmpMax =
+ comparator.compare(nextQueryPoint.bytes, nextQueryPoint.offset, maxPackedValue, 0);
if (cmpMax > 0) {
// query point is after the end of this cell
return Relation.CELL_OUTSIDE_QUERY;