jainankitk commented on code in PR #17045:
URL: https://github.com/apache/lucene/pull/17045#discussion_r4637004374
##########
lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.java:
##########
@@ -367,6 +366,55 @@ private static Buffer concat(List<Buffer> buffers) {
return new Buffer(docs, totalLength);
}
+ /** Sort {@code docs[0:length]} in ascending order. */
+ private void sort(int[] docs, int length) {
+ LSBRadixSorter sorter = new LSBRadixSorter();
+ final int numBits = PackedInts.bitsRequired(maxDoc - 1);
+ if (length < LSBRadixSorter.INSERTION_SORT_THRESHOLD) {
+ // Small inputs are sorted with insertion sort, whose cost does not
depend on the number of
+ // bits of the values.
+ sorter.sort(numBits, docs, length);
+ return;
+ }
+ // The cost of the radix sort is proportional to the number of bits of the
values to sort. Doc
+ // IDs are often clustered in a small range of the doc ID space, e.g. when
a range query on a
+ // timestamp runs against a segment whose documents got indexed in
(approximate) timestamp
+ // order, or when the index is sorted. So we sort deltas from the minimum
doc ID rather than
+ // doc IDs if this saves sorting passes.
+ final int numPasses = numRadixSortPasses(numBits);
+ int min = docs[0];
+ int max = docs[0];
Review Comment:
Could this live in `LSBRadixSorter` instead, e.g. an offset that's
subtracted when computing buckets? That avoids the separate subtract/add
passes, lets `FreqProxTermsWriter.SortingDocsEnum` benefit too, and keeps
`INSERTION_SORT_THRESHOLD` private.
##########
lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.java:
##########
@@ -367,6 +366,55 @@ private static Buffer concat(List<Buffer> buffers) {
return new Buffer(docs, totalLength);
}
+ /** Sort {@code docs[0:length]} in ascending order. */
+ private void sort(int[] docs, int length) {
+ LSBRadixSorter sorter = new LSBRadixSorter();
+ final int numBits = PackedInts.bitsRequired(maxDoc - 1);
+ if (length < LSBRadixSorter.INSERTION_SORT_THRESHOLD) {
+ // Small inputs are sorted with insertion sort, whose cost does not
depend on the number of
+ // bits of the values.
+ sorter.sort(numBits, docs, length);
+ return;
+ }
+ // The cost of the radix sort is proportional to the number of bits of the
values to sort. Doc
+ // IDs are often clustered in a small range of the doc ID space, e.g. when
a range query on a
+ // timestamp runs against a segment whose documents got indexed in
(approximate) timestamp
+ // order, or when the index is sorted. So we sort deltas from the minimum
doc ID rather than
+ // doc IDs if this saves sorting passes.
+ final int numPasses = numRadixSortPasses(numBits);
+ int min = docs[0];
+ int max = docs[0];
+ // The range of doc IDs can only grow as we look at more doc IDs, so stop
looking at doc IDs
+ // as soon as we know that sorting deltas would not save passes. This way
doc IDs that are
+ // scattered across the doc ID space only pay for looking at the first few
of them.
+ for (int from = 1;
+ from < length && numRadixSortPasses(PackedInts.bitsRequired(max -
min)) < numPasses; ) {
+ final int to = Math.min(length, from + 64);
+ for (; from < to; ++from) {
+ final int doc = docs[from];
+ min = Math.min(min, doc);
+ max = Math.max(max, doc);
+ }
+ }
+ final int numDeltaBits = PackedInts.bitsRequired(max - min);
+ if (numRadixSortPasses(numDeltaBits) < numPasses) {
Review Comment:
When this saves only one pass, the extra scan + subtract + add passes may
cost about as much as the radix pass they save. This also hits scattered hits
when `maxDoc` is just above a byte boundary (e.g. ~17M docs: the range rarely
reaches 2^24, so we scan most of the array and take this path). Could you
benchmark that case, or require saving at least 2 passes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]