Vector: Use TypedTransfer in more parts of Vector

Make more Vector-of-trivial-type operations go fast :^)
This commit is contained in:
Andreas Kling 2019-08-07 15:25:34 +02:00
parent e8e85f5457
commit 6da6ca64d2
Notes: sideshowbarker 2024-07-19 12:50:38 +09:00
2 changed files with 24 additions and 9 deletions

View File

@ -167,4 +167,18 @@ TEST_CASE(vector_compare)
EXPECT_EQ(strings, same_strings); EXPECT_EQ(strings, same_strings);
} }
BENCHMARK_CASE(vector_append_trivial)
{
// This should be super fast thanks to Vector using memmove.
Vector<int> ints;
for (int i = 0; i < 1000000; ++i) {
ints.append(i);
}
for (int i = 0; i < 100; ++i) {
Vector<int> tmp;
tmp.append(ints);
EXPECT_EQ(tmp.size(), 1000000);
}
}
TEST_MAIN(Vector) TEST_MAIN(Vector)

View File

@ -142,8 +142,8 @@ public:
Vector(const Vector& other) Vector(const Vector& other)
{ {
ensure_capacity(other.size()); ensure_capacity(other.size());
for (int i = 0; i < other.size(); ++i) TypedTransfer<T>::copy(data(), other.data(), other.size());
unchecked_append(other[i]); m_size = other.size();
} }
// FIXME: What about assigning from a vector with lower inline capacity? // FIXME: What about assigning from a vector with lower inline capacity?
@ -311,8 +311,8 @@ public:
if (this != &other) { if (this != &other) {
clear(); clear();
ensure_capacity(other.size()); ensure_capacity(other.size());
for (const auto& v : other) TypedTransfer<T>::copy(data(), other.data(), other.size());
unchecked_append(v); m_size = other.size();
} }
return *this; return *this;
} }
@ -323,17 +323,18 @@ public:
*this = move(other); *this = move(other);
return; return;
} }
auto other_size = other.size();
Vector tmp = move(other); Vector tmp = move(other);
grow_capacity(size() + tmp.size()); grow_capacity(size() + other_size);
for (auto&& v : tmp) TypedTransfer<T>::move(data() + m_size, tmp.data(), other_size);
unchecked_append(move(v)); m_size += other_size;
} }
void append(const Vector& other) void append(const Vector& other)
{ {
grow_capacity(size() + other.size()); grow_capacity(size() + other.size());
for (auto& value : other) TypedTransfer<T>::copy(data() + m_size, other.data(), other.size());
unchecked_append(value); m_size += other.m_size;
} }
template<typename Callback> template<typename Callback>