uchiha-asha commented on code in PR #17045:
URL: https://github.com/apache/lucene/pull/17045#discussion_r4639200094
##########
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:
Thanks @jainankitk for the review. I ran the benchmark for scattered hits
with `maxDoc` just above `2^16 (40% faster)` and `2^24 (27% faster)`. Extra
scan is sequential while radix pass involve building histogram and random
writes.
--
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]