Skip to content

Apache Lucene open-source search software

Skip heavy TreeSet opts for the first group in FirstPassGroupingCollector (#15128)

gaobinlong9ed912a6b1d8
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 7a9f03ac5d7..98d26031709 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -209,6 +209,8 @@ Optimizations
* GITHUB#15151: Use `SimScorer#score` bulk API to compute impact scores per
block of postings. (Adrien Grand)
+* GITHUB#15128: Skip heavy TreeSet opts for the first group in FirstPassGroupingCollector (Binlong Gao)
+
* GITHUB#15167: FirstPassGroupingCollector supports ignoring docs without group field (Binlong Gao)
* GITHUB#15160: Increased the size used for blocks of postings from 128 to 256.
diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollector.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollector.java
index 34686721515..3471c4ccabe 100644
--- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollector.java
+++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollector.java
@@ -317,14 +317,19 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector {
}
}
- // Remove before updating the group since lookup is done via comparators
- // TODO: optimize this
-
final CollectedSearchGroup<T> prevLast;
+ boolean skipHeavyOps = false;
+
if (orderedGroups != null) {
prevLast = orderedGroups.last();
- orderedGroups.remove(group);
- assert orderedGroups.size() == topNGroups - 1;
+
+ // Skip remove/add for first group
+ if (group == orderedGroups.first()) {
+ skipHeavyOps = true;
+ } else {
+ orderedGroups.remove(group);
+ assert orderedGroups.size() == topNGroups - 1;
+ }
} else {
prevLast = null;
}
@@ -336,9 +341,11 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector {
spareSlot = group.comparatorSlot;
group.comparatorSlot = tmp;
- // Re-add the changed group
+ // Re-add only if we removed it
if (orderedGroups != null) {
- orderedGroups.add(group);
+ if (!skipHeavyOps) {
+ orderedGroups.add(group);
+ }
assert orderedGroups.size() == topNGroups;
final CollectedSearchGroup<?> newLast = orderedGroups.last();
// If we changed the value of the last group, or changed which group was last, then update