mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
Vector: Use TypedTransfer in more parts of Vector
Make more Vector-of-trivial-type operations go fast :^)
This commit is contained in:
parent
e8e85f5457
commit
6da6ca64d2
Notes:
sideshowbarker
2024-07-19 12:50:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/6da6ca64d23
2 changed files with 24 additions and 9 deletions
|
@ -167,4 +167,18 @@ TEST_CASE(vector_compare)
|
|||
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)
|
||||
|
|
19
AK/Vector.h
19
AK/Vector.h
|
@ -142,8 +142,8 @@ public:
|
|||
Vector(const Vector& other)
|
||||
{
|
||||
ensure_capacity(other.size());
|
||||
for (int i = 0; i < other.size(); ++i)
|
||||
unchecked_append(other[i]);
|
||||
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
||||
m_size = other.size();
|
||||
}
|
||||
|
||||
// FIXME: What about assigning from a vector with lower inline capacity?
|
||||
|
@ -311,8 +311,8 @@ public:
|
|||
if (this != &other) {
|
||||
clear();
|
||||
ensure_capacity(other.size());
|
||||
for (const auto& v : other)
|
||||
unchecked_append(v);
|
||||
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
||||
m_size = other.size();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
@ -323,17 +323,18 @@ public:
|
|||
*this = move(other);
|
||||
return;
|
||||
}
|
||||
auto other_size = other.size();
|
||||
Vector tmp = move(other);
|
||||
grow_capacity(size() + tmp.size());
|
||||
for (auto&& v : tmp)
|
||||
unchecked_append(move(v));
|
||||
grow_capacity(size() + other_size);
|
||||
TypedTransfer<T>::move(data() + m_size, tmp.data(), other_size);
|
||||
m_size += other_size;
|
||||
}
|
||||
|
||||
void append(const Vector& other)
|
||||
{
|
||||
grow_capacity(size() + other.size());
|
||||
for (auto& value : other)
|
||||
unchecked_append(value);
|
||||
TypedTransfer<T>::copy(data() + m_size, other.data(), other.size());
|
||||
m_size += other.m_size;
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
|
|
Loading…
Add table
Reference in a new issue