[libcxx-commits] [libcxx] [libc++][NFC] Use eary returns in some vector functions (PR #207367)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 3 03:37:54 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/207367

None

>From b9a27cc5a19e7f115d26393b7c798897dfc0d0cd Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 3 Jul 2026 12:37:32 +0200
Subject: [PATCH] [libc++][NFC] Use eary returns in some vector functions

---
 libcxx/include/__vector/vector.h | 101 ++++++++++++++++---------------
 1 file changed, 53 insertions(+), 48 deletions(-)

diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 2b9508ecafeac..20111c993c5b3 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -1198,29 +1198,30 @@ vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) {
 template <class _Tp, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
 vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) {
+  if (__n <= 0)
+    return __position;
+
   pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
-  if (__n > 0) {
-    if (__n <= __layout_.__remaining_capacity()) {
-      size_type __old_n  = __n;
-      pointer __end      = __layout_.__end_ptr();
-      pointer __old_last = __end;
-      if (__n > static_cast<size_type>(__end - __p)) {
-        size_type __cx = __n - (__end - __p);
-        __construct_at_end(__cx, __x);
-        __n -= __cx;
-      }
-      if (__n > 0) {
-        __move_range(__p, __old_last, __p + __old_n);
-        const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
-        if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end), std::addressof(__x)))
-          __xr += __old_n;
-        std::fill_n(__p, __n, *__xr);
-      }
-    } else {
-      _SplitBuffer __v(__recommend(size() + __n), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
-      __v.__construct_at_end(__n, __x);
-      __p = __layout_.__relocate_with_pivot(__v, __p);
+  if (__n <= __layout_.__remaining_capacity()) {
+    size_type __old_n  = __n;
+    pointer __end      = __layout_.__end_ptr();
+    pointer __old_last = __end;
+    if (__n > static_cast<size_type>(__end - __p)) {
+      size_type __cx = __n - (__end - __p);
+      __construct_at_end(__cx, __x);
+      __n -= __cx;
+    }
+    if (__n > 0) {
+      __move_range(__p, __old_last, __p + __old_n);
+      const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
+      if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end), std::addressof(__x)))
+        __xr += __old_n;
+      std::fill_n(__p, __n, *__xr);
     }
+  } else {
+    _SplitBuffer __v(__recommend(size() + __n), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
+    __v.__construct_at_end(__n, __x);
+    __p = __layout_.__relocate_with_pivot(__v, __p);
   }
   return __make_iter(__p);
 }
@@ -1271,36 +1272,40 @@ template <class _AlgPolicy, class _Iterator, class _Sentinel>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
 vector<_Tp, _Allocator>::__insert_with_size(
     const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n) {
+  if (__n <= 0)
+    return __position;
+
   pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
-  if (__n > 0) {
-    if (__n <= static_cast<difference_type>(__layout_.__remaining_capacity())) {
-      pointer __end        = __layout_.__end_ptr();
-      pointer __old_last   = __end;
-      difference_type __dx = __end - __p;
-      if (__n > __dx) {
+
+  if (__n > static_cast<difference_type>(__layout_.__remaining_capacity())) {
+    _SplitBuffer __v(__recommend(size() + __n), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
+    __v.__construct_at_end_with_size(std::move(__first), __n);
+    __p = __layout_.__relocate_with_pivot(__v, __p);
+    return __make_iter(__p);
+  }
+
+  pointer __end        = __layout_.__end_ptr();
+  difference_type __dx = __end - __p;
+
+  if (__n <= __dx) {
+    __move_range(__p, __end, __p + __n);
+    __insert_assign_n_unchecked<_AlgPolicy>(std::move(__first), __n, __p);
+    return __make_iter(__p);
+  }
+
+  pointer __old_last = __end;
 #if _LIBCPP_STD_VER >= 23
-        if constexpr (!forward_iterator<_Iterator>) {
-          __construct_at_end(std::move(__first), std::move(__last), __n);
-          std::rotate(__p, __old_last, __end);
-        } else
+  if constexpr (!forward_iterator<_Iterator>) {
+    __construct_at_end(std::move(__first), std::move(__last), __n);
+    std::rotate(__p, __old_last, __end);
+    return __make_iter(__p);
+  }
 #endif
-        {
-          _Iterator __m = std::next(__first, __dx);
-          __construct_at_end(__m, __last, __n - __dx);
-          if (__dx > 0) {
-            __move_range(__p, __old_last, __p + __n);
-            __insert_assign_n_unchecked<_AlgPolicy>(__first, __dx, __p);
-          }
-        }
-      } else {
-        __move_range(__p, __old_last, __p + __n);
-        __insert_assign_n_unchecked<_AlgPolicy>(std::move(__first), __n, __p);
-      }
-    } else {
-      _SplitBuffer __v(__recommend(size() + __n), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
-      __v.__construct_at_end_with_size(std::move(__first), __n);
-      __p = __layout_.__relocate_with_pivot(__v, __p);
-    }
+  _Iterator __m = std::next(__first, __dx);
+  __construct_at_end(__m, __last, __n - __dx);
+  if (__dx > 0) {
+    __move_range(__p, __old_last, __p + __n);
+    __insert_assign_n_unchecked<_AlgPolicy>(__first, __dx, __p);
   }
   return __make_iter(__p);
 }



More information about the libcxx-commits mailing list