Skip to content

Apache Lucene open-source search software

LUCENE-7097: let IntroSorter go 2X deeper in quicksort before switching to heapsort

Mike McCandless8cbe47137755
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index cd9fac2851f..4998eb0ea28 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -21,6 +21,9 @@ Optimizations
* LUCENE-7099: LatLonPoint's newDistanceQuery supports two-phase
iteration. (Robert Muir)
+* LUCENE-7097: IntroSorter now recurses to 2 * log_2(count) quicksort
+ stack depth before switching to heapsort (Adrien Grand, Mike McCandless)
+
Other
* LUCENE-7087: Let MemoryIndex#fromDocument(...) accept 'Iterable<? extends IndexableField>'
diff --git a/lucene/core/src/java/org/apache/lucene/util/IntroSorter.java b/lucene/core/src/java/org/apache/lucene/util/IntroSorter.java
index d9cdd62bf61..498c06aa6ff 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IntroSorter.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IntroSorter.java
@@ -28,17 +28,13 @@ package org.apache.lucene.util;
*/
public abstract class IntroSorter extends Sorter {
- static int ceilLog2(int n) {
- return Integer.SIZE - Integer.numberOfLeadingZeros(n - 1);
- }
-
/** Create a new {@link IntroSorter}. */
public IntroSorter() {}
@Override
public final void sort(int from, int to) {
checkRange(from, to);
- quicksort(from, to, ceilLog2(to - from));
+ quicksort(from, to, 2 * MathUtil.log(to - from, 2));
}
void quicksort(int from, int to, int maxDepth) {