Skip to content

Commit d94f294

Browse files
committed
perf(discovery): reduce per-file allocations in discovery phase
- Replace QString::split('.') with indexOf/lastIndexOf to extract basename and extension without allocating a QList<QString> per file. - Replace 5 QTimer::singleShot(0,...) calls with QMetaObject::invokeMethod(Qt::QueuedConnection) which achieves the same deferred invocation without allocating a QTimer on the heap each time. In a large sync with many directories, this eliminates thousands of unnecessary heap allocations. Signed-off-by: Qoole <2862661+qoole@users.noreply.github.com>
1 parent 9ac8d75 commit d94f294

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/libsync/discovery.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void ProcessDirectoryJob::process()
250250
processFile(std::move(path), e.localEntry, e.serverEntry, e.dbEntry);
251251
}
252252
_discoveryData->_listExclusiveFiles.clear();
253-
QTimer::singleShot(0, _discoveryData, &DiscoveryPhase::scheduleMoreJobs);
253+
QMetaObject::invokeMethod(_discoveryData, &DiscoveryPhase::scheduleMoreJobs, Qt::QueuedConnection);
254254
}
255255

256256
bool ProcessDirectoryJob::handleExcluded(const QString &path, const Entries &entries, const std::map<QString, Entries> &allEntries, const bool isHidden, const bool isBlacklisted)
@@ -304,9 +304,10 @@ bool ProcessDirectoryJob::handleExcluded(const QString &path, const Entries &ent
304304
}
305305

306306
const auto &localName = entries.localEntry.name;
307-
const auto splitName = localName.split('.');
308-
const auto &baseName = splitName.first();
309-
const auto extension = splitName.size() > 1 ? splitName.last() : QString();
307+
const auto firstDotIndex = localName.indexOf(QLatin1Char('.'));
308+
const auto lastDotIndex = localName.lastIndexOf(QLatin1Char('.'));
309+
const auto baseName = firstDotIndex >= 0 ? localName.left(firstDotIndex) : localName;
310+
const auto extension = lastDotIndex >= 0 ? localName.mid(lastDotIndex + 1) : QString();
310311
const auto accountCaps = _discoveryData->_account->capabilities();
311312
const auto forbiddenFilenames = accountCaps.forbiddenFilenames();
312313
const auto forbiddenBasenames = accountCaps.forbiddenFilenameBasenames();
@@ -699,7 +700,7 @@ void ProcessDirectoryJob::postProcessServerNew(const SyncFileItemPtr &item,
699700
if (!result) {
700701
processFileAnalyzeLocalInfo(item, path, localEntry, serverEntry, dbEntry, _queryServer);
701702
}
702-
QTimer::singleShot(0, _discoveryData, &DiscoveryPhase::scheduleMoreJobs);
703+
QMetaObject::invokeMethod(_discoveryData, &DiscoveryPhase::scheduleMoreJobs, Qt::QueuedConnection);
703704
});
704705
return;
705706
}
@@ -1063,7 +1064,7 @@ void ProcessDirectoryJob::processFileAnalyzeRemoteInfo(const SyncFileItemPtr &it
10631064
const auto job = new RequestEtagJob(_discoveryData->_account, _discoveryData->_remoteFolder + originalPath, this);
10641065
connect(job, &RequestEtagJob::finishedWithResult, this, [=, this](const HttpResult<QByteArray> &etag) mutable {
10651066
_pendingAsyncJobs--;
1066-
QTimer::singleShot(0, _discoveryData, &DiscoveryPhase::scheduleMoreJobs);
1067+
QMetaObject::invokeMethod(_discoveryData, &DiscoveryPhase::scheduleMoreJobs, Qt::QueuedConnection);
10671068
if (etag || etag.error().code != 404 ||
10681069
// Somehow another item claimed this original path, consider as if it existed
10691070
_discoveryData->isRenamed(originalPath)) {
@@ -1714,7 +1715,7 @@ void ProcessDirectoryJob::processFileAnalyzeLocalInfo(
17141715
}
17151716
processFileFinalize(item, path, item->isDirectory(), NormalQuery, recurseQueryServer);
17161717
_pendingAsyncJobs--;
1717-
QTimer::singleShot(0, _discoveryData, &DiscoveryPhase::scheduleMoreJobs);
1718+
QMetaObject::invokeMethod(_discoveryData, &DiscoveryPhase::scheduleMoreJobs, Qt::QueuedConnection);
17181719
});
17191720
job->start();
17201721
return;
@@ -2194,7 +2195,7 @@ void ProcessDirectoryJob::subJobFinished()
21942195
int count = _runningJobs.removeAll(job);
21952196
ASSERT(count == 1);
21962197
job->deleteLater();
2197-
QTimer::singleShot(0, _discoveryData, &DiscoveryPhase::scheduleMoreJobs);
2198+
QMetaObject::invokeMethod(_discoveryData, &DiscoveryPhase::scheduleMoreJobs, Qt::QueuedConnection);
21982199
}
21992200

22002201
int ProcessDirectoryJob::processSubJobs(int nbJobs)

0 commit comments

Comments
 (0)