[libcxx-commits] [libcxx] [libc++] Optimize vector::erase (PR #213916)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Aug 12 07:20:35 PDT 2026
================
@@ -431,6 +431,44 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __uninitialized_allocat
}
}
+// __uninitialized_allocator_relocate_forward relocates the objects in [__first, __last) into __result
+// element-by-element from __first to __last. Relocation means that the objects in [__first, __last) are placed into
+// __result as-if by move-construct and destroy, except that the move constructor and destructor may never be called if
+// they are known to be equivalent to a memcpy.
+//
+// Preconditions: At __result there is no object and [__first, __last) contains objects
+// Postconditions: __result contains the objects from [__first, __last) and
+// [__first, __last) doesn't contain any objects
+//
+// The strong exception guarantee is provided.
+template <class _Alloc, class _ContiguousIterator>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __uninitialized_allocator_relocate_forward(
+ _Alloc& __alloc, _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) {
+ static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "");
+ using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type;
+ static_assert(
+ __is_nothrow_relocatable_v<_ValueType> && __allocator_has_trivial_move_construct_v<_Alloc, _ValueType> &&
+ __allocator_has_trivial_destroy_v<_Alloc, _ValueType>);
+ if (__libcpp_is_constant_evaluated() || !__is_trivially_relocatable_v<_ValueType>) {
+ auto __iter = __first;
+ while (__iter != __last) {
+#if _LIBCPP_HAS_EXCEPTIONS
+ allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move_if_noexcept(*__iter));
+#else
+ allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move(*__iter));
+#endif
----------------
ldionne wrote:
This should simplify if don't provide the strong exception safety guarantee.
https://github.com/llvm/llvm-project/pull/213916
More information about the libcxx-commits
mailing list