[optimization] QVariants comparison
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>
Mon, 31 Oct 2011 09:22:46 +0000 (10:22 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 9 Nov 2011 09:12:34 +0000 (10:12 +0100)
QMetaType::isRegistered and QMetaType::typeName are quite expensive.
To compare two custom objects we need to have their type name (for
dereferenced comparison). If the name exist we know for sure that the
type is registered and we do not have to call QMetaType::isRegistered
anymore.

Change-Id: Iba631e012504c8633868a902880fa30d38afb917
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
src/corelib/kernel/qvariant_p.h

index aa03bb2..5729c0d 100644 (file)
@@ -161,13 +161,13 @@ class QVariantComparator {
     struct FilteredComparator<T, /* IsAcceptedType = */ false> {
         static bool compare(const QVariant::Private *m_a, const QVariant::Private *m_b)
         {
-            if (!QMetaType::isRegistered(m_a->type))
+            const char *const typeName = QMetaType::typeName(m_a->type);
+            if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(m_a->type)))
                 qFatal("QVariant::compare: type %d unknown to QVariant.", m_a->type);
 
             const void *a_ptr = m_a->is_shared ? m_a->data.shared->ptr : &(m_a->data.ptr);
             const void *b_ptr = m_b->is_shared ? m_b->data.shared->ptr : &(m_b->data.ptr);
 
-            const char *const typeName = QMetaType::typeName(m_a->type);
             uint typeNameLen = qstrlen(typeName);
             if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
                 return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);