Skip to content

Commit 34f95aa

Browse files
Use binary search in 'find_change_nts' (#5997) (#6026)
* Refs #23618: Use binary search in 'find_change_nts' * Refs #23618: Take into account changes with same sequence number --------- (cherry picked from commit 434af1b) Signed-off-by: cferreiragonz <[email protected]> Co-authored-by: Carlos Ferreira González <[email protected]>
1 parent e0ac90d commit 34f95aa

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/cpp/rtps/history/History.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <fastdds/dds/log/Log.hpp>
2424
#include <fastdds/rtps/common/CacheChange.hpp>
2525

26+
#include <rtps/common/ChangeComparison.hpp>
2627
#include <rtps/history/BasicPayloadPool.hpp>
2728
#include <rtps/history/CacheChangePool.h>
2829

@@ -54,11 +55,15 @@ History::const_iterator History::find_change_nts(
5455
return const_iterator();
5556
}
5657

57-
return std::find_if(changesBegin(), changesEnd(), [this, ch](const CacheChange_t* chi)
58-
{
59-
// use the derived classes comparison criteria for searching
60-
return this->matches_change(chi, ch);
61-
});
58+
// Use binary search to find the first change with the same sequence number
59+
auto lb = std::lower_bound(changesBegin(), changesEnd(), ch, history_order_cmp);
60+
61+
if (lb != changesEnd() && matches_change(*lb, ch))
62+
{
63+
return lb;
64+
}
65+
66+
return changesEnd();
6267
}
6368

6469
bool History::matches_change(

0 commit comments

Comments
 (0)