From 3de60970e58aa7c455e356c277667614804475ce Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 24 Aug 2012 12:40:20 +0200 Subject: [PATCH] QMetaTypeId: optimize qt_metatype_id() in special cases Same change as for Q_DECLARE_METATYPE, but in the container and smart pointer specialisations of it. Change-Id: If9390ccc43eb3b07122f5ea5b8094139b5e1ded0 Reviewed-by: Olivier Goffart --- src/corelib/kernel/qmetatype.h | 98 +++++++++++++++++----------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 80bc4a48b7..cc4626f5eb 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -648,16 +648,17 @@ struct QMetaTypeIdQObject static int qt_metatype_id() { static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); - if (!metatype_id.load()) { - const int len = int(strlen(T::staticMetaObject.className())); - QVarLengthArray classNameStar; - classNameStar.append(T::staticMetaObject.className(), len); - classNameStar.append('*'); - metatype_id.storeRelease(qRegisterNormalizedMetaType( \ + if (const int id = metatype_id.loadAcquire()) + return id; + const int len = int(strlen(T::staticMetaObject.className())); + QVarLengthArray classNameStar; + classNameStar.append(T::staticMetaObject.className(), len); + classNameStar.append('*'); + const int newId = qRegisterNormalizedMetaType( \ QByteArray(classNameStar.constData(), classNameStar.size()), - reinterpret_cast(quintptr(-1)))); - } - return metatype_id.loadAcquire(); + reinterpret_cast(quintptr(-1))); + metatype_id.storeRelease(newId); + return newId; } }; @@ -733,22 +734,22 @@ struct QMetaTypeId< SINGLE_ARG_TEMPLATE > \ static int qt_metatype_id() \ { \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ - if (!metatype_id.load()) { \ - QVarLengthArray name; \ - name.append(#SINGLE_ARG_TEMPLATE, int(sizeof(#SINGLE_ARG_TEMPLATE)) - 1); \ - name.append('<'); \ - const char *tName = QMetaType::typeName(qMetaTypeId()); \ - Q_ASSERT(tName); \ - name.append(tName, int(strlen(tName))); \ - if (name.last() == '>') \ - name.append(' '); \ - name.append('>'); \ - metatype_id.storeRelease( \ - qRegisterNormalizedMetaType< SINGLE_ARG_TEMPLATE >( \ + if (const int id = metatype_id.load()) \ + return id; \ + QVarLengthArray name; \ + name.append(#SINGLE_ARG_TEMPLATE, int(sizeof(#SINGLE_ARG_TEMPLATE)) - 1); \ + name.append('<'); \ + const char *tName = QMetaType::typeName(qMetaTypeId()); \ + Q_ASSERT(tName); \ + name.append(tName, int(strlen(tName))); \ + if (name.last() == '>') \ + name.append(' '); \ + name.append('>'); \ + const int newId = qRegisterNormalizedMetaType< SINGLE_ARG_TEMPLATE >( \ QByteArray(name.constData(), name.size()), \ - reinterpret_cast< SINGLE_ARG_TEMPLATE *>(quintptr(-1)))); \ - } \ - return metatype_id.loadAcquire(); \ + reinterpret_cast< SINGLE_ARG_TEMPLATE *>(quintptr(-1))); \ + metatype_id.storeRelease(newId); \ + return newId; \ } \ }; @@ -762,26 +763,26 @@ struct QMetaTypeId< DOUBLE_ARG_TEMPLATE > \ static int qt_metatype_id() \ { \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ - if (!metatype_id.load()) {\ - QVarLengthArray name; \ - name.append(#DOUBLE_ARG_TEMPLATE, sizeof(#DOUBLE_ARG_TEMPLATE) - 1); \ - name.append('<'); \ - const char *tName = QMetaType::typeName(qMetaTypeId()); \ - Q_ASSERT(tName); \ - name.append(tName, int(strlen(tName))); \ - name.append(','); \ - const char *uName = QMetaType::typeName(qMetaTypeId()); \ - Q_ASSERT(uName); \ - name.append(uName, int(strlen(uName))); \ - if (name.last() == '>') \ - name.append(' '); \ - name.append('>'); \ - metatype_id.storeRelease(\ - qRegisterNormalizedMetaType< DOUBLE_ARG_TEMPLATE >(\ + if (const int id = metatype_id.loadAcquire()) \ + return id; \ + QVarLengthArray name; \ + name.append(#DOUBLE_ARG_TEMPLATE, sizeof(#DOUBLE_ARG_TEMPLATE) - 1); \ + name.append('<'); \ + const char *tName = QMetaType::typeName(qMetaTypeId()); \ + Q_ASSERT(tName); \ + name.append(tName, int(strlen(tName))); \ + name.append(','); \ + const char *uName = QMetaType::typeName(qMetaTypeId()); \ + Q_ASSERT(uName); \ + name.append(uName, int(strlen(uName))); \ + if (name.last() == '>') \ + name.append(' '); \ + name.append('>'); \ + const int newId = qRegisterNormalizedMetaType< DOUBLE_ARG_TEMPLATE >(\ QByteArray(name.constData(), name.size()), \ - reinterpret_cast< DOUBLE_ARG_TEMPLATE *>(quintptr(-1)))); \ - }\ - return metatype_id.loadAcquire(); \ + reinterpret_cast< DOUBLE_ARG_TEMPLATE *>(quintptr(-1))); \ + metatype_id.storeRelease(newId); \ + return newId; \ } \ }; @@ -803,11 +804,12 @@ struct QMetaTypeId_ ## SMART_POINTER ## _QObjectStar \ static int qt_metatype_id() \ { \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ - if (!metatype_id.load()) { \ - metatype_id.storeRelease(qRegisterNormalizedMetaType< SMART_POINTER >( #SMART_POINTER "<" + QByteArray(T::staticMetaObject.className()) + ">", \ - reinterpret_cast< SMART_POINTER *>(quintptr(-1)))); \ - } \ - return metatype_id.loadAcquire(); \ + if (const int id = metatype_id.loadAcquire()) \ + return id; \ + const int newId = qRegisterNormalizedMetaType< SMART_POINTER >( #SMART_POINTER "<" + QByteArray(T::staticMetaObject.className()) + ">", \ + reinterpret_cast< SMART_POINTER *>(quintptr(-1))); \ + metatype_id.storeRelease(newId); \ + return newId; \ } \ }; \ \