From 6ec1dc904d59bab3a0330ff2a6c26600a35c87a9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 22 Feb 2022 21:27:37 +0100 Subject: QSettings: port key processing to QAnyStringView ... in preparation for replacing the QString keys in the public API with QAnyStringView ones. This removes the "important optimization" that avoids a detach in the common case where the input is the same as the output of normalization. But that optimization is beside the point, because it trades a memory allocation avoided in the library for O(N) allocations inserted into user code for each call to QSettings::value(), the vast majority of which are calls with string literals. With the public interface ported to QAnyStringView in the follow-up patch, we can then internally optimize memory allocations _in a central place_ (e.g. by returning std::u16string or QVarLengthArray from normalizeKey() instead of QString). But first we need to get rid of all the unwarranted allocations in user code. Change-Id: I45fc83d972c552a220c9c29508001d3f172e1162 Reviewed-by: Qt CI Bot Reviewed-by: Edward Welbourne Reviewed-by: Sona Kurazyan --- tests/auto/corelib/io/qsettings/tst_qsettings.cpp | 27 ++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'tests/auto/corelib/io/qsettings/tst_qsettings.cpp') diff --git a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp index c4ede66381..e8bef28c02 100644 --- a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp +++ b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -2160,20 +2160,17 @@ void tst_QSettings::testNormalizedKey() inKey.detach(); - QString result = QSettingsPrivate::normalizedKey(inKey); - QCOMPARE(result, outKey); - - /* - If the key is already normalized, we verify that outKey is - just a shallow copy of the input string. This is an important - optimization that shouldn't be removed accidentally. - */ - if (inKey == outKey) { - QVERIFY(!result.isDetached()); - } else { - if (!result.isEmpty()) { - QVERIFY(result.isDetached()); - } + { + auto result = QSettingsPrivate::normalizedKey(inKey); + QCOMPARE(result, outKey); + } + { + auto result = QSettingsPrivate::normalizedKey(QUtf8StringView{inKey.toUtf8()}); + QCOMPARE(result, outKey); + } + { + auto result = QSettingsPrivate::normalizedKey(QLatin1String{inKey.toLatin1()}); + QCOMPARE(result, outKey); } } #endif -- cgit v1.2.3