AK: Stop Vector::extend from unnecessary reallocation

Previously, Vector::extend for a moved vector would move the other
vector into this vector if this vector was empty, thereby throwing away
existing allocated capacity. Therefore, this commit allows the move to
only happen if this vector's capacity is too small to fit the other
vector. This will also alleviate bugs where callers relied on the
capacity to never shrink with calls to unchecked_append, extend and the
like.
This commit is contained in:
kleines Filmröllchen 2021-11-28 17:24:53 +01:00 committed by Brian Gianforcaro
parent 05cb499d58
commit 295eec2d49

View file

@ -491,7 +491,7 @@ public:
ErrorOr<void> try_extend(Vector&& other)
{
if (is_empty()) {
if (is_empty() && capacity() <= other.capacity()) {
*this = move(other);
return {};
}