]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Faster drag&drop in directories with thousands of files
authorJaime Torres <jtamate@gmail.com>
Sun, 4 Feb 2018 20:19:33 +0000 (21:19 +0100)
committerJaime Torres <jtamate@gmail.com>
Mon, 5 Feb 2018 18:25:44 +0000 (19:25 +0100)
Summary:
The check is called when the mouse is moved in a drag&drop operation.

Dragging all files in a directory with 3000 files under callgrind,
moving the mouse to the other panel and then canceling, doing it twice,
callgrind shows that the method urlListMatchesUrl is called around
200 times, spending around 9,30% of the cpu in those calls.
Applying the patch, callgrind tells it uses now 0.31% of the cpu in 1208 calls.

CCBUG: 342056

Reviewers: #dolphin, elvisangelaccio, markg

Reviewed By: #dolphin, elvisangelaccio, markg

Subscribers: markg, anthonyfieroni, michaelh, elvisangelaccio, ngraham

Differential Revision: https://phabricator.kde.org/D10085

src/kitemviews/kitemlistcontroller.cpp
src/views/draganddrophelper.cpp
src/views/draganddrophelper.h

index 1309364884ee5fa708492c50c7e15fc26b5a0850..7af8781b4835be613446009cca7b6acf50acf133 100644 (file)
@@ -849,6 +849,9 @@ bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, con
 {
     Q_UNUSED(event);
     Q_UNUSED(transform);
+
+    DragAndDropHelper::clearUrlListMatchesUrlCache();
+
     return false;
 }
 
index e944227df9f9859d01382041202b821145c842b0..4d76992cadcda90f49f8f97bed5a6e0e34ac5cff 100644 (file)
 #include <KIO/DropJob>
 #include <KJobWidgets>
 
+QHash<QUrl, bool> DragAndDropHelper::m_urlListMatchesUrlCache;
 
 bool DragAndDropHelper::urlListMatchesUrl(const QList<QUrl>& urls, const QUrl& destUrl)
 {
-    return std::find_if(urls.constBegin(), urls.constEnd(), [destUrl](const QUrl& url) {
-        return url.matches(destUrl, QUrl::StripTrailingSlash);
-    }) != urls.constEnd();
+    auto iteratorResult = m_urlListMatchesUrlCache.constFind(destUrl);
+    if (iteratorResult != m_urlListMatchesUrlCache.constEnd()) {
+        return *iteratorResult;
+    }
+
+    const bool destUrlMatches =
+        std::find_if(urls.constBegin(), urls.constEnd(), [destUrl](const QUrl& url) {
+            return url.matches(destUrl, QUrl::StripTrailingSlash);
+        }) != urls.constEnd();
+
+    return *m_urlListMatchesUrlCache.insert(destUrl, destUrlMatches);
 }
 
 KIO::DropJob* DragAndDropHelper::dropUrls(const QUrl& destUrl, QDropEvent* event, QWidget* window)
@@ -63,3 +72,8 @@ KIO::DropJob* DragAndDropHelper::dropUrls(const QUrl& destUrl, QDropEvent* event
     return nullptr;
 }
 
+void DragAndDropHelper::clearUrlListMatchesUrlCache()
+{
+    DragAndDropHelper::m_urlListMatchesUrlCache.clear();
+}
+
index e47f83ca83ce4aa309bd2c3200ce74d8239ab975..0c7eadc6cb95c6f8b862a4757c815d887b441533 100644 (file)
@@ -54,6 +54,16 @@ public:
      * @return True if destUrl is contained in the urls parameter.
      */
     static bool urlListMatchesUrl(const QList<QUrl>& urls, const QUrl& destUrl);
+
+    /**
+     * clear the internal cache.
+     */
+    static void clearUrlListMatchesUrlCache();
+private:
+    /**
+     * Stores the results of the expensive checks made in urlListMatchesUrl.
+     */
+    static QHash<QUrl, bool> m_urlListMatchesUrlCache;
 };
 
 #endif