From 15663284fdebd5bc59b2177f7cc4fa3803ac0f52 Mon Sep 17 00:00:00 2001 From: Binlong Gao Date: Mon, 30 Mar 2026 21:49:28 +0800 Subject: [PATCH 1/2] Skip heavy TreeSet opts for the first group in SearchGroup#merge Signed-off-by: Binlong Gao --- lucene/CHANGES.txt | 2 ++ .../org/apache/lucene/search/grouping/SearchGroup.java | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 6c8da2bf113d..a8644a50cabb 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -278,6 +278,8 @@ Optimizations * GITHUB#15779: Improve BytesRefHash.add performance by optimize rehash operation (tyronecai) +* GITHUB#15128: Skip heavy TreeSet opts for the first group in SearchGroup#merge (Binlong Gao) + Bug Fixes --------------------- * GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequent diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java index 863924d42c26..36bab96b4f4f 100644 --- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java +++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java @@ -249,13 +249,17 @@ private void updateNextGroup(int topN, ShardIter shard) { // System.out.println(" competes=" + competes); if (competes) { - // Group's sort changed -- remove & re-insert - if (mergedGroup.inQueue) { + // Group's sort changed -- remove & re-insert, update first group in place for + // efficiency + boolean skipHeavyOps = queue.first() == mergedGroup; + if (mergedGroup.inQueue && !skipHeavyOps) { queue.remove(mergedGroup); } mergedGroup.topValues = group.sortValues; mergedGroup.minShardIndex = shard.shardIndex; - queue.add(mergedGroup); + if (!skipHeavyOps) { + queue.add(mergedGroup); + } mergedGroup.inQueue = true; } } From 5f5197489775ecace90cc026f3a1adbe0cb439f5 Mon Sep 17 00:00:00 2001 From: Binlong Gao Date: Tue, 31 Mar 2026 16:45:44 +0800 Subject: [PATCH 2/2] Modify change log Signed-off-by: Binlong Gao --- lucene/CHANGES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index a8644a50cabb..db3c0a0d3df7 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -278,7 +278,7 @@ Optimizations * GITHUB#15779: Improve BytesRefHash.add performance by optimize rehash operation (tyronecai) -* GITHUB#15128: Skip heavy TreeSet opts for the first group in SearchGroup#merge (Binlong Gao) +* GITHUB#15896: Skip heavy TreeSet opts for the first group in SearchGroup#merge (Binlong Gao) Bug Fixes ---------------------