QSqlRecord: (re)speedup indexOf(const QString& name)

While adding the possibility to access values for QSqlRecord with
decorated field names (table.field), some string-allocations were added
which created a remarkable slowdown. Replace the QString allocations
with QStringRef avoids those allocations and restores the speed for
normal operations (apart from on QString::indexOf() call and some
integer comparisons)

Task-number: QTBUG-65226
Change-Id: I9e458523891421abce9e4a7ed931fec000dcbe76
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
bb10
Christian Ehrlicher 2017-12-24 14:13:39 +01:00
parent 1e75dcf251
commit b739b3a040
1 changed files with 8 additions and 7 deletions

View File

@ -232,18 +232,19 @@ QString QSqlRecord::fieldName(int index) const
int QSqlRecord::indexOf(const QString& name) const
{
QString tableName;
QString fieldName = name;
QStringRef tableName;
QStringRef fieldName(&name);
const int idx = name.indexOf(QLatin1Char('.'));
if (idx != -1) {
tableName = name.left(idx);
fieldName = name.mid(idx + 1);
tableName = name.leftRef(idx);
fieldName = name.midRef(idx + 1);
}
for (int i = 0; i < count(); ++i) {
const int cnt = count();
for (int i = 0; i < cnt; ++i) {
// Check the passed in name first in case it is an alias using a dot.
// Then check if both the table and field match when there is a table name specified.
const auto currentField = d->fields.at(i);
const auto currentFieldName = currentField.name();
const auto &currentField = d->fields.at(i);
const auto &currentFieldName = currentField.name();
if (currentFieldName.compare(name, Qt::CaseInsensitive) == 0
|| (idx != -1 && currentFieldName.compare(fieldName, Qt::CaseInsensitive) == 0
&& currentField.tableName().compare(tableName, Qt::CaseInsensitive) == 0)) {