]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Sort in items in two stages to speed up natural sorting
authorFrank Reininghaus <frank78ac@googlemail.com>
Wed, 30 Oct 2013 16:48:32 +0000 (17:48 +0100)
committerFrank Reininghaus <frank78ac@googlemail.com>
Wed, 30 Oct 2013 16:48:42 +0000 (17:48 +0100)
Sort the items in a folder first according to their name, without doing
a natural/locale-aware sorting. This is very fast, but the order of the
items is then already close to the final order in most cases.

The number of expensive natural comparisons required to sort the items
is thus greatly reduced.

In my experiments with a folder with 100,000 items, the time required
to sort the files was reduced by 63% with this patch.

REVIEW: 113485

src/kitemviews/kfileitemmodel.cpp
src/kitemviews/kfileitemmodel.h

index f21edbf4a85d7d428bda2d75328697626afdc188..e2d0413a26b00baa6ac277cf0c7c140df692411a 100644 (file)
@@ -1036,6 +1036,14 @@ void KFileItemModel::insertItems(QList<ItemData*>& newItems)
 
     m_groups.clear();
 
 
     m_groups.clear();
 
+    if (m_naturalSorting) {
+        // Natural sorting of items can be very slow. However, it becomes much
+        // faster if the input sequence is already mostly sorted. Therefore, we
+        // first sort 'newItems' according to the QStrings returned by
+        // KFileItem::text() using QString::operator<(), which is quite fast.
+        parallelMergeSort(newItems.begin(), newItems.end(), nameLessThan, QThread::idealThreadCount());
+    }
+
     sort(newItems.begin(), newItems.end());
 
 #ifdef KFILEITEMMODEL_DEBUG
     sort(newItems.begin(), newItems.end());
 
 #ifdef KFILEITEMMODEL_DEBUG
index d005705492e1a0feb4092808eca2f488ac0143ae..4b50477d985cbac05e05c2cbc5d1b0c3214cd1f6 100644 (file)
@@ -349,6 +349,12 @@ private:
 
     QHash<QByteArray, QVariant> retrieveData(const KFileItem& item, const ItemData* parent) const;
 
 
     QHash<QByteArray, QVariant> retrieveData(const KFileItem& item, const ItemData* parent) const;
 
+    /**
+     * @return True if \a a has a KFileItem whose text is 'less than' the one
+     *         of \a b according to QString::operator<(const QString&).
+     */
+    static bool nameLessThan(const ItemData* a, const ItemData* b);
+
     /**
      * @return True if the item-data \a a should be ordered before the item-data
      *         \b. The item-data may have different parent-items.
     /**
      * @return True if the item-data \a a should be ordered before the item-data
      *         \b. The item-data may have different parent-items.
@@ -485,6 +491,12 @@ private:
     friend class DolphinPart;                  // Accesses m_dirLister
 };
 
     friend class DolphinPart;                  // Accesses m_dirLister
 };
 
+inline bool KFileItemModel::nameLessThan(const ItemData* a, const ItemData* b)
+{
+    return a->item.text() < b->item.text();
+}
+
+
 inline bool KFileItemModel::isChildItem(int index) const
 {
     if (m_itemData.at(index)->parent) {
 inline bool KFileItemModel::isChildItem(int index) const
 {
     if (m_itemData.at(index)->parent) {