Skip to content

Commit 30af271

Browse files
committed
perf: sort discovered sync items once instead of inserting in order
slotItemDiscovered() kept _syncItems sorted by inserting each item at its lower_bound. Inserting into the middle of a contiguous vector memmoves the tail, so discovering n items costs O(n^2): for a first sync of a large tree that is billions of shared-pointer moves on the GUI thread, all of it before the first file is transferred. Nothing reads _syncItems before discovery ends -- the first consumer is handleMassDeletion(), from slotDiscoveryFinished() -- so append and sort once there. Stable, so that items comparing equal keep the order they were discovered in. Signed-off-by: Felipe Tumonis <ftumonis@gmail.com>
1 parent b385cf5 commit 30af271

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/libsync/syncengine.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <unistd.h>
3131
#endif
3232

33+
#include <algorithm>
3334
#include <climits>
3435
#include <cassert>
3536
#include <chrono>
@@ -473,9 +474,10 @@ void OCC::SyncEngine::slotItemDiscovered(const OCC::SyncFileItemPtr &item)
473474
checkErrorBlacklisting(*item);
474475
_needsUpdate = true;
475476

476-
// Insert sorted
477-
auto it = std::lower_bound( _syncItems.begin(), _syncItems.end(), item ); // the _syncItems is sorted
478-
_syncItems.insert( it, item );
477+
// Consumers need _syncItems sorted, but inserting in order here would memmove the tail of the
478+
// vector once per item and cost O(n^2) overall. Nothing reads _syncItems before discovery ends,
479+
// so it is sorted in one pass in slotDiscoveryFinished() instead.
480+
_syncItems.append(item);
479481

480482
slotNewItem(item);
481483

@@ -847,6 +849,10 @@ void SyncEngine::slotDiscoveryFinished()
847849
_restartedSyncAfterDiscovery = false;
848850
}
849851

852+
// Restores the ordering slotItemDiscovered() deliberately did not maintain per item. Stable so
853+
// that items comparing equal keep the order they were discovered in.
854+
std::stable_sort(_syncItems.begin(), _syncItems.end());
855+
850856
if (handleMassDeletion()) {
851857
return;
852858
}

0 commit comments

Comments
 (0)