Implemented multithreading in KFileItemModelSortAlgorithm. Use as many threads as possible.

Thanks to Frank Reininghaus for all his suggestions.

REVIEW: 107025
This commit is contained in:
Emmanuel Pescosta 2012-10-28 17:58:06 +01:00
parent 299c8332dd
commit 429218eaf2
2 changed files with 42 additions and 0 deletions

View file

@ -19,9 +19,20 @@
#include "kfileitemmodelsortalgorithm.h" #include "kfileitemmodelsortalgorithm.h"
#include <QThread>
#include <QtCore>
void KFileItemModelSortAlgorithm::sort(KFileItemModel* model, void KFileItemModelSortAlgorithm::sort(KFileItemModel* model,
QList<KFileItemModel::ItemData*>::iterator begin, QList<KFileItemModel::ItemData*>::iterator begin,
QList<KFileItemModel::ItemData*>::iterator end) QList<KFileItemModel::ItemData*>::iterator end)
{
static const int numberOfThreads = QThread::idealThreadCount();
parallelSort(model, begin, end, numberOfThreads);
}
void KFileItemModelSortAlgorithm::sequentialSort(KFileItemModel* model,
QList< KFileItemModel::ItemData* >::iterator begin,
QList< KFileItemModel::ItemData* >::iterator end)
{ {
// The implementation is based on qStableSortHelper() from qalgorithms.h // The implementation is based on qStableSortHelper() from qalgorithms.h
// Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
@ -37,6 +48,28 @@ void KFileItemModelSortAlgorithm::sort(KFileItemModel* model,
merge(model, begin, middle, end); merge(model, begin, middle, end);
} }
void KFileItemModelSortAlgorithm::parallelSort(KFileItemModel* model,
QList< KFileItemModel::ItemData* >::iterator begin,
QList< KFileItemModel::ItemData* >::iterator end,
const int numberOfThreads)
{
const int span = end - begin;
if (numberOfThreads > 1 && span > 100) {
const int newNumberOfThreads = numberOfThreads / 2;
const QList<KFileItemModel::ItemData*>::iterator middle = begin + span / 2;
QFuture<void> future = QtConcurrent::run(parallelSort, model, begin, middle, newNumberOfThreads);
parallelSort(model, middle, end, newNumberOfThreads);
future.waitForFinished();
merge(model, begin, middle, end);
} else {
sequentialSort(model, begin, end);
}
}
void KFileItemModelSortAlgorithm::merge(KFileItemModel* model, void KFileItemModelSortAlgorithm::merge(KFileItemModel* model,
QList<KFileItemModel::ItemData*>::iterator begin, QList<KFileItemModel::ItemData*>::iterator begin,
QList<KFileItemModel::ItemData*>::iterator pivot, QList<KFileItemModel::ItemData*>::iterator pivot,

View file

@ -44,6 +44,15 @@ public:
QList<KFileItemModel::ItemData*>::iterator end); QList<KFileItemModel::ItemData*>::iterator end);
private: private:
static void sequentialSort(KFileItemModel* model,
QList<KFileItemModel::ItemData*>::iterator begin,
QList<KFileItemModel::ItemData*>::iterator end);
static void parallelSort(KFileItemModel* model,
QList<KFileItemModel::ItemData*>::iterator begin,
QList<KFileItemModel::ItemData*>::iterator end,
const int numberOfThreads);
static void merge(KFileItemModel* model, static void merge(KFileItemModel* model,
QList<KFileItemModel::ItemData*>::iterator begin, QList<KFileItemModel::ItemData*>::iterator begin,
QList<KFileItemModel::ItemData*>::iterator pivot, QList<KFileItemModel::ItemData*>::iterator pivot,