From 05bd83dfe12463231b53259aff23b685c8574831 Mon Sep 17 00:00:00 2001 From: gf2121 <52390227+gf2121@users.noreply.github.com> Date: Wed, 26 Oct 2022 13:39:32 +0800 Subject: [PATCH] Use ByteArrayComparator for PointInSetQuery#MergePointVisitor (#11876) --- lucene/CHANGES.txt | 3 +++ .../org/apache/lucene/search/PointInSetQuery.java | 15 +++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) 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;