1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 22:07:36 +00:00

Vector: Use memmove() for moving trivial types around more

This can definitely be improved with better trivial type detection and
by using the TypedTransfer template in more places.

It's a bit annoying that we can't get <type_traits> in Vector.h since
it's included in the toolchain compilation before we have libstdc++.
This commit is contained in:
Andreas Kling 2019-08-07 11:53:21 +02:00
parent bebe7c4cff
commit b48b6c0caa
2 changed files with 38 additions and 5 deletions

View file

@ -63,6 +63,30 @@ private:
int m_index { 0 };
};
template<typename T>
class TypedTransfer {
public:
static void move(T* destination, T* source, size_t count)
{
if constexpr (Traits<T>::is_trivial()) {
memmove(destination, source, count * sizeof(T));
return;
}
for (size_t i = 0; i < count; ++i)
new (&destination[i]) T(AK::move(source[i]));
}
static void copy(T* destination, const T* source, size_t count)
{
if constexpr (Traits<T>::is_trivial()) {
memmove(destination, source, count * sizeof(T));
return;
}
for (size_t i = 0; i < count; ++i)
new (&destination[i]) T(source[i]);
}
};
template<typename T, int inline_capacity = 0>
class Vector {
public:
@ -379,9 +403,7 @@ public:
}
Vector tmp = move(other);
for (int i = 0; i < tmp.size(); ++i)
new (slot(i)) T(move(tmp.at(i)));
TypedTransfer<T>::move(slot(0), tmp.data(), tmp.size());
m_size += other_size;
}
@ -390,8 +412,7 @@ public:
if (!count)
return;
grow_capacity(size() + count);
for (int i = 0; i < count; ++i)
new (slot(m_size + i)) T(values[i]);
TypedTransfer<T>::copy(slot(m_size), values, count);
m_size += count;
}