Optimize QString::section(QReg*Exp*)

Instead of creating lots of temporary QString objects, use QStringRefs.

Together with the two other (micro) optimizations before, this speeds
up section() by up to 2x (section_regex(IPv6)).

No test has become slower.

Change-Id: I514667004f82ddc1518fe3ee699ec5fbf96bb82f
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@theqtcompany.com>
This commit is contained in:
Marc Mutz 2014-08-13 21:26:59 +02:00
parent 4bd81e0b82
commit c4ed23b3e0

View File

@ -4049,9 +4049,9 @@ QString QString::section(const QString &sep, int start, int end, SectionFlags fl
class qt_section_chunk { class qt_section_chunk {
public: public:
qt_section_chunk() {} qt_section_chunk() {}
qt_section_chunk(int l, QString s) : length(l), string(qMove(s)) {} qt_section_chunk(int l, QStringRef s) : length(l), string(qMove(s)) {}
int length; int length;
QString string; QStringRef string;
}; };
Q_DECLARE_TYPEINFO(qt_section_chunk, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(qt_section_chunk, Q_MOVABLE_TYPE);
@ -4144,12 +4144,12 @@ QString QString::section(const QRegExp &reg, int start, int end, SectionFlags fl
QVector<qt_section_chunk> sections; QVector<qt_section_chunk> sections;
int n = length(), m = 0, last_m = 0, last_len = 0; int n = length(), m = 0, last_m = 0, last_len = 0;
while ((m = sep.indexIn(*this, m)) != -1) { while ((m = sep.indexIn(*this, m)) != -1) {
sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m))); sections.append(qt_section_chunk(last_len, QStringRef(this, last_m, m - last_m)));
last_m = m; last_m = m;
last_len = sep.matchedLength(); last_len = sep.matchedLength();
m += qMax(sep.matchedLength(), 1); m += qMax(sep.matchedLength(), 1);
} }
sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m))); sections.append(qt_section_chunk(last_len, QStringRef(this, last_m, n - last_m)));
return extractSections(sections, start, end, flags); return extractSections(sections, start, end, flags);
} }
@ -4192,11 +4192,11 @@ QString QString::section(const QRegularExpression &re, int start, int end, Secti
while (iterator.hasNext()) { while (iterator.hasNext()) {
QRegularExpressionMatch match = iterator.next(); QRegularExpressionMatch match = iterator.next();
m = match.capturedStart(); m = match.capturedStart();
sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m))); sections.append(qt_section_chunk(last_len, QStringRef(this, last_m, m - last_m)));
last_m = m; last_m = m;
last_len = match.capturedLength(); last_len = match.capturedLength();
} }
sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m))); sections.append(qt_section_chunk(last_len, QStringRef(this, last_m, n - last_m)));
return extractSections(sections, start, end, flags); return extractSections(sections, start, end, flags);
} }