mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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:
parent
bebe7c4cff
commit
b48b6c0caa
Notes:
sideshowbarker
2024-07-19 12:50:46 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/b48b6c0caaf
2 changed files with 38 additions and 5 deletions
12
AK/Traits.h
12
AK/Traits.h
|
@ -12,32 +12,44 @@ struct GenericTraits {
|
|||
|
||||
template<typename T>
|
||||
struct Traits : public GenericTraits<T> {
|
||||
static constexpr bool is_trivial() { return false; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<int> : public GenericTraits<int> {
|
||||
static constexpr bool is_trivial() { return true; }
|
||||
static unsigned hash(int i) { return int_hash(i); }
|
||||
static void dump(int i) { kprintf("%d", i); }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<unsigned> : public GenericTraits<unsigned> {
|
||||
static constexpr bool is_trivial() { return true; }
|
||||
static unsigned hash(unsigned u) { return int_hash(u); }
|
||||
static void dump(unsigned u) { kprintf("%u", u); }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<u16> : public GenericTraits<u16> {
|
||||
static constexpr bool is_trivial() { return true; }
|
||||
static unsigned hash(u16 u) { return int_hash(u); }
|
||||
static void dump(u16 u) { kprintf("%u", u); }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<char> : public GenericTraits<char> {
|
||||
static constexpr bool is_trivial() { return true; }
|
||||
static unsigned hash(char c) { return int_hash(c); }
|
||||
static void dump(char c) { kprintf("%c", c); }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Traits<T*> {
|
||||
static unsigned hash(const T* p)
|
||||
{
|
||||
return int_hash((unsigned)(__PTRDIFF_TYPE__)p);
|
||||
}
|
||||
static constexpr bool is_trivial() { return true; }
|
||||
static void dump(const T* p) { kprintf("%p", p); }
|
||||
static bool equals(const T* a, const T* b) { return a == b; }
|
||||
};
|
||||
|
|
31
AK/Vector.h
31
AK/Vector.h
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue