Skip to content

Commit 295eec2

Browse files
kleines Filmröllchenbgianfo
authored andcommitted
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.
1 parent 05cb499 commit 295eec2

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

AK/Vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ requires(!IsRvalueReference<T>) class Vector {
491491

492492
ErrorOr<void> try_extend(Vector&& other)
493493
{
494-
if (is_empty()) {
494+
if (is_empty() && capacity() <= other.capacity()) {
495495
*this = move(other);
496496
return {};
497497
}

0 commit comments

Comments
 (0)