diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-06-24 02:04:09 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-02-17 19:45:02 +0000 |
commit | aae362e2fe3c0a71dd6d01eaf7f20085506ef7f4 (patch) | |
tree | 57fc11276ce07e950dc0e89161faf4528098b100 /src/widgets | |
parent | 86c5a337e3b123f2e41c8b15b416f39655519acd (diff) | |
download | qtbase-aae362e2fe3c0a71dd6d01eaf7f20085506ef7f4.tar.gz |
QDirModel: replace an inefficient QList with QVector
SavedPersistent is larger than a void*, so holding it in QLists
is needlessly inefficient. Worse, the code could come to depend
on the fragile property of (inefficient) QLists that references
to elements therein never are invalidated.
Fix by marking it movable and holding it in a QVector instead.
Take advantage of rvalue-enabled QVector::push_back(); optimize
element construction by using aggregate initialization.
Change-Id: I4fd88879aa13e6536d59d164b3c33fbc2fead77f
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/itemviews/qdirmodel.cpp | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/widgets/itemviews/qdirmodel.cpp b/src/widgets/itemviews/qdirmodel.cpp index 37ba5a59f8..dfb1d7619e 100644 --- a/src/widgets/itemviews/qdirmodel.cpp +++ b/src/widgets/itemviews/qdirmodel.cpp @@ -136,11 +136,12 @@ public: QPersistentModelIndexData *data; QPersistentModelIndex index; }; - QList<SavedPersistent> savedPersistent; + QVector<SavedPersistent> savedPersistent; QPersistentModelIndex toBeRefreshed; bool shouldStat; // use the "carefull not to stat directories" mode }; +Q_DECLARE_TYPEINFO(QDirModelPrivate::SavedPersistent, Q_MOVABLE_TYPE); void qt_setDirModelShouldNotStat(QDirModelPrivate *modelPrivate) { @@ -1237,14 +1238,16 @@ void QDirModelPrivate::savePersistentIndexes() { Q_Q(QDirModel); savedPersistent.clear(); + savedPersistent.reserve(persistent.indexes.size()); foreach (QPersistentModelIndexData *data, persistent.indexes) { - SavedPersistent saved; QModelIndex index = data->index; - saved.path = q->filePath(index); - saved.column = index.column(); - saved.data = data; - saved.index = index; - savedPersistent.append(saved); + SavedPersistent saved = { + q->filePath(index), + index.column(), + data, + index, + }; + savedPersistent.push_back(std::move(saved)); } } @@ -1253,11 +1256,9 @@ void QDirModelPrivate::restorePersistentIndexes() Q_Q(QDirModel); bool allow = allowAppendChild; allowAppendChild = false; - for (int i = 0; i < savedPersistent.count(); ++i) { - QPersistentModelIndexData *data = savedPersistent.at(i).data; - QString path = savedPersistent.at(i).path; - int column = savedPersistent.at(i).column; - QModelIndex idx = q->index(path, column); + for (const SavedPersistent &sp : qAsConst(savedPersistent)) { + QPersistentModelIndexData *data = sp.data; + QModelIndex idx = q->index(sp.path, sp.column); if (idx != data->index || data->model == 0) { //data->model may be equal to 0 if the model is getting destroyed persistent.indexes.remove(data->index); |