forked from qt-creator/qt-creator
TreeModel/Find: Save a few cycles when searching
Calls parent() only ~two out of three times and remove some function calls in the TreeModel::parent() implementation, reducing the overall footprint of this bit from ~5 to ~1.5% when searching something in the project tree (test case was Creator-in-Creator, and searching for 'dddddddddd', resulting in 8 complete scans of the tree. Task-number: QTCREATORBUG-17956 Change-Id: I4e46ef0467dd2aea58a7c944e1a2ee5c01e6fbba Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -919,11 +919,9 @@ QModelIndex BaseTreeModel::parent(const QModelIndex &idx) const
|
|||||||
if (!grandparent)
|
if (!grandparent)
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
|
|
||||||
for (int i = 0, n = grandparent->childCount(); i < n; ++i)
|
// This is on the performance-critical path for ItemViewFind.
|
||||||
if (grandparent->childAt(i) == parent)
|
const int i = grandparent->m_children.indexOf(parent);
|
||||||
return createIndex(i, 0, static_cast<void*>(parent));
|
return createIndex(i, 0, static_cast<void*>(parent));
|
||||||
|
|
||||||
return QModelIndex();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int BaseTreeModel::rowCount(const QModelIndex &idx) const
|
int BaseTreeModel::rowCount(const QModelIndex &idx) const
|
||||||
|
@@ -66,7 +66,7 @@ public:
|
|||||||
|
|
||||||
using const_iterator = QVector<TreeItem *>::const_iterator;
|
using const_iterator = QVector<TreeItem *>::const_iterator;
|
||||||
using value_type = TreeItem *;
|
using value_type = TreeItem *;
|
||||||
int childCount() const { return end() - begin(); }
|
int childCount() const { return m_children.size(); }
|
||||||
int indexInParent() const;
|
int indexInParent() const;
|
||||||
TreeItem *childAt(int index) const;
|
TreeItem *childAt(int index) const;
|
||||||
int indexOf(const TreeItem *item) const;
|
int indexOf(const TreeItem *item) const;
|
||||||
|
@@ -249,12 +249,13 @@ QModelIndex ItemViewFind::nextIndex(const QModelIndex &idx, bool *wrapped) const
|
|||||||
return model->index(0, 0);
|
return model->index(0, 0);
|
||||||
|
|
||||||
// same parent has more columns, go to next column
|
// same parent has more columns, go to next column
|
||||||
if (idx.column() + 1 < model->columnCount(idx.parent()))
|
const QModelIndex parentIdx = idx.parent();
|
||||||
return model->index(idx.row(), idx.column() + 1, idx.parent());
|
if (idx.column() + 1 < model->columnCount(parentIdx))
|
||||||
|
return model->index(idx.row(), idx.column() + 1, parentIdx);
|
||||||
|
|
||||||
// tree views have their children attached to first column
|
// tree views have their children attached to first column
|
||||||
// make sure we are at first column
|
// make sure we are at first column
|
||||||
QModelIndex current = model->index(idx.row(), 0, idx.parent());
|
QModelIndex current = model->index(idx.row(), 0, parentIdx);
|
||||||
|
|
||||||
// check for children
|
// check for children
|
||||||
if (d->m_option == FetchMoreWhileSearching && model->canFetchMore(current))
|
if (d->m_option == FetchMoreWhileSearching && model->canFetchMore(current))
|
||||||
|
Reference in New Issue
Block a user