[libcxx-commits] [libcxx] [libc++] Optimize vector assign for trivially copyable types (PR #233863)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Oct 21 07:55:17 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/233863.diff
1 Files Affected:
- (modified) libcxx/include/__vector/vector.h (+22)
``````````diff
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 37a25f20e65cd4..6dfea9b2046485 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -48,6 +48,7 @@
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
#include <__split_buffer>
+#include <__string/constexpr_c_functions.h>
#include <__type_traits/conditional.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_allocator.h>
@@ -59,6 +60,7 @@
#include <__type_traits/is_relocatable.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_swappable.h>
+#include <__type_traits/is_trivially_copyable.h>
#include <__type_traits/remove_const_ref.h>
#include <__type_traits/type_identity.h>
#include <__utility/assume.h>
@@ -968,6 +970,26 @@ template <class _AlgPolicy, class _Iterator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, difference_type __n) {
size_type __new_size = static_cast<size_type>(__n);
+
+#ifndef _LIBCPP_CXX03_LANG
+ if constexpr (is_same<value_type, __policy_value_type<_AlgPolicy, _Iterator>>::value &&
+ __allocator_has_trivial_move_construct_v<allocator_type, value_type> &&
+ __allocator_has_trivial_destroy_v<allocator_type, value_type> &&
+ is_trivially_copyable<value_type>::value && __libcpp_is_contiguous_iterator<_Iterator>::value) {
+ if (!__libcpp_is_constant_evaluated()) {
+ if (__new_size > capacity()) {
+ __vdeallocate();
+ __vallocate(__recommend(__new_size));
+ }
+
+ std::__constexpr_memmove(
+ std::__to_address(__layout_.__begin_ptr()), std::__to_address(__first), __element_count(__new_size));
+ __layout_.__set_bound_using_pointer(__layout_.__begin_ptr() + __new_size);
+ return;
+ }
+ }
+#endif
+
if (__new_size <= capacity()) {
auto const __size = size();
if (__new_size > __size) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/233863
More information about the libcxx-commits
mailing list