From 295eec2d49b0c361a99915cbcabf33a0a296df32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 28 Nov 2021 17:24:53 +0100 Subject: [PATCH] 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. --- AK/Vector.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AK/Vector.h b/AK/Vector.h index 751952b987b..327be79be86 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -491,7 +491,7 @@ public: ErrorOr try_extend(Vector&& other) { - if (is_empty()) { + if (is_empty() && capacity() <= other.capacity()) { *this = move(other); return {}; }