[libcxx-commits] [libcxx] Optimize __assign_with_sentinel in std::vector (PR #113852)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 11 13:39:48 PST 2024


================
@@ -1017,9 +1017,14 @@ template <class _Tp, class _Allocator>
 template <class _Iterator, class _Sentinel>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
 vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
-  clear();
-  for (; __first != __last; ++__first)
-    emplace_back(*__first);
+  pointer __cur = __begin_;
+  for (; __first != __last && __cur != __end_; ++__first, (void)++__cur)
+    *__cur = *__first;
+  if (__cur != __end_)
+    __destruct_at_end(__cur);
+  else
+    for (; __first != __last; ++__first)
+      emplace_back(*__first);
----------------
philnik777 wrote:

```suggestion
  if (__cur != __end_) {
    __destruct_at_end(__cur);
  } else {
    for (; __first != __last; ++__first)
      emplace_back(*__first);
  }
```


https://github.com/llvm/llvm-project/pull/113852


More information about the libcxx-commits mailing list