From 60ad66ce3c32b389d9e2b66722e18717ddfcfd4e Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 28 Jul 2017 11:26:30 +0200 Subject: 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::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 Reviewed-by: Edward Welbourne --- src/sql/models/qsqltablemodel.cpp | 14 +++++++++----- 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) { -- cgit v1.2.3