[libcxx-commits] [libcxx] Optimize input_iterator-pair `insert` for std::vector (PR #113768)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 14 08:37:11 PST 2025


================
@@ -1250,30 +1250,29 @@ vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _Inpu
   difference_type __off = __position - begin();
   pointer __p           = this->__begin_ + __off;
   pointer __old_last    = this->__end_;
-  for (; this->__end_ != this->__cap_ && __first != __last; ++__first) {
+  for (; this->__end_ != this->__cap_ && __first != __last; ++__first)
     __construct_one_at_end(*__first);
+
+  if (__first == __last)
+    (void)std::rotate(__p, __old_last, this->__end_);
+  else {
+    __split_buffer<value_type, allocator_type&> __v(__alloc_);
+    auto __guard = std::__make_exception_guard(
+        _AllocatorDestroyRangeReverse<allocator_type, pointer>(__alloc_, __old_last, this->__end_));
+    __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
+    __split_buffer<value_type, allocator_type&> __merged(__recommend(size() + __v.size()), __off, __alloc_);
----------------
ldionne wrote:

```suggestion
    __split_buffer<value_type, allocator_type&> __merged(__recommend(size() + __v.size()), __off, __alloc_); // has `__off` positions available at the front
```

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


More information about the libcxx-commits mailing list