Skip to content

runners/services: reduce string allocations

Fushan Wen requested to merge work/fuf/servicerunnerstringview into master

QStringView::split is more efficient than QString::split as the former is only a view but the latter allocates memory for new strings.

Benchmark
#pragma GCC push_options
#pragma GCC optimize("O0")

#include <QTest>
#include <QString>

using namespace Qt::StringLiterals;

class StringFamilyTest : public QObject {
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void test_stringview();
    void test_string();
private:
    QString query = u"Qt Creator"_s;
};

QTEST_GUILESS_MAIN(StringFamilyTest)

void StringFamilyTest::initTestCase()
{
}

void StringFamilyTest::test_stringview()
{
    QBENCHMARK {
        auto queryList = QStringView(query).split(QLatin1Char('\n'));
    }
}

void StringFamilyTest::test_string()
{
    QBENCHMARK {
        auto queryList = query.split(QLatin1Char('\n'));
    }
}

#include "main.moc"

#pragma GCC pop_options
********* Start testing of StringFamilyTest *********
Config: Using QtTest library 6.6.1, Qt 6.6.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 13.2.1 20240125 [revision fc7d87e0ffadca49bec29b2107c1efd0da6b6ded]), opensuse-tumbleweed 20240212
PASS   : StringFamilyTest::initTestCase()
PASS   : StringFamilyTest::test_stringview()
RESULT : StringFamilyTest::test_stringview():
     824 instruction reads per iteration (total: 824, iterations: 1)
PASS   : StringFamilyTest::test_string()
RESULT : StringFamilyTest::test_string():
     1,279 instruction reads per iteration (total: 1,279, iterations: 1)
PASS   : StringFamilyTest::cleanupTestCase()
Totals: 4 passed, 0 failed, 0 skipped, 0 blacklisted, 178ms
********* Finished testing of StringFamilyTest *********

Merge request reports

Loading