From cc6b80a55b66594256b74d02034eb7bf58933f9c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 14 Jun 2024 00:17:07 +0300 Subject: [PATCH] BAN: Optimize Vector copy assignment to reduce allocations If vector contains enough elements, it will now replace old elements instead of clearing and reallocating --- BAN/include/BAN/Vector.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 2674bd6942..039e264986 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -138,10 +138,13 @@ namespace BAN template Vector& Vector::operator=(const Vector& other) { - clear(); MUST(ensure_capacity(other.size())); - for (size_type i = 0; i < other.size(); i++) + for (size_type i = 0; i < BAN::Math::min(size(), other.size()); i++) + m_data[i] = other.m_data[i]; + for (size_type i = size(); i < other.size(); i++) new (m_data + i) T(other[i]); + for (size_type i = other.size(); i < size(); i++) + m_data[i].~T(); m_size = other.m_size; return *this; }