summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-01-19 01:47:35 +0100
committerMarc Mutz <marc.mutz@qt.io>2022-01-20 22:56:12 +0100
commitb1b0c2970e480ef460a61f37fa430dc443390358 (patch)
tree3f4045199a8e82a7d39b1f4b9b6055fd8b69ef6f /src/corelib/tools
parentb083c27d0ab1e436c1b21785c67e2df419c67335 (diff)
QtCore: replace qSwap with std::swap/member-swap where possible
qSwap() is a monster that looks for ADL overloads of swap() and also detects the noexcept of the wrapped swap() function, so it should only be used when the argument type is unknown. In the vast majority of cases, the type is known to be efficiently std::swap()able or to have a member-swap. Call either of these. For the common case of pointer types, circumvent the expensive trait checks on std::swap() by providing a hand-rolled qt_ptr_swap() template, the advantage being that it can be unconditionally noexcept, removing all type traits instantiations. Don't document it, otherwise we'd be unable to pick it to 6.2. Effects on Clang -ftime-trace of a PCH'ed libQt6Gui.so build: before: **** Template sets that took longest to instantiate: [...] 27766 ms: qSwap<$> (9073 times, avg 3 ms) [...] 2806 ms: std::swap<$> (1229 times, avg 2 ms) (30572ms) after: **** Template sets that took longest to instantiate: [...] 5047 ms: qSwap<$> (641 times, avg 7 ms) [...] 3371 ms: std::swap<$> (1376 times, avg 2 ms) [qt_ptr_swap<$> does not appear in the top 400, so < 905ms] (< 9323ms) As a drive-by, remove superfluous inline keywords and template ornaments. Task-number: QTBUG-97601 Pick-to: 6.3 6.2 Change-Id: I88f9b4e3cbece268c4a1238b6d50e5712a1bab5a Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qarraydatapointer.h6
-rw-r--r--src/corelib/tools/qbitarray.h2
-rw-r--r--src/corelib/tools/qcache.h4
-rw-r--r--src/corelib/tools/qcommandlineoption.h2
-rw-r--r--src/corelib/tools/qcontiguouscache.h2
-rw-r--r--src/corelib/tools/qeasingcurve.h2
-rw-r--r--src/corelib/tools/qhash.h9
-rw-r--r--src/corelib/tools/qlist.h2
-rw-r--r--src/corelib/tools/qmap.h4
-rw-r--r--src/corelib/tools/qscopedpointer.h2
-rw-r--r--src/corelib/tools/qshareddata.h4
-rw-r--r--src/corelib/tools/qshareddata_impl.h2
-rw-r--r--src/corelib/tools/qsharedpointer_impl.h12
-rw-r--r--src/corelib/tools/qtaggedpointer.h2
-rw-r--r--src/corelib/tools/qversionnumber.h2
15 files changed, 31 insertions, 26 deletions
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h
index dda3cf13eb..2759810919 100644
--- a/src/corelib/tools/qarraydatapointer.h
+++ b/src/corelib/tools/qarraydatapointer.h
@@ -149,9 +149,9 @@ public:
void swap(QArrayDataPointer &other) noexcept
{
- qSwap(d, other.d);
- qSwap(ptr, other.ptr);
- qSwap(size, other.size);
+ qt_ptr_swap(d, other.d);
+ qt_ptr_swap(ptr, other.ptr);
+ std::swap(size, other.size);
}
void clear() noexcept(std::is_nothrow_destructible<T>::value)
diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h
index 75d680dc84..4466fbc9c0 100644
--- a/src/corelib/tools/qbitarray.h
+++ b/src/corelib/tools/qbitarray.h
@@ -62,7 +62,7 @@ public:
inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBitArray)
- inline void swap(QBitArray &other) noexcept { qSwap(d, other.d); }
+ void swap(QBitArray &other) noexcept { d.swap(other.d); }
inline qsizetype size() const { return (d.size() << 3) - *d.constData(); }
inline qsizetype count() const { return (d.size() << 3) - *d.constData(); }
diff --git a/src/corelib/tools/qcache.h b/src/corelib/tools/qcache.h
index 72a456890f..d5e978d6f3 100644
--- a/src/corelib/tools/qcache.h
+++ b/src/corelib/tools/qcache.h
@@ -64,8 +64,8 @@ class QCache
}
Value &operator=(Value &&other) noexcept
{
- qSwap(t, other.t);
- qSwap(cost, other.cost);
+ qt_ptr_swap(t, other.t);
+ std::swap(cost, other.cost);
return *this;
}
~Value() { delete t; }
diff --git a/src/corelib/tools/qcommandlineoption.h b/src/corelib/tools/qcommandlineoption.h
index 2e7d8fd9da..253c46a178 100644
--- a/src/corelib/tools/qcommandlineoption.h
+++ b/src/corelib/tools/qcommandlineoption.h
@@ -75,7 +75,7 @@ public:
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QCommandLineOption)
void swap(QCommandLineOption &other) noexcept
- { qSwap(d, other.d); }
+ { d.swap(other.d); }
QStringList names() const;
diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h
index 62ebfa0658..e78a4d3184 100644
--- a/src/corelib/tools/qcontiguouscache.h
+++ b/src/corelib/tools/qcontiguouscache.h
@@ -97,7 +97,7 @@ public:
QContiguousCache<T> &operator=(const QContiguousCache<T> &other);
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QContiguousCache)
- inline void swap(QContiguousCache<T> &other) noexcept { qSwap(d, other.d); }
+ void swap(QContiguousCache &other) noexcept { qt_ptr_swap(d, other.d); }
#ifndef Q_CLANG_QDOC
template <typename U = T>
diff --git a/src/corelib/tools/qeasingcurve.h b/src/corelib/tools/qeasingcurve.h
index 7e50743bf1..225641d477 100644
--- a/src/corelib/tools/qeasingcurve.h
+++ b/src/corelib/tools/qeasingcurve.h
@@ -81,7 +81,7 @@ public:
QEasingCurve(QEasingCurve &&other) noexcept : d_ptr(other.d_ptr) { other.d_ptr = nullptr; }
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QEasingCurve)
- void swap(QEasingCurve &other) noexcept { qSwap(d_ptr, other.d_ptr); }
+ void swap(QEasingCurve &other) noexcept { qt_ptr_swap(d_ptr, other.d_ptr); }
bool operator==(const QEasingCurve &other) const;
inline bool operator!=(const QEasingCurve &other) const
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index c4b4098c0f..a19713b337 100644
--- a/src/corelib/tools/qhash.h
+++ b/src/corelib/tools/qhash.h
@@ -911,7 +911,7 @@ public:
insert(f->first, f->second);
}
#endif
- void swap(QHash &other) noexcept { qSwap(d, other.d); }
+ void swap(QHash &other) noexcept { qt_ptr_swap(d, other.d); }
#ifndef Q_CLANG_QDOC
template <typename AKey = Key, typename AT = T>
@@ -1446,7 +1446,12 @@ public:
{
unite(std::move(other));
}
- void swap(QMultiHash &other) noexcept { qSwap(d, other.d); qSwap(m_size, other.m_size); }
+
+ void swap(QMultiHash &other) noexcept
+ {
+ qt_ptr_swap(d, other.d);
+ std::swap(m_size, other.m_size);
+ }
#ifndef Q_CLANG_QDOC
template <typename AKey = Key, typename AT = T>
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h
index aaa2a950dd..93498dfa69 100644
--- a/src/corelib/tools/qlist.h
+++ b/src/corelib/tools/qlist.h
@@ -351,7 +351,7 @@ public:
// compiler-generated special member functions are fine!
- void swap(QList<T> &other) noexcept { qSwap(d, other.d); }
+ void swap(QList &other) noexcept { d.swap(other.d); }
#ifndef Q_CLANG_QDOC
template <typename U = T>
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index f2f1bc96f4..5daf24189b 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -238,7 +238,7 @@ public:
void swap(QMap<Key, T> &other) noexcept
{
- qSwap(d, other.d);
+ d.swap(other.d);
}
QMap(std::initializer_list<std::pair<Key, T>> list)
@@ -849,7 +849,7 @@ public:
void swap(QMultiMap<Key, T> &other) noexcept
{
- qSwap(d, other.d);
+ d.swap(other.d);
}
explicit QMultiMap(const QMap<Key, T> &other)
diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h
index c9afb248a1..8c7ecae01a 100644
--- a/src/corelib/tools/qscopedpointer.h
+++ b/src/corelib/tools/qscopedpointer.h
@@ -173,7 +173,7 @@ public:
QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
void swap(QScopedPointer<T, Cleanup> &other) noexcept
{
- qSwap(d, other.d);
+ qt_ptr_swap(d, other.d);
}
#endif
diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h
index de11257db3..717e33770e 100644
--- a/src/corelib/tools/qshareddata.h
+++ b/src/corelib/tools/qshareddata.h
@@ -125,7 +125,7 @@ public:
bool operator!() const noexcept { return d == nullptr; }
void swap(QSharedDataPointer &other) noexcept
- { qSwap(d, other.d); }
+ { qt_ptr_swap(d, other.d); }
#define DECLARE_COMPARE_SET(T1, A1, T2, A2) \
friend bool operator<(T1, T2) noexcept \
@@ -222,7 +222,7 @@ public:
bool operator!() const noexcept { return d == nullptr; }
void swap(QExplicitlySharedDataPointer &other) noexcept
- { qSwap(d, other.d); }
+ { qt_ptr_swap(d, other.d); }
DECLARE_COMPARE_SET(const QExplicitlySharedDataPointer &p1, p1.d, const QExplicitlySharedDataPointer &p2, p2.d)
DECLARE_COMPARE_SET(const QExplicitlySharedDataPointer &p1, p1.d, const T *ptr, ptr)
diff --git a/src/corelib/tools/qshareddata_impl.h b/src/corelib/tools/qshareddata_impl.h
index cf1c534cdf..960dbdecdc 100644
--- a/src/corelib/tools/qshareddata_impl.h
+++ b/src/corelib/tools/qshareddata_impl.h
@@ -138,7 +138,7 @@ public:
constexpr void swap(QExplicitlySharedDataPointerV2 &other) noexcept
{
- qSwap(d, other.d);
+ qt_ptr_swap(d, other.d);
}
// important change from QExplicitlySharedDataPointer: deep const
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h
index 61aeee4952..7b02ffe50a 100644
--- a/src/corelib/tools/qsharedpointer_impl.h
+++ b/src/corelib/tools/qsharedpointer_impl.h
@@ -508,8 +508,8 @@ private:
void internalSwap(QSharedPointer &other) noexcept
{
- qSwap(d, other.d);
- qSwap(this->value, other.value);
+ qt_ptr_swap(d, other.d);
+ qt_ptr_swap(this->value, other.value);
}
template <class X> friend class QSharedPointer;
@@ -538,8 +538,8 @@ private:
}
}
- qSwap(d, o);
- qSwap(this->value, actual);
+ qt_ptr_swap(d, o);
+ qt_ptr_swap(this->value, actual);
if (!d || d->strongref.loadRelaxed() == 0)
this->value = nullptr;
@@ -609,8 +609,8 @@ public:
void swap(QWeakPointer &other) noexcept
{
- qSwap(this->d, other.d);
- qSwap(this->value, other.value);
+ qt_ptr_swap(this->d, other.d);
+ qt_ptr_swap(this->value, other.value);
}
inline QWeakPointer(const QSharedPointer<T> &o) : d(o.d), value(o.data())
diff --git a/src/corelib/tools/qtaggedpointer.h b/src/corelib/tools/qtaggedpointer.h
index c12d5acdc2..68074cc43d 100644
--- a/src/corelib/tools/qtaggedpointer.h
+++ b/src/corelib/tools/qtaggedpointer.h
@@ -144,7 +144,7 @@ public:
void swap(QTaggedPointer &other) noexcept
{
- qSwap(d, other.d);
+ std::swap(d, other.d);
}
friend inline bool operator==(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
diff --git a/src/corelib/tools/qversionnumber.h b/src/corelib/tools/qversionnumber.h
index 7b3e0995a0..8efdde3391 100644
--- a/src/corelib/tools/qversionnumber.h
+++ b/src/corelib/tools/qversionnumber.h
@@ -131,7 +131,7 @@ class QVersionNumber
void swap(SegmentStorage &other) noexcept
{
- qSwap(dummy, other.dummy);
+ std::swap(dummy, other.dummy);
}
explicit SegmentStorage(QList<int> &&seg)