Skip to content

Commit

Permalink
Add simple benchmark for QRegularExpression
Browse files Browse the repository at this point in the history
The benchmark simply calls QRegularExpression's public API methods, so
that we can assess how changes to the implication impact performance.
(Its addition is prompted by evaluation of whether adding a move
constructor saves more or less than the resulting need for
d-pointer null-checks costs.)

Task-number: QTBUG-86634
Change-Id: Idef775ef6cf9f9ded3ce7ba5b85e460571d12756
Reviewed-by: Edward Welbourne <[email protected]>
  • Loading branch information
isolovev committed Jan 20, 2021
1 parent 0875626 commit fb51b47
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/benchmarks/corelib/text/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_subdirectory(qchar)
add_subdirectory(qlocale)
add_subdirectory(qstringbuilder)
add_subdirectory(qstringlist)
add_subdirectory(qregularexpression)
if(GCC)
add_subdirectory(qstring)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tst_bench_qregularexpression
12 changes: 12 additions & 0 deletions tests/benchmarks/corelib/text/qregularexpression/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated from qregularexpression.pro.

#####################################################################
## tst_bench_qregularexpression Binary:
#####################################################################

qt_internal_add_benchmark(tst_bench_qregularexpression
SOURCES
tst_bench_qregularexpression.cpp
PUBLIC_LIBRARIES
Qt::Test
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONFIG += benchmark
QT = core testlib

TARGET = tst_bench_qregularexpression
SOURCES += tst_bench_qregularexpression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QRegularExpression>
#include <QTest>

/*!
\internal
The main idea of the benchmark is to compare performance of QRE classes
before and after any changes in the implementation.
It does not try to compare performance between different patterns or
matching options.
*/

static const QString textToMatch { "The quick brown fox jumped over the lazy dogs" };
static const QString nonEmptyPattern { "(?<article>\\w+) (?<noun>\\w+)" };
static const QRegularExpression::PatternOptions nonEmptyPatternOptions {
QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption
};

class tst_QRegularExpressionBenchmark : public QObject
{
Q_OBJECT

private slots:
void createDefault();
void createAndMoveDefault();

void createCustom();
void createAndMoveCustom();

void matchDefault();
void matchDefaultOptimized();

void matchCustom();
void matchCustomOptimized();

void globalMatchDefault();
void globalMatchDefaultOptimized();

void globalMatchCustom();
void globalMatchCustomOptimized();

void queryMatchResults();
void queryMatchResultsByGroupIndex();
void queryMatchResultsByGroupName();
void iterateThroughGlobalMatchResults();
};

void tst_QRegularExpressionBenchmark::createDefault()
{
QBENCHMARK {
QRegularExpression re;
Q_UNUSED(re);
}
}

void tst_QRegularExpressionBenchmark::createAndMoveDefault()
{
QBENCHMARK {
QRegularExpression re;
// We can compare to results of previous test to roughly
// estimate the cost for the move() call.
QRegularExpression re2(std::move(re));
Q_UNUSED(re2);
}
}

void tst_QRegularExpressionBenchmark::createCustom()
{
QBENCHMARK {
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
Q_UNUSED(re);
}
}

void tst_QRegularExpressionBenchmark::createAndMoveCustom()
{
QBENCHMARK {
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
// We can compare to results of previous test to roughly
// estimate the cost for the move() call.
QRegularExpression re2(std::move(re));
Q_UNUSED(re2);
}
}

/*!
\internal This benchmark measures the performance of the match() together
with pattern compilation for a default-constructed object.
We need to create the object every time, so that the compiled pattern
does not get cached.
*/
void tst_QRegularExpressionBenchmark::matchDefault()
{
QBENCHMARK {
QRegularExpression re;
auto matchResult = re.match(textToMatch);
Q_UNUSED(matchResult);
}
}

/*!
\internal This benchmark measures the performance of the match() without
pattern compilation for a default-constructed object.
The pattern is precompiled before the actual benchmark starts.
\note In case we make the default constructor non-allocating, the results
of the benchmark will be very close to the unoptimized one, as it will have
to compile the pattern inside the call to match().
*/
void tst_QRegularExpressionBenchmark::matchDefaultOptimized()
{
QRegularExpression re;
re.optimize();
QBENCHMARK {
auto matchResult = re.match(textToMatch);
Q_UNUSED(matchResult);
}
}

/*!
\internal This benchmark measures the performance of the match() together
with pattern compilation for an object with custom pattern and pattern
options.
We need to create the object every time, so that the compiled pattern
does not get cached.
*/
void tst_QRegularExpressionBenchmark::matchCustom()
{
QBENCHMARK {
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
auto matchResult = re.match(textToMatch);
Q_UNUSED(matchResult);
}
}

/*!
\internal This benchmark measures the performance of the match() without
pattern compilation for an object with custom pattern and pattern
options.
The pattern is precompiled before the actual benchmark starts.
*/
void tst_QRegularExpressionBenchmark::matchCustomOptimized()
{
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
re.optimize();
QBENCHMARK {
auto matchResult = re.match(textToMatch);
Q_UNUSED(matchResult);
}
}

/*!
\internal This benchmark measures the performance of the globalMatch()
together with the pattern compilation for a default-constructed object.
We need to create the object every time, so that the compiled pattern
does not get cached.
*/
void tst_QRegularExpressionBenchmark::globalMatchDefault()
{
QBENCHMARK {
QRegularExpression re;
auto matchResultIterator = re.globalMatch(textToMatch);
Q_UNUSED(matchResultIterator);
}
}

/*!
\internal This benchmark measures the performance of the globalMatch()
without the pattern compilation for a default-constructed object.
The pattern is precompiled before the actual benchmark starts.
\note In case we make the default constructor non-allocating, the results
of the benchmark will be very close to the unoptimized one, as it will have
to compile the pattern inside the call to globalMatch().
*/
void tst_QRegularExpressionBenchmark::globalMatchDefaultOptimized()
{
QRegularExpression re;
re.optimize();
QBENCHMARK {
auto matchResultIterator = re.globalMatch(textToMatch);
Q_UNUSED(matchResultIterator);
}
}

/*!
\internal This benchmark measures the performance of the globalMatch()
together with the pattern compilation for an object with custom pattern
and pattern options.
We need to create the object every time, so that the compiled pattern
does not get cached.
*/
void tst_QRegularExpressionBenchmark::globalMatchCustom()
{
QBENCHMARK {
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
auto matchResultIterator = re.globalMatch(textToMatch);
Q_UNUSED(matchResultIterator);
}
}

/*!
\internal This benchmark measures the performance of the globalMatch()
without the pattern compilation for an object with custom pattern and
pattern options.
The pattern is precompiled before the actual benchmark starts.
*/
void tst_QRegularExpressionBenchmark::globalMatchCustomOptimized()
{
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
re.optimize();
QBENCHMARK {
auto matchResultIterator = re.globalMatch(textToMatch);
Q_UNUSED(matchResultIterator);
}
}

void tst_QRegularExpressionBenchmark::queryMatchResults()
{
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
auto matchResult = re.match(textToMatch);
QBENCHMARK {
auto texts = matchResult.capturedTexts();
Q_UNUSED(texts);
}
}

void tst_QRegularExpressionBenchmark::queryMatchResultsByGroupIndex()
{
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
auto matchResult = re.match(textToMatch);
const int capturedCount = matchResult.lastCapturedIndex();
QBENCHMARK {
for (int i = 0, imax = capturedCount; i < imax; ++i) {
auto result = matchResult.capturedView(i);
Q_UNUSED(result);
}
}
}

void tst_QRegularExpressionBenchmark::queryMatchResultsByGroupName()
{
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
auto matchResult = re.match(textToMatch);
const auto groups = matchResult.regularExpression().namedCaptureGroups();
QBENCHMARK {
for (const QString &groupName : groups) {
// introduce this checks to get rid of tons of warnings.
// The result for empty string is always the same.
if (!groupName.isEmpty()) {
auto result = matchResult.capturedView(groupName);
Q_UNUSED(result);
}
}
}
}

void tst_QRegularExpressionBenchmark::iterateThroughGlobalMatchResults()
{
QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
auto matchResultIterator = re.globalMatch(textToMatch);
QBENCHMARK {
if (matchResultIterator.isValid()) {
while (matchResultIterator.hasNext()) {
matchResultIterator.next();
}
}
}
}

QTEST_MAIN(tst_QRegularExpressionBenchmark)

#include "tst_bench_qregularexpression.moc"
3 changes: 2 additions & 1 deletion tests/benchmarks/corelib/text/text.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SUBDIRS = \
qchar \
qlocale \
qstringbuilder \
qstringlist
qstringlist \
qregularexpression

*g++*: SUBDIRS += qstring

0 comments on commit fb51b47

Please sign in to comment.