Always use the variants internal space if possible

There's no point in storing small types with an external
refcount, even if they aren't movable. Simply copying
the type should be faster in pretty much all cases, while
this uses less memory.

Change-Id: I127474f8e3c5fa042f530684f9d5bfccbba134ca
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Lars Knoll 2020-07-10 13:39:20 +02:00
parent cb17157b27
commit 76e8e8e9c8
3 changed files with 5 additions and 5 deletions

View File

@ -1398,8 +1398,7 @@ static void customConstruct(QVariant::Private *d, const void *copy)
}
// this logic should match with QVariantIntegrator::CanUseInternalSpace
if (size <= sizeof(QVariant::Private::Data)
&& (type.flags() & (QMetaType::MovableType | QMetaType::IsEnumeration))) {
if (size <= sizeof(QVariant::Private::Data)) {
type.construct(&d->data, copy);
d->is_shared = false;
} else {

View File

@ -63,8 +63,7 @@ QT_BEGIN_NAMESPACE
template<typename T>
struct QVariantIntegrator
{
static const bool CanUseInternalSpace = sizeof(T) <= sizeof(QVariant::Private::Data)
&& ((QTypeInfoQuery<T>::isRelocatable) || std::is_enum<T>::value);
static const bool CanUseInternalSpace = sizeof(T) <= sizeof(QVariant::Private::Data);
typedef std::integral_constant<bool, CanUseInternalSpace> CanUseInternalSpace_t;
};
static_assert(QVariantIntegrator<double>::CanUseInternalSpace);

View File

@ -1909,7 +1909,7 @@ void tst_QVariant::userType()
QCOMPARE(instanceCount, 3);
{
QVariant second = myCarrier;
QCOMPARE(instanceCount, 3);
QCOMPARE(instanceCount, 4);
second.detach();
QCOMPARE(instanceCount, 4);
}
@ -3315,6 +3315,8 @@ struct MyNotMovable
if (!ok) qFatal("MyNotMovable has been moved");
return ok;
}
// Make it too big to store it in the variant itself
void *dummy[4];
};
int MyNotMovable::count = 0;