From 04b6fb54c32da26243e90a78ea7c39faddd7f01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Mon, 30 Oct 2023 11:03:46 +0100 Subject: [PATCH] feat(LeftJoinModel): Use QAbstractItemModel::match for searching key idx in right model It creates a possibility to transparently improve performance of id lookup in right model by implementing fast search on given role in the righ model itself by reimplementing QAbstractItemModel::match. Closes: #12574 --- ui/StatusQ/src/leftjoinmodel.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/ui/StatusQ/src/leftjoinmodel.cpp b/ui/StatusQ/src/leftjoinmodel.cpp index d4966cfbd1..52884b51bf 100644 --- a/ui/StatusQ/src/leftjoinmodel.cpp +++ b/ui/StatusQ/src/leftjoinmodel.cpp @@ -129,7 +129,7 @@ QVariant LeftJoinModel::data(const QModelIndex& index, int role) const if (m_rightModelDestroyed) return {}; - QVariant joinRoleLeftValue = m_leftModel->data(idx, m_leftModelJoinRole); + auto joinRoleLeftValue = m_leftModel->data(idx, m_leftModelJoinRole); if (m_lastUsedRightModelIndex.isValid() && m_rightModel->data(m_lastUsedRightModelIndex, @@ -139,22 +139,15 @@ QVariant LeftJoinModel::data(const QModelIndex& index, int role) const role - m_rightModelRolesOffset); } - int rightModelCount = m_rightModel->rowCount(); + QModelIndexList match = m_rightModel->match( + m_rightModel->index(0, 0), m_rightModelJoinRole, + joinRoleLeftValue, 1, Qt::MatchExactly); - for (int i = 0; i < rightModelCount; i++) { - auto rightModelIdx = m_rightModel->index(i, 0); - auto rightJointRoleValue = m_rightModel->data(rightModelIdx, - m_rightModelJoinRole); + if (match.empty()) + return {}; - if (joinRoleLeftValue == rightJointRoleValue) { - m_lastUsedRightModelIndex = rightModelIdx; - - return m_rightModel->data(rightModelIdx, - role - m_rightModelRolesOffset); - } - } - - return {}; + m_lastUsedRightModelIndex = match.first(); + return match.first().data(role - m_rightModelRolesOffset); } void LeftJoinModel::setLeftModel(QAbstractItemModel* model)