Reduce QtCore lib binary size by around ~3KB, by removing template code

Reusing a template is much better then creating a new one, even if it
should inline the same code.

For some reason replacing T* by void* force gcc to remove a few bytes
per template instantiation too, it is not really significant, but it
alows us to simplify the code.

Benchmarks don't show any regressions.

Change-Id: I4fdf1e4dc311b23021eb5758605602937d05b183
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jędrzej Nowacki 2012-02-10 09:14:41 +01:00 committed by Qt by Nokia
parent df055acc81
commit beab403d9f
4 changed files with 42 additions and 91 deletions

View File

@ -1290,7 +1290,7 @@ namespace {
class TypeDestroyer {
template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted>
struct DestroyerImpl {
static void Destroy(const int /* type */, T *where) { delete where; }
static void Destroy(const int /* type */, void *where) { qMetaTypeDeleteHelper<T>(where); }
};
template<typename T>
struct DestroyerImpl<T, /* IsAcceptedType = */ false> {
@ -1354,11 +1354,11 @@ namespace {
class TypeConstructor {
template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted>
struct ConstructorImpl {
static void *Construct(const int /*type*/, void *where, const T *copy) { return qMetaTypeConstructHelper(where, copy); }
static void *Construct(const int /*type*/, void *where, const void *copy) { return qMetaTypeConstructHelper<T>(where, copy); }
};
template<typename T>
struct ConstructorImpl<T, /* IsAcceptedType = */ false> {
static void *Construct(const int type, void *where, const T *copy)
static void *Construct(const int type, void *where, const void *copy)
{
if (QTypeModuleInfo<T>::IsGui)
return Q_LIKELY(qMetaTypeGuiHelper) ? qMetaTypeGuiHelper[type - QMetaType::FirstGuiType].constructor(where, copy) : 0;
@ -1440,7 +1440,7 @@ namespace {
class TypeDestructor {
template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted>
struct DestructorImpl {
static void Destruct(const int /* type */, T *where) { qMetaTypeDestructHelper(where); }
static void Destruct(const int /* type */, void *where) { qMetaTypeDestructHelper<T>(where); }
};
template<typename T>
struct DestructorImpl<T, /* IsAcceptedType = */ false> {

View File

@ -318,46 +318,57 @@ private:
Q_DECLARE_OPERATORS_FOR_FLAGS(QMetaType::TypeFlags)
template <typename T>
void qMetaTypeDeleteHelper(T *t)
void qMetaTypeDeleteHelper(void *t)
{
delete t;
delete static_cast<T*>(t);
}
template <> inline void qMetaTypeDeleteHelper<void>(void *) {}
template <typename T>
void *qMetaTypeCreateHelper(const T *t)
void *qMetaTypeCreateHelper(const void *t)
{
if (t)
return new T(*static_cast<const T*>(t));
return new T();
}
template <typename T>
void qMetaTypeDestructHelper(T *t)
{
Q_UNUSED(t) // Silence MSVC that warns for POD types.
t->~T();
}
template <> inline void *qMetaTypeCreateHelper<void>(const void *) { return 0; }
template <typename T>
void *qMetaTypeConstructHelper(void *where, const T *t)
void qMetaTypeDestructHelper(void *t)
{
Q_UNUSED(t) // Silence MSVC that warns for POD types.
static_cast<T*>(t)->~T();
}
template <> inline void qMetaTypeDestructHelper<void>(void *) {}
template <typename T>
void *qMetaTypeConstructHelper(void *where, const void *t)
{
if (t)
return new (where) T(*static_cast<const T*>(t));
return new (where) T;
}
template <> inline void *qMetaTypeConstructHelper<void>(void *, const void *) { return 0; }
#ifndef QT_NO_DATASTREAM
template <typename T>
void qMetaTypeSaveHelper(QDataStream &stream, const T *t)
void qMetaTypeSaveHelper(QDataStream &stream, const void *t)
{
stream << *t;
stream << *static_cast<const T*>(t);
}
template <> inline void qMetaTypeSaveHelper<void>(QDataStream &, const void *) {}
template <typename T>
void qMetaTypeLoadHelper(QDataStream &stream, T *t)
void qMetaTypeLoadHelper(QDataStream &stream, void *t)
{
stream >> *t;
stream >> *static_cast<T*>(t);
}
template <> inline void qMetaTypeLoadHelper<void>(QDataStream &, void *) {}
#endif // QT_NO_DATASTREAM
template <typename T>
@ -442,15 +453,6 @@ int qRegisterMetaType(const char *typeName
if (typedefOf != -1)
return QMetaType::registerTypedef(typeName, typedefOf);
typedef void*(*CreatePtr)(const T*);
CreatePtr cptr = qMetaTypeCreateHelper<T>;
typedef void(*DeletePtr)(T*);
DeletePtr dptr = qMetaTypeDeleteHelper<T>;
typedef void*(*ConstructPtr)(void *, const T*);
ConstructPtr ipcptr = qMetaTypeConstructHelper<T>;
typedef void(*DestructPtr)(T*);
DestructPtr ipdptr = qMetaTypeDestructHelper<T>;
QMetaType::TypeFlags flags;
if (!QTypeInfo<T>::isStatic)
flags |= QMetaType::MovableType;
@ -461,10 +463,10 @@ int qRegisterMetaType(const char *typeName
if (QtPrivate::IsPointerToTypeDerivedFromQObject<T>::Value)
flags |= QMetaType::PointerToQObject;
return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Deleter>(dptr),
reinterpret_cast<QMetaType::Creator>(cptr),
reinterpret_cast<QMetaType::Destructor>(ipdptr),
reinterpret_cast<QMetaType::Constructor>(ipcptr),
return QMetaType::registerType(typeName, qMetaTypeDeleteHelper<T>,
qMetaTypeCreateHelper<T>,
qMetaTypeDestructHelper<T>,
qMetaTypeConstructHelper<T>,
sizeof(T),
flags);
}
@ -477,14 +479,8 @@ void qRegisterMetaTypeStreamOperators(const char *typeName
#endif
)
{
typedef void(*SavePtr)(QDataStream &, const T *);
typedef void(*LoadPtr)(QDataStream &, T *);
SavePtr sptr = qMetaTypeSaveHelper<T>;
LoadPtr lptr = qMetaTypeLoadHelper<T>;
qRegisterMetaType<T>(typeName);
QMetaType::registerStreamOperators(typeName, reinterpret_cast<QMetaType::SaveOperator>(sptr),
reinterpret_cast<QMetaType::LoadOperator>(lptr));
QMetaType::registerStreamOperators(typeName, qMetaTypeSaveHelper<T>, qMetaTypeLoadHelper<T>);
}
#endif // QT_NO_DATASTREAM
@ -516,16 +512,8 @@ inline int qRegisterMetaType(
template <typename T>
inline int qRegisterMetaTypeStreamOperators()
{
typedef void(*SavePtr)(QDataStream &, const T *);
typedef void(*LoadPtr)(QDataStream &, T *);
SavePtr sptr = qMetaTypeSaveHelper<T>;
LoadPtr lptr = qMetaTypeLoadHelper<T>;
register int id = qMetaTypeId<T>();
QMetaType::registerStreamOperators(id,
reinterpret_cast<QMetaType::SaveOperator>(sptr),
reinterpret_cast<QMetaType::LoadOperator>(lptr));
QMetaType::registerStreamOperators(id, qMetaTypeSaveHelper<T>, qMetaTypeLoadHelper<T>);
return id;
}
#endif

View File

@ -122,31 +122,6 @@ QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_DECLARE_WIDGETS_MODULE_TYPES_ITER)
class QMetaTypeInterface
{
public:
template<typename T>
struct Impl {
static void *creator(const T *t)
{
if (t)
return new T(*t);
return new T();
}
static void deleter(T *t) { delete t; }
static void saver(QDataStream &stream, const T *t) { stream << *t; }
static void loader(QDataStream &stream, T *t) { stream >> *t; }
static void destructor(T *t)
{
Q_UNUSED(t) // Silence MSVC that warns for POD types.
t->~T();
}
static void *constructor(void *where, const T *t)
{
if (t)
return new (where) T(*static_cast<const T*>(t));
return new (where) T;
}
};
QMetaType::Creator creator;
QMetaType::Deleter deleter;
QMetaType::SaveOperator saveOp;
@ -157,20 +132,10 @@ public:
quint32 flags; // same as QMetaType::TypeFlags
};
template<>
struct QMetaTypeInterface::Impl<void> {
static void *creator(const void *) { return 0; }
static void deleter(void *) {}
static void saver(QDataStream &, const void *) {}
static void loader(QDataStream &, void *) {}
static void destructor(void *){}
static void *constructor(void *, const void *) { return 0; }
};
#ifndef QT_NO_DATASTREAM
# define QT_METATYPE_INTERFACE_INIT_DATASTREAM_IMPL(Type) \
/*saveOp*/(reinterpret_cast<QMetaType::SaveOperator>(QMetaTypeInterface::Impl<Type>::saver)), \
/*loadOp*/(reinterpret_cast<QMetaType::LoadOperator>(QMetaTypeInterface::Impl<Type>::loader)),
/*saveOp*/(qMetaTypeSaveHelper<Type>), \
/*loadOp*/(qMetaTypeLoadHelper<Type>),
# define QT_METATYPE_INTERFACE_INIT_EMPTY_DATASTREAM_IMPL(Type) \
/*saveOp*/ 0, \
/*loadOp*/ 0,
@ -184,11 +149,11 @@ struct QMetaTypeInterface::Impl<void> {
#define QT_METATYPE_INTERFACE_INIT_IMPL(Type, DATASTREAM_DELEGATE) \
{ \
/*creator*/(reinterpret_cast<QMetaType::Creator>(QMetaTypeInterface::Impl<Type>::creator)), \
/*deleter*/(reinterpret_cast<QMetaType::Deleter>(QMetaTypeInterface::Impl<Type>::deleter)), \
/*creator*/(qMetaTypeCreateHelper<Type>), \
/*deleter*/(qMetaTypeDeleteHelper<Type>), \
DATASTREAM_DELEGATE(Type) \
/*constructor*/(reinterpret_cast<QMetaType::Constructor>(QMetaTypeInterface::Impl<Type>::constructor)), \
/*destructor*/(reinterpret_cast<QMetaType::Destructor>(QMetaTypeInterface::Impl<Type>::destructor)), \
/*constructor*/(qMetaTypeConstructHelper<Type>), \
/*destructor*/(qMetaTypeDestructHelper<Type>), \
/*size*/(QTypeInfo<Type>::sizeOf), \
/*flags*/(!QTypeInfo<Type>::isStatic * QMetaType::MovableType) \
| (QTypeInfo<Type>::isComplex * QMetaType::NeedsConstruction) \

View File

@ -268,9 +268,7 @@ namespace QtTestInternal
struct Getter {
static QMetaType::SaveOperator saveOp()
{
typedef void(*SavePtr)(QDataStream &, const T *);
SavePtr op = ::qMetaTypeSaveHelper<T>;
return reinterpret_cast<QMetaType::SaveOperator>(op);
return ::qMetaTypeSaveHelper<T>;
}
};