QSettings: port API from QString to QAnyStringView keys

With the public interface ported to QAnyStringView, we can now
internally optimize memory allocations _in a central place_ (e.g. by
returning std::u16string or QVarLengthArray<QChar> from normalizeKey()
instead of QString). But first we needed to get rid of all the
unwarranted allocations in user code.

Effects on Linux AMD64 stripped C++20 release builds:

GCC 11.2 libstdc++ (TEXT -= 6.5%):

     text    data     bss     dec     hex filename
   635148   10992    2824  648964   9e704 tst_qsettings-01-baseline
   593691   10992    2824  607507   94513 tst_qsettings-02-qanystringview

Clang 10.0.0 libc++ (TEXT -= 11.6%(!)):

     text    data     bss     dec     hex filename
   790336   10640    2832  803808   c43e0 tst_qsettings-01-baseline
   698572   10640    2832  712044   add6c tst_qsettings-02-qanystringview

That's the beauty of QAnyStringView: transparently reducing temporary
QString creation; and the simplest code is also the most efficient.

[ChangeLog][QtCore][QSettings] Keys can now be passed as
QAnyStringView (was: QString). The most efficient way to pass literal
keys is now "key"_L1, the backwards-compatible way is
QStringLiteral("key").

Fixes: QTBUG-101390
Change-Id: I510fb4ce17ef109dac7c9cdc5d90ede0d1a9db5f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-03-12 16:25:33 +01:00 committed by Marc Mutz
parent a9cef86b8f
commit 4cf299eb5b
3 changed files with 90 additions and 10 deletions

View File

@ -214,6 +214,49 @@ void QObject::setObjectName(const QString &name)
}
#include "qsettings.h"
void QSettings::beginGroup(const QString &prefix)
{
return beginGroup(qToAnyStringViewIgnoringNull(prefix));
}
int QSettings::beginReadArray(const QString &prefix)
{
return beginReadArray(qToAnyStringViewIgnoringNull(prefix));
}
void QSettings::beginWriteArray(const QString &prefix, int size)
{
beginWriteArray(qToAnyStringViewIgnoringNull(prefix), size);
}
void QSettings::setValue(const QString &key, const QVariant &value)
{
setValue(qToAnyStringViewIgnoringNull(key), value);
}
void QSettings::remove(const QString &key)
{
remove(qToAnyStringViewIgnoringNull(key));
}
bool QSettings::contains(const QString &key) const
{
return contains(qToAnyStringViewIgnoringNull(key));
}
QVariant QSettings::value(const QString &key, const QVariant &defaultValue) const
{
return value(qToAnyStringViewIgnoringNull(key), defaultValue);
}
QVariant QSettings::value(const QString &key) const
{
return value(qToAnyStringViewIgnoringNull(key));
}
#include "qversionnumber.h"
QT_WARNING_PUSH

View File

@ -2914,9 +2914,12 @@ void QSettings::setAtomicSyncRequired(bool enable)
Call endGroup() to reset the current group to what it was before
the corresponding beginGroup() call. Groups can be nested.
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa endGroup(), group()
*/
void QSettings::beginGroup(const QString &prefix)
void QSettings::beginGroup(QAnyStringView prefix)
{
Q_D(QSettings);
d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix)));
@ -2970,9 +2973,12 @@ QString QSettings::group() const
Use beginWriteArray() to write the array in the first place.
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa beginWriteArray(), endArray(), setArrayIndex()
*/
int QSettings::beginReadArray(const QString &prefix)
int QSettings::beginReadArray(QAnyStringView prefix)
{
Q_D(QSettings);
d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), false));
@ -3006,9 +3012,12 @@ int QSettings::beginReadArray(const QString &prefix)
To read back an array, use beginReadArray().
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa beginReadArray(), endArray(), setArrayIndex()
*/
void QSettings::beginWriteArray(const QString &prefix, int size)
void QSettings::beginWriteArray(QAnyStringView prefix, int size)
{
Q_D(QSettings);
d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), size < 0));
@ -3169,9 +3178,12 @@ bool QSettings::isWritable() const
\snippet code/src_corelib_io_qsettings.cpp 23
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa value(), remove(), contains()
*/
void QSettings::setValue(const QString &key, const QVariant &value)
void QSettings::setValue(QAnyStringView key, const QVariant &value)
{
Q_D(QSettings);
if (key.isEmpty()) {
@ -3203,9 +3215,12 @@ void QSettings::setValue(const QString &key, const QVariant &value)
case-sensitive keys. To avoid portability problems, see the
\l{Section and Key Syntax} rules.
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa setValue(), value(), contains()
*/
void QSettings::remove(const QString &key)
void QSettings::remove(QAnyStringView key)
{
Q_D(QSettings);
/*
@ -3238,9 +3253,12 @@ void QSettings::remove(const QString &key)
case-sensitive keys. To avoid portability problems, see the
\l{Section and Key Syntax} rules.
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa value(), setValue()
*/
bool QSettings::contains(const QString &key) const
bool QSettings::contains(QAnyStringView key) const
{
Q_D(const QSettings);
return d->get(d->actualKey(key)) != std::nullopt;
@ -3288,8 +3306,8 @@ bool QSettings::event(QEvent *event)
#endif
/*!
\fn QSettings::value(const QString &key) const
\fn QSettings::value(const QString &key, const QVariant &defaultValue) const
\fn QSettings::value(QAnyStringView key) const
\fn QSettings::value(QAnyStringView key, const QVariant &defaultValue) const
Returns the value for setting \a key. If the setting doesn't
exist, returns \a defaultValue.
@ -3306,15 +3324,18 @@ bool QSettings::event(QEvent *event)
\snippet code/src_corelib_io_qsettings.cpp 26
\note In Qt versions prior to 6.4, this function took QString, not
QAnyStringView.
\sa setValue(), contains(), remove()
*/
QVariant QSettings::value(const QString &key) const
QVariant QSettings::value(QAnyStringView key) const
{
Q_D(const QSettings);
return d->value(key, nullptr);
}
QVariant QSettings::value(const QString &key, const QVariant &defaultValue) const
QVariant QSettings::value(QAnyStringView key, const QVariant &defaultValue) const
{
Q_D(const QSettings);
return d->value(key, &defaultValue);

View File

@ -148,12 +148,19 @@ public:
bool isAtomicSyncRequired() const;
void setAtomicSyncRequired(bool enable);
#if QT_CORE_REMOVED_SINCE(6, 4)
void beginGroup(const QString &prefix);
#endif
void beginGroup(QAnyStringView prefix);
void endGroup();
QString group() const;
#if QT_CORE_REMOVED_SINCE(6, 4)
int beginReadArray(const QString &prefix);
void beginWriteArray(const QString &prefix, int size = -1);
#endif
int beginReadArray(QAnyStringView prefix);
void beginWriteArray(QAnyStringView prefix, int size = -1);
void endArray();
void setArrayIndex(int i);
@ -162,12 +169,21 @@ public:
QStringList childGroups() const;
bool isWritable() const;
#if QT_CORE_REMOVED_SINCE(6, 4)
void setValue(const QString &key, const QVariant &value);
QVariant value(const QString &key, const QVariant &defaultValue) const;
QVariant value(const QString &key) const;
#endif
void setValue(QAnyStringView key, const QVariant &value);
QVariant value(QAnyStringView key, const QVariant &defaultValue) const;
QVariant value(QAnyStringView key) const;
#if QT_CORE_REMOVED_SINCE(6, 4)
void remove(const QString &key);
bool contains(const QString &key) const;
#endif
void remove(QAnyStringView key);
bool contains(QAnyStringView key) const;
void setFallbacksEnabled(bool b);
bool fallbacksEnabled() const;