Optimize Min and Max BKD optimizations (#44315)

MinAggregator - skip BKD optimization when no result found after 1024 lookups.
MaxAggregator - skip unnecessary conversions.
This commit is contained in:
Michał Perlak 2019-07-26 21:40:27 +02:00 committed by Zachary Tong
parent 3839840b0c
commit 245c9b7914
2 changed files with 13 additions and 4 deletions

View File

@ -22,9 +22,9 @@ import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PointValues; import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.CollectionTerminatedException; import org.apache.lucene.search.CollectionTerminatedException;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.FutureArrays; import org.apache.lucene.util.FutureArrays;
import org.apache.lucene.search.ScoreMode;
import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.DoubleArray;
@ -174,7 +174,7 @@ class MaxAggregator extends NumericMetricsAggregator.SingleValue {
} }
int numBytes = pointValues.getBytesPerDimension(); int numBytes = pointValues.getBytesPerDimension();
final byte[] maxValue = pointValues.getMaxPackedValue(); final byte[] maxValue = pointValues.getMaxPackedValue();
final Number[] result = new Number[1]; final byte[][] result = new byte[1][];
pointValues.intersect(new PointValues.IntersectVisitor() { pointValues.intersect(new PointValues.IntersectVisitor() {
@Override @Override
public void visit(int docID) { public void visit(int docID) {
@ -186,7 +186,10 @@ class MaxAggregator extends NumericMetricsAggregator.SingleValue {
if (liveDocs.get(docID)) { if (liveDocs.get(docID)) {
// we need to collect all values in this leaf (the sort is ascending) where // we need to collect all values in this leaf (the sort is ascending) where
// the last live doc is guaranteed to contain the max value for the segment. // the last live doc is guaranteed to contain the max value for the segment.
result[0] = converter.apply(packedValue); if (result[0] == null) {
result[0] = new byte[packedValue.length];
}
System.arraycopy(packedValue, 0, result[0], 0, packedValue.length);
} }
} }
@ -200,6 +203,6 @@ class MaxAggregator extends NumericMetricsAggregator.SingleValue {
} }
} }
}); });
return result[0]; return result[0] != null ? converter.apply(result[0]) : null;
} }
} }

View File

@ -52,6 +52,7 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
class MinAggregator extends NumericMetricsAggregator.SingleValue { class MinAggregator extends NumericMetricsAggregator.SingleValue {
private static final int MAX_BKD_LOOKUPS = 1024;
final ValuesSource.Numeric valuesSource; final ValuesSource.Numeric valuesSource;
final DocValueFormat format; final DocValueFormat format;
@ -212,6 +213,8 @@ class MinAggregator extends NumericMetricsAggregator.SingleValue {
final Number[] result = new Number[1]; final Number[] result = new Number[1];
try { try {
pointValues.intersect(new PointValues.IntersectVisitor() { pointValues.intersect(new PointValues.IntersectVisitor() {
private short lookupCounter = 0;
@Override @Override
public void visit(int docID) { public void visit(int docID) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -224,6 +227,9 @@ class MinAggregator extends NumericMetricsAggregator.SingleValue {
// this is the first leaf with a live doc so the value is the minimum for this segment. // this is the first leaf with a live doc so the value is the minimum for this segment.
throw new CollectionTerminatedException(); throw new CollectionTerminatedException();
} }
if (++lookupCounter > MAX_BKD_LOOKUPS) {
throw new CollectionTerminatedException();
}
} }
@Override @Override