diff --git a/src/impl/results_notifier.cpp b/src/impl/results_notifier.cpp index 55300f56..b097cec0 100644 --- a/src/impl/results_notifier.cpp +++ b/src/impl/results_notifier.cpp @@ -40,17 +40,6 @@ void ResultsNotifier::release_data() noexcept m_query = nullptr; } -static bool map_moves(size_t& idx, CollectionChangeIndices const& changes) -{ - for (auto&& move : changes.moves) { - if (move.from == idx) { - idx = move.to; - return true; - } - } - return false; -} - // Most of the inter-thread synchronization for run(), prepare_handover(), // attach_to(), detach(), release_data() and deliver() is done by // RealmCoordinator external to this code, which has some potentially @@ -117,14 +106,18 @@ void ResultsNotifier::run() next_rows.push_back(m_tv[i].get_index()); if (changes) { + auto move_map = changes->moves; + std::sort(begin(move_map), end(move_map), + [](auto const& a, auto const& b) { return a.from < b.from; }); for (auto& idx : m_previous_rows) { - if (!map_moves(idx, *changes)) { - if (changes->deletions.contains(idx)) - idx = npos; - else { - REALM_ASSERT_DEBUG(!changes->insertions.contains(idx)); - } - } + auto it = lower_bound(begin(move_map), end(move_map), idx, + [](auto const& a, auto b) { return a.from < b; }); + if (it != move_map.end() && it->from == idx) + idx = it->to; + else if (changes->deletions.contains(idx)) + idx = npos; + else + REALM_ASSERT_DEBUG(!changes->insertions.contains(idx)); } }