[libcxx-commits] [libcxx] be28e55 - [libc++] Memoise `size()` in `vector::__assign_with_size` (#180288)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 16 14:39:16 PST 2026


Author: Christopher Di Bella
Date: 2026-02-16T22:39:11Z
New Revision: be28e55d2d76dfa6f75a282d84a7edfb483b2470

URL: https://github.com/llvm/llvm-project/commit/be28e55d2d76dfa6f75a282d84a7edfb483b2470
DIFF: https://github.com/llvm/llvm-project/commit/be28e55d2d76dfa6f75a282d84a7edfb483b2470.diff

LOG: [libc++] Memoise `size()` in `vector::__assign_with_size` (#180288)

This is an optimisation from the size-based vector project that's
applicable to all vector implementations.

Added: 
    

Modified: 
    libcxx/include/__vector/vector.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index e28ceb7f69e96..d1c3f7ddcec1c 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -1048,9 +1048,10 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
 vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last, 
diff erence_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