Draft: applets/kimpanel: reduce allocations in `XkbLayoutManager::getLayout`
Before: 5,777 instruction reads per iteration (total: 5,777, iterations: 1
After: 1,341 instruction reads per iteration (total: 1,341, iterations: 1)
Benchmark
#pragma GCC push_options
#pragma GCC optimize("O2")
#include <ranges>
#include <QTest>
#include <QString>
#include <QProcess>
using namespace Qt::StringLiterals;
class StringFamilyTest : public QObject {
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void test_splitQByteArray();
void test_split_view_const();
private:
QByteArray output;
};
QTEST_GUILESS_MAIN(StringFamilyTest)
void StringFamilyTest::initTestCase()
{
QProcess process;
process.start(u"setxkbmap"_s, QStringList(u"-query"_s));
process.waitForFinished();
output = process.readAllStandardOutput();
}
void StringFamilyTest::test_splitQByteArray()
{
QString m_defaultLayout;
QString m_defaultVariant;
QString m_defaultOption;
QBENCHMARK {
const QList<QByteArray> lines = output.split('\n');
for (const QByteArray &line : lines) {
QByteArray element("layout:");
if (line.startsWith(element)) {
m_defaultLayout = QString::fromLatin1(line.mid(element.length())).trimmed();
}
element = "variant:";
if (line.startsWith(element)) {
m_defaultVariant = QString::fromLatin1(line.mid(element.length())).trimmed();
}
element = "options:";
if (line.startsWith(element)) {
m_defaultOption = QString::fromLatin1(line.mid(element.length())).trimmed();
}
}
}
}
void StringFamilyTest::test_split_view_const()
{
QString m_defaultLayout;
QString m_defaultVariant;
QString m_defaultOption;
QBENCHMARK {
for (const QByteArrayView line : std::views::split(std::as_const(output), '\n')) {
constexpr QByteArrayView element("layout:");
if (line.startsWith(element)) {
m_defaultLayout = QString::fromLatin1(line.sliced(element.length())).trimmed();
}
constexpr QByteArrayView element2("variant:");
if (line.startsWith(element2)) {
m_defaultVariant = QString::fromLatin1(line.sliced(element2.length())).trimmed();
}
constexpr QByteArrayView element3 = "options:";
if (line.startsWith(element3)) {
m_defaultOption = QString::fromLatin1(line.sliced(element3.length())).trimmed();
}
}
}
}
#include "main.moc"
#pragma GCC pop_options