summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2022-03-12 16:25:33 +0100
committerMarc Mutz <marc.mutz@qt.io>2022-03-23 14:46:33 +0100
commit4cf299eb5bbdbac8484c2ee8c5afbd260dccc6d5 (patch)
tree57399212a42525523fa3cf48f3f48d5a3e41ddf9 /src
parenta9cef86b8fda35bb5551b70385fee8adeea25784 (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/compat/removed_api.cpp43
-rw-r--r--src/corelib/io/qsettings.cpp41
-rw-r--r--src/corelib/io/qsettings.h16
3 files changed, 90 insertions, 10 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp
index 64c3e9ccff..51482d1e60 100644
--- a/src/corelib/compat/removed_api.cpp
+++ b/src/corelib/compat/removed_api.cpp
@@ -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
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp
index 2d5246352a..d3868a92a5 100644
--- a/src/corelib/io/qsettings.cpp
+++ b/src/corelib/io/qsettings.cpp
@@ -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);
diff --git a/src/corelib/io/qsettings.h b/src/corelib/io/qsettings.h
index 42b2527f0b..98977be953 100644
--- a/src/corelib/io/qsettings.h
+++ b/src/corelib/io/qsettings.h
@@ -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;