diff options
author | David Faure <david.faure@kdab.com> | 2017-07-28 11:26:30 +0200 |
---|---|---|
committer | David Faure <david.faure@kdab.com> | 2017-08-10 14:50:29 +0000 |
commit | 60ad66ce3c32b389d9e2b66722e18717ddfcfd4e (patch) | |
tree | d5035d6fbc072be6575f5a42138c48dd260c6da7 /src/sql | |
parent | 74bbb6ba8a402b8a480fca480c94160d5500b466 (diff) |
QSqlTableModel: avoid copying QSqlRecord instances
Profiling a large model showed that QSqlRecord was being copied and
destroyed from a few places, due to use of QMap<int, ModifiedRow>::value()
where ModifiedRow has two QSqlRecord members. This can easily be avoided
(in the common case with no modified rows) by using constFind() instead.
My testcase (iterating over a model with 126936 rows) went from 1266 ms
to 896 ms.
Change-Id: I04e98b5573ef24165bf6cff19946e5bedd0fb0ba
Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/sql')
-rw-r--r-- | src/sql/models/qsqltablemodel.cpp | 14 | ||||
-rw-r--r-- | src/sql/models/qsqltablemodel_p.h | 2 |
2 files changed, 10 insertions, 6 deletions
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index c0706ac22d..05feb87466 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -477,9 +477,9 @@ QVariant QSqlTableModel::data(const QModelIndex &index, int role) const if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole)) return QVariant(); - const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row()); - if (mrow.op() != QSqlTableModelPrivate::None) - return mrow.rec().value(index.column()); + const auto it = d->cache.constFind(index.row()); + if (it != d->cache.constEnd() && it->op() != QSqlTableModelPrivate::None) + return it->rec().value(index.column()); return QSqlQueryModel::data(index, role); } @@ -532,7 +532,10 @@ bool QSqlTableModel::isDirty(const QModelIndex &index) const if (!index.isValid()) return false; - const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); + const auto it = d->cache.constFind(index.row()); + if (it == d->cache.constEnd()) + return false; + const QSqlTableModelPrivate::ModifiedRow &row = *it; if (row.submitted()) return false; @@ -1231,7 +1234,8 @@ int QSqlTableModel::rowCount(const QModelIndex &parent) const QModelIndex QSqlTableModel::indexInQuery(const QModelIndex &item) const { Q_D(const QSqlTableModel); - if (d->cache.value(item.row()).insert()) + const auto it = d->cache.constFind(item.row()); + if (it != d->cache.constEnd() && it->insert()) return QModelIndex(); const int rowOffset = d->insertCount(item.row()); diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h index 3b64cdfa47..490bb48a24 100644 --- a/src/sql/models/qsqltablemodel_p.h +++ b/src/sql/models/qsqltablemodel_p.h @@ -117,7 +117,7 @@ public: m_rec = m_db_values; setGenerated(m_rec, m_op == Delete); } - inline QSqlRecord rec() const { return m_rec; } + inline const QSqlRecord &rec() const { return m_rec; } inline QSqlRecord& recRef() { return m_rec; } inline void setValue(int c, const QVariant &v) { |