QMetaType:🆔 add fastpath

This inlines the fastpath of QMetaType::id and splits the slowpath into
its own function. With that change, we can also use id in operator==,
simplifying the code there.

Change-Id: I286fe173b43a495dbda8faa151a93895b4fd22e4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Fabian Kosmale 2021-02-12 20:56:23 +01:00
parent 51e50bd1e6
commit fe6338bded
2 changed files with 22 additions and 13 deletions

View File

@ -487,15 +487,17 @@ bool QMetaType::isRegistered() const
Returns id type hold by this QMetatype instance.
*/
int QMetaType::id() const
/*!
\internal
The slowpath of id(). Precondition: d_ptr != nullptr
*/
int QMetaType::idHelper() const
{
if (d_ptr) {
if (int id = d_ptr->typeId.loadRelaxed())
return id;
auto reg = customTypeRegistry();
if (reg) {
return reg->registerCustomType(d_ptr);
}
Q_ASSERT(d_ptr);
auto reg = customTypeRegistry();
if (reg) {
return reg->registerCustomType(d_ptr);
}
return 0;
}

View File

@ -443,7 +443,15 @@ public:
bool isValid() const;
bool isRegistered() const;
int id() const;
int id() const
{
if (d_ptr) {
if (int id = d_ptr->typeId.loadRelaxed())
return id;
return idHelper();
}
return 0;
};
constexpr qsizetype sizeOf() const;
constexpr qsizetype alignOf() const;
constexpr TypeFlags flags() const;
@ -486,10 +494,8 @@ public:
if (!a.d_ptr || !b.d_ptr)
return false; // one type is undefined, the other is defined
// avoid id call if we already have the id
const int ai = a.d_ptr->typeId.loadRelaxed(); // avoid reading the atomic twice
const int aId = ai ? ai : a.id();
const int bi = b.d_ptr->typeId.loadRelaxed();
const int bId = bi ? bi : b.id();
const int aId = a.id();
const int bId = b.id();
return aId == bId;
}
friend bool operator!=(QMetaType a, QMetaType b) { return !(a == b); }
@ -709,6 +715,7 @@ public:
const QtPrivate::QMetaTypeInterface *iface() { return d_ptr; }
private:
int idHelper() const;
friend class QVariant;
const QtPrivate::QMetaTypeInterface *d_ptr = nullptr;
};