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>
bb10
David Faure 2017-07-28 11:26:30 +02:00
parent 74bbb6ba8a
commit 60ad66ce3c
2 changed files with 10 additions and 6 deletions

View File

@ -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());

View File

@ -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)
{