[libcxx-commits] [libcxx] [libc++] Memoise `size()` in `vector::__assign_with_size` (PR #180288)
Christopher Di Bella via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Feb 16 14:35:42 PST 2026
https://github.com/cjdb updated https://github.com/llvm/llvm-project/pull/180288
>From eb58f3659380b6c72cc55a3cba4aa908340f5c2f Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 6 Feb 2026 21:37:38 +0000
Subject: [PATCH] [libc++] Memoise `size()` in `vector::__assign_with_size`
This is an optimisation from the size-based vector project that's
applicable to all vector implementations.
---
libcxx/include/__vector/vector.h | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 9747575bedafc..c9be96224ec64 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -1051,9 +1051,10 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n) {
size_type __new_size = static_cast<size_type>(__n);
if (__new_size <= capacity()) {
- if (__new_size > size()) {
- auto __mid = std::__copy_n<_AlgPolicy>(std::move(__first), size(), this->__begin_).first;
- __construct_at_end(std::move(__mid), std::move(__last), __new_size - size());
+ auto const __size = size();
+ if (__new_size > __size) {
+ auto __mid = std::__copy_n<_AlgPolicy>(std::move(__first), __size, this->__begin_).first;
+ __construct_at_end(std::move(__mid), std::move(__last), __new_size - __size);
} else {
pointer __m = std::__copy(std::move(__first), __last, this->__begin_).second;
this->__destruct_at_end(__m);
More information about the libcxx-commits
mailing list