You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2024/01/30 10:39:21 UTC

(lucene) branch main updated: Change `set.removeAll(list)` to `list.forEach(set::remove)` (#13052)

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 39c10a29291 Change `set.removeAll(list)` to `list.forEach(set::remove)` (#13052)
39c10a29291 is described below

commit 39c10a29291203b10f8da0133f4cabb392c50fe2
Author: Dmitry Cherniachenko <2s...@gmail.com>
AuthorDate: Tue Jan 30 11:39:15 2024 +0100

    Change `set.removeAll(list)` to `list.forEach(set::remove)` (#13052)
---
 lucene/CHANGES.txt                                                  | 2 ++
 .../src/java/org/apache/lucene/index/UpgradeIndexMergePolicy.java   | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f0a5dea07a8..2db6735e644 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -228,6 +228,8 @@ Optimizations
   this will help boolean queries that consist of a mix OF FILTER clauses and
   SHOULD clauses. (Adrien Grand)
 
+* GITHUB#13052: Avoid set.removeAll(list) O(n^2) performance trap in the UpgradeIndexMergePolicy (Dmitry Cherniachenko)
+
 Bug Fixes
 ---------------------
 * GITHUB#12866: Prevent extra similarity computation for single-level HNSW graphs. (Kaival Parikh)
diff --git a/lucene/core/src/java/org/apache/lucene/index/UpgradeIndexMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/UpgradeIndexMergePolicy.java
index 655d5747ec4..944fd780d8c 100644
--- a/lucene/core/src/java/org/apache/lucene/index/UpgradeIndexMergePolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/index/UpgradeIndexMergePolicy.java
@@ -106,7 +106,11 @@ public class UpgradeIndexMergePolicy extends FilterMergePolicy {
       // the resulting set contains all segments that are left over
       // and will be merged to one additional segment:
       for (final OneMerge om : spec.merges) {
-        oldSegments.keySet().removeAll(om.segments);
+        // om.segments.forEach(::remove) is used here instead of oldSegments.keySet().removeAll()
+        // for performance reasons; when om.segments.size() == oldSegments.size()
+        // the AbstractSet#removeAll() implementation will iterate the set elements
+        // calling list.contains() for each of them, resulting in O(n^2) performance
+        om.segments.forEach(oldSegments::remove);
       }
     }