From: Martin Kronbichler Date: Fri, 30 Jun 2023 09:34:12 +0000 (+0200) Subject: Avoid quadratic complexity in IndexSet::add_ranges X-Git-Tag: relicensing~843^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18b0c16b6348fcb4bc52455e217cb5d4722f2831;p=dealii.git Avoid quadratic complexity in IndexSet::add_ranges --- diff --git a/source/base/index_set.cc b/source/base/index_set.cc index f51d30ddfb..cf73cf524b 100644 --- a/source/base/index_set.cc +++ b/source/base/index_set.cc @@ -388,8 +388,15 @@ IndexSet::pop_front() void IndexSet::add_range_lower_bound(const Range &new_range) { - ranges.insert(Utilities::lower_bound(ranges.begin(), ranges.end(), new_range), - new_range); + // if the inserted range is already within the range we find by lower_bound, + // there is no need to do anything; we do not try to be clever here and + // leave all other work to compress(). + const auto insert_position = + Utilities::lower_bound(ranges.begin(), ranges.end(), new_range); + if (insert_position == ranges.end() || + insert_position->begin > new_range.begin || + insert_position->end < new_range.end) + ranges.insert(insert_position, new_range); }