[libcxx-commits] [libcxx] [libc++] Implement single element vector::insert in terms of emplace (PR #210284)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jul 17 02:36:01 PDT 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/210284
This also ports an optimization to `emplace`: if we copy/move construct the element we can avoid constructing a temporary.
>From 9e55faefd42d6637185809a4e9dfc1dd38049fb1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 17 Jul 2026 11:34:33 +0200
Subject: [PATCH] [libc++] Implement single element vector::insert in terms of
emplace
---
libcxx/include/__vector/vector.h | 65 ++++++++++----------------------
1 file changed, 20 insertions(+), 45 deletions(-)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 8226a7f87a119..22ee34dd86bc0 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -58,6 +58,7 @@
#include <__type_traits/is_same.h>
#include <__type_traits/is_swappable.h>
#include <__type_traits/is_trivially_relocatable.h>
+#include <__type_traits/remove_const_ref.h>
#include <__type_traits/type_identity.h>
#include <__utility/declval.h>
#include <__utility/exception_guard.h>
@@ -504,9 +505,14 @@ class vector {
this->__destruct_at_end(__layout_.__end_ptr() - 1);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, const_reference __x);
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, const_reference __x) {
+ return emplace(__position, __x);
+ }
+
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, value_type&& __x) {
+ return emplace(__position, std::move(__x));
+ }
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, value_type&& __x);
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __position, _Args&&... __args);
@@ -1128,49 +1134,6 @@ vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointe
std::move_backward(__from_s, __from_s + __n, __old_last);
}
-template <class _Tp, class _Allocator>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
-vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) {
- pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
- if (size() != capacity()) {
- pointer __end = __layout_.__end_ptr();
- if (__p == __end) {
- __emplace_back_assume_capacity(__x);
- } else {
- __move_range(__p, __end, __p + 1);
- 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;
- *__p = *__xr;
- }
- } else {
- _SplitBuffer __v(__recommend(size() + 1), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
- __v.emplace_back(__x);
- __p = __layout_.__relocate_with_pivot(__v, __p);
- }
- return __make_iter(__p);
-}
-
-template <class _Tp, class _Allocator>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
-vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) {
- pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
- if (size() != capacity()) {
- pointer __end = __layout_.__end_ptr();
- if (__p == __end) {
- __emplace_back_assume_capacity(std::move(__x));
- } else {
- __move_range(__p, __end, __p + 1);
- *__p = std::move(__x);
- }
- } else {
- _SplitBuffer __v(__recommend(size() + 1), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
- __v.emplace_back(std::move(__x));
- __p = __layout_.__relocate_with_pivot(__v, __p);
- }
- return __make_iter(__p);
-}
-
template <class _Tp, class _Allocator>
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
@@ -1181,6 +1144,18 @@ vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) {
if (__p == __end) {
__emplace_back_assume_capacity(std::forward<_Args>(__args)...);
} else {
+#ifndef _LIBCPP_CXX03_LANG
+ if constexpr (sizeof...(_Args) == 1) {
+ if constexpr (is_same<__remove_const_ref_t<_Args>..., value_type>::value) {
+ __move_range(__p, __end, __p + 1);
+ auto __xr = std::addressof(__args...);
+ if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end), std::addressof(__args)...))
+ ++__xr;
+ *__p = (std::forward<_Args>(*__xr), ...);
+ return __make_iter(__p);
+ }
+ }
+#endif
__temp_value<value_type, _Allocator> __tmp(this->__layout_.__alloc(), std::forward<_Args>(__args)...);
__move_range(__p, __end, __p + 1);
*__p = std::move(__tmp.get());
More information about the libcxx-commits
mailing list