Skip to content

Commit 0869709

Browse files
committed
[libc++] Optimize vector growing of trivially relocatable types
1 parent 8346e86 commit 0869709

File tree

12 files changed

+265
-62
lines changed

12 files changed

+265
-62
lines changed

libcxx/benchmarks/ContainerBenchmarks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void BM_ConstructFromRange(benchmark::State& st, Container, GenInputs gen) {
8080
}
8181

8282
template <class Container>
83-
void BM_Pushback(benchmark::State& state, Container c) {
83+
void BM_Pushback_no_grow(benchmark::State& state, Container c) {
8484
int count = state.range(0);
8585
c.reserve(count);
8686
while (state.KeepRunningBatch(count)) {

libcxx/benchmarks/vector_operations.bench.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cstdint>
22
#include <cstdlib>
33
#include <cstring>
4+
#include <deque>
45
#include <functional>
56
#include <vector>
67

@@ -39,6 +40,21 @@ BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_size_t, std::vector<size_t>{}, g
3940
BENCHMARK_CAPTURE(BM_ConstructFromRange, vector_string, std::vector<std::string>{}, getRandomStringInputs)
4041
->Arg(TestNumInputs);
4142

42-
BENCHMARK_CAPTURE(BM_Pushback, vector_int, std::vector<int>{})->Arg(TestNumInputs);
43+
BENCHMARK_CAPTURE(BM_Pushback_no_grow, vector_int, std::vector<int>{})->Arg(TestNumInputs);
44+
45+
template <class T>
46+
void bm_grow(benchmark::State& state) {
47+
for (auto _ : state) {
48+
std::vector<T> vec;
49+
benchmark::DoNotOptimize(vec);
50+
for (size_t i = 0; i != 2048; ++i)
51+
vec.emplace_back();
52+
benchmark::DoNotOptimize(vec);
53+
}
54+
}
55+
BENCHMARK(bm_grow<int>);
56+
BENCHMARK(bm_grow<std::string>);
57+
BENCHMARK(bm_grow<std::unique_ptr<int>>);
58+
BENCHMARK(bm_grow<std::deque<int>>);
4359

4460
BENCHMARK_MAIN();

libcxx/docs/ReleaseNotes/18.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Improvements and New Features
6969
- ``std::for_each`` has been optimized for segmented iterators like ``std::deque::iterator`` in C++23 and
7070
later, which can lead up to 40x performance improvements.
7171

72+
- The performance of growing ``std::vector`` has been improved for trivially relocatable types.
73+
7274
- The library now provides several hardening modes under which common cases of library undefined behavior will be turned
7375
into a reliable program termination. The ``fast`` hardening mode enables a set of security-critical checks with
7476
minimal runtime overhead; the ``extensive`` hardening mode additionally enables relatively cheap checks that catch

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ set(files
803803
__type_traits/is_trivially_lexicographically_comparable.h
804804
__type_traits/is_trivially_move_assignable.h
805805
__type_traits/is_trivially_move_constructible.h
806+
__type_traits/is_trivially_relocatable.h
806807
__type_traits/is_unbounded_array.h
807808
__type_traits/is_union.h
808809
__type_traits/is_unsigned.h

libcxx/include/__fwd/string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <__availability>
1313
#include <__config>
1414
#include <__fwd/memory_resource.h>
15+
#include <__type_traits/is_fundamental.h>
16+
#include <__type_traits/is_trivially_relocatable.h>
1517

1618
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1719
# pragma GCC system_header

libcxx/include/__memory/uninitialized_algorithms.h

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <__type_traits/is_trivially_copy_constructible.h>
3030
#include <__type_traits/is_trivially_move_assignable.h>
3131
#include <__type_traits/is_trivially_move_constructible.h>
32+
#include <__type_traits/is_trivially_relocatable.h>
3233
#include <__type_traits/is_unbounded_array.h>
3334
#include <__type_traits/negation.h>
3435
#include <__type_traits/remove_const.h>
@@ -591,60 +592,48 @@ __uninitialized_allocator_copy(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1,
591592
return std::__rewrap_iter(__first2, __result);
592593
}
593594

594-
// Move-construct the elements [__first1, __last1) into [__first2, __first2 + N)
595-
// if the move constructor is noexcept, where N is distance(__first1, __last1).
596-
//
597-
// Otherwise try to copy all elements. If an exception is thrown the already copied
598-
// elements are destroyed in reverse order of their construction.
599-
template <class _Alloc, class _Iter1, class _Sent1, class _Iter2>
600-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2
601-
__uninitialized_allocator_move_if_noexcept(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1, _Iter2 __first2) {
602-
static_assert(__is_cpp17_move_insertable<_Alloc>::value,
603-
"The specified type does not meet the requirements of Cpp17MoveInsertable");
604-
auto __destruct_first = __first2;
605-
auto __guard =
606-
std::__make_exception_guard(_AllocatorDestroyRangeReverse<_Alloc, _Iter2>(__alloc, __destruct_first, __first2));
607-
while (__first1 != __last1) {
608-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
609-
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__first2), std::move_if_noexcept(*__first1));
610-
#else
611-
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__first2), std::move(*__first1));
612-
#endif
613-
++__first1;
614-
++__first2;
615-
}
616-
__guard.__complete();
617-
return __first2;
618-
}
619-
620595
template <class _Alloc, class _Type>
621596
struct __allocator_has_trivial_move_construct : _Not<__has_construct<_Alloc, _Type*, _Type&&> > {};
622597

623598
template <class _Type>
624599
struct __allocator_has_trivial_move_construct<allocator<_Type>, _Type> : true_type {};
625600

626-
#ifndef _LIBCPP_COMPILER_GCC
627-
template <
628-
class _Alloc,
629-
class _Iter1,
630-
class _Iter2,
631-
class _Type = typename iterator_traits<_Iter1>::value_type,
632-
class = __enable_if_t<is_trivially_move_constructible<_Type>::value && is_trivially_move_assignable<_Type>::value &&
633-
__allocator_has_trivial_move_construct<_Alloc, _Type>::value> >
634-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter2
635-
__uninitialized_allocator_move_if_noexcept(_Alloc&, _Iter1 __first1, _Iter1 __last1, _Iter2 __first2) {
636-
if (__libcpp_is_constant_evaluated()) {
637-
while (__first1 != __last1) {
638-
std::__construct_at(std::__to_address(__first2), std::move(*__first1));
639-
++__first1;
640-
++__first2;
601+
template <class _Alloc, class _Tp>
602+
struct __allocator_has_trivial_destroy : _Not<__has_destroy<_Alloc, _Tp*>> {};
603+
604+
template <class _Tp, class _Up>
605+
struct __allocator_has_trivial_destroy<allocator<_Tp>, _Up> : true_type {};
606+
607+
// __uninitialized_allocator_relocate relocates the objects in [__first, __last) into __result.
608+
// relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and destroy,
609+
// except that the move constructor and destructor may never be called if they are known to be equivalent to a memcpy.
610+
//
611+
// Preconditions: __result doesn't contain any objects and [__first, __last) contains objects
612+
// Postconditions: __result contains the object from [__first, __last) and [__first, __last) doesn't contain any objects
613+
template <class _Alloc, class _Tp>
614+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
615+
__uninitialized_allocator_relocate(_Alloc& __alloc, _Tp* __first, _Tp* __last, _Tp* __result) {
616+
if (__libcpp_is_constant_evaluated() || !__libcpp_is_trivially_relocatable<_Tp>::value ||
617+
!__allocator_has_trivial_move_construct<_Alloc, _Tp>::value ||
618+
!__allocator_has_trivial_destroy<_Alloc, _Tp>::value) {
619+
auto __destruct_first = __result;
620+
auto __guard =
621+
std::__make_exception_guard(_AllocatorDestroyRangeReverse<_Alloc, _Tp*>(__alloc, __destruct_first, __result));
622+
while (__first != __last) {
623+
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
624+
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move_if_noexcept(*__first));
625+
#else
626+
allocator_traits<_Alloc>::construct(__alloc, std::__to_address(__result), std::move(*__first));
627+
#endif
628+
++__first;
629+
++__result;
641630
}
642-
return __first2;
631+
__guard.__complete();
632+
std::__allocator_destroy(__alloc, __first, __last);
643633
} else {
644-
return std::move(__first1, __last1, __first2);
634+
__builtin_memcpy(__result, __first, sizeof(_Tp) * (__last - __first));
645635
}
646636
}
647-
#endif // _LIBCPP_COMPILER_GCC
648637

649638
_LIBCPP_END_NAMESPACE_STD
650639

libcxx/include/__memory/unique_ptr.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <__type_traits/is_reference.h>
3434
#include <__type_traits/is_same.h>
3535
#include <__type_traits/is_swappable.h>
36+
#include <__type_traits/is_trivially_relocatable.h>
3637
#include <__type_traits/is_void.h>
3738
#include <__type_traits/remove_extent.h>
3839
#include <__type_traits/type_identity.h>
@@ -129,6 +130,17 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
129130

130131
static_assert(!is_rvalue_reference<deleter_type>::value, "the specified deleter type cannot be an rvalue reference");
131132

133+
// A unique_ptr contains the following members which may be trivially relocatable:
134+
// - pointer : this may be trivially relocatable, so it's checked
135+
// - delter_type: this may be trivially relocatable, so it's checked
136+
//
137+
// This uniuqe_ptr implementation only contains a pointer to the unique object and a delter, so there are no
138+
// references to itself. This means that the entire structure is trivially relocatable if it's members are.
139+
using __trivially_relocatable = __conditional_t<
140+
__libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,
141+
unique_ptr,
142+
void>;
143+
132144
private:
133145
__compressed_pair<pointer, deleter_type> __ptr_;
134146

@@ -280,6 +292,17 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
280292
typedef _Dp deleter_type;
281293
typedef typename __pointer<_Tp, deleter_type>::type pointer;
282294

295+
// A unique_ptr contains the following members which may be trivially relocatable:
296+
// - pointer : this may be trivially relocatable, so it's checked
297+
// - delter_type: this may be trivially relocatable, so it's checked
298+
//
299+
// This uniuqe_ptr implementation only contains a pointer to the unique object and a delter, so there are no
300+
// references to itself. This means that the entire structure is trivially relocatable if it's members are.
301+
using __trivially_relocatable = __conditional_t<
302+
__libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,
303+
unique_ptr,
304+
void>;
305+
283306
private:
284307
__compressed_pair<pointer, deleter_type> __ptr_;
285308

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_RELOCATABLE_H
10+
#define _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_RELOCATABLE_H
11+
12+
#include <__config>
13+
#include <__type_traits/enable_if.h>
14+
#include <__type_traits/integral_constant.h>
15+
#include <__type_traits/is_same.h>
16+
#include <__type_traits/is_trivially_copyable.h>
17+
18+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19+
# pragma GCC system_header
20+
#endif
21+
22+
_LIBCPP_BEGIN_NAMESPACE_STD
23+
24+
#if __has_builtin(__is_trivially_relocatable)
25+
template <class _Tp, class = void>
26+
struct __libcpp_is_trivially_relocatable : integral_constant<bool, __is_trivially_relocatable(_Tp)> {};
27+
#else
28+
template <class _Tp, class = void>
29+
struct __libcpp_is_trivially_relocatable : is_trivially_copyable<_Tp> {};
30+
#endif
31+
32+
template <class _Tp>
33+
struct __libcpp_is_trivially_relocatable<_Tp,
34+
__enable_if_t<is_same<_Tp, typename _Tp::__trivially_relocatable>::value> >
35+
: true_type {};
36+
37+
_LIBCPP_END_NAMESPACE_STD
38+
39+
#endif // _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_RELOCATABLE_H

libcxx/include/string

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,20 @@ public:
724724
typedef typename __alloc_traits::pointer pointer;
725725
typedef typename __alloc_traits::const_pointer const_pointer;
726726

727+
// A basic_string contains the following members which may be trivially relocatable:
728+
// - pointer: is currently assumed to be trivially relocatable, but is still checked in case that changes
729+
// - size_type: is always trivially relocatable, since it has to be an integral type
730+
// - value_type: is always trivially relocatable, since it has to be trivial
731+
// - unsigned char: is a fundamental type, so it's trivially relocatable
732+
// - allocator_type: may or may not be trivially relocatable, so it's checked
733+
//
734+
// This string implementation doesn't contain any references into itself. It only contains a bit that says whether
735+
// it is in small or large string mode, so the entire structure is trivially relocatable if it's members are.
736+
using __trivially_relocatable = __conditional_t<
737+
__libcpp_is_trivially_relocatable<allocator_type>::value && __libcpp_is_trivially_relocatable<pointer>::value,
738+
basic_string,
739+
void>;
740+
727741
static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
728742
static_assert((is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
729743
static_assert((is_trivial<value_type>::value), "Character type of basic_string must be trivial");

libcxx/include/vector

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -982,37 +982,54 @@ template <ranges::input_range _Range,
982982
vector(from_range_t, _Range&&, _Alloc = _Alloc()) -> vector<ranges::range_value_t<_Range>, _Alloc>;
983983
#endif
984984

985+
// __swap_out_circular_buffer relocates the objects in [__begin_, __end_) into the front of __v and swaps the buffers of
986+
// *this and __v. It is assumed that __v provides space for exactly (__end_ - __begin_) objects in the front. This
987+
// function has a strong exception guarantee.
985988
template <class _Tp, class _Allocator>
986989
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
987990
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) {
988991
__annotate_delete();
989-
using _RevIter = std::reverse_iterator<pointer>;
990-
__v.__begin_ = std::__uninitialized_allocator_move_if_noexcept(
991-
__alloc(), _RevIter(__end_), _RevIter(__begin_), _RevIter(__v.__begin_))
992-
.base();
992+
auto __new_begin = __v.__begin_ - (__end_ - __begin_);
993+
std::__uninitialized_allocator_relocate(
994+
__alloc(), std::__to_address(__begin_), std::__to_address(__end_), std::__to_address(__new_begin));
995+
__v.__begin_ = __new_begin;
996+
__end_ = __begin_; // All the objects have been destroyed by relocating them.
993997
std::swap(this->__begin_, __v.__begin_);
994998
std::swap(this->__end_, __v.__end_);
995999
std::swap(this->__end_cap(), __v.__end_cap());
9961000
__v.__first_ = __v.__begin_;
9971001
__annotate_new(size());
9981002
}
9991003

1004+
// __swap_out_circular_buffer relocates the objects in [__begin_, __p) into the front of __v, the objects in
1005+
// [__p, __end_) into the back of __v and swaps the buffers of *this and __v. It is assumed that __v provides space for
1006+
// exactly (__p - __begin_) objects in the front and space for at least (__end_ - __p) objects in the back. This
1007+
// function has a strong exception guarantee if __begin_ == __p || __end_ == __p.
10001008
template <class _Tp, class _Allocator>
10011009
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
10021010
vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) {
10031011
__annotate_delete();
1004-
pointer __r = __v.__begin_;
1005-
using _RevIter = std::reverse_iterator<pointer>;
1006-
__v.__begin_ = std::__uninitialized_allocator_move_if_noexcept(
1007-
__alloc(), _RevIter(__p), _RevIter(__begin_), _RevIter(__v.__begin_))
1008-
.base();
1009-
__v.__end_ = std::__uninitialized_allocator_move_if_noexcept(__alloc(), __p, __end_, __v.__end_);
1012+
pointer __ret = __v.__begin_;
1013+
1014+
// Relocate [__p, __end_) first to avoid having a hole in [__begin_, __end_)
1015+
// in case something in [__begin_, __p) throws.
1016+
std::__uninitialized_allocator_relocate(
1017+
__alloc(), std::__to_address(__p), std::__to_address(__end_), std::__to_address(__v.__end_));
1018+
__v.__end_ += (__end_ - __p);
1019+
__end_ = __p; // The objects in [__p, __end_) have been destroyed by relocating them.
1020+
auto __new_begin = __v.__begin_ - (__p - __begin_);
1021+
1022+
std::__uninitialized_allocator_relocate(
1023+
__alloc(), std::__to_address(__begin_), std::__to_address(__p), std::__to_address(__new_begin));
1024+
__v.__begin_ = __new_begin;
1025+
__end_ = __begin_; // All the objects have been destroyed by relocating them.
1026+
10101027
std::swap(this->__begin_, __v.__begin_);
10111028
std::swap(this->__end_, __v.__end_);
10121029
std::swap(this->__end_cap(), __v.__end_cap());
10131030
__v.__first_ = __v.__begin_;
10141031
__annotate_new(size());
1015-
return __r;
1032+
return __ret;
10161033
}
10171034

10181035
template <class _Tp, class _Allocator>

libcxx/test/libcxx/type_traits/is_trivially_comparable.compile.pass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ static_assert(std::__libcpp_is_trivially_equality_comparable<
4747
"");
4848
static_assert(std::__libcpp_is_trivially_equality_comparable<char16_t, std::uint_least16_t>::value, "");
4949

50-
struct S {
50+
struct Empty {
5151
char c;
5252
};
5353

5454
struct S2 {
5555
char c;
5656
};
5757

58-
struct VirtualBase : virtual S {};
59-
struct NonVirtualBase : S, S2 {};
58+
struct VirtualBase : virtual Empty {};
59+
struct NonVirtualBase : Empty, S2 {};
6060

61-
static_assert(!std::__libcpp_is_trivially_equality_comparable<S*, VirtualBase*>::value, "");
61+
static_assert(!std::__libcpp_is_trivially_equality_comparable<Empty*, VirtualBase*>::value, "");
6262
static_assert(!std::__libcpp_is_trivially_equality_comparable<S2*, VirtualBase*>::value, "");
6363

6464
// This is trivially_equality_comparable, but we can't detect it currently
65-
static_assert(!std::__libcpp_is_trivially_equality_comparable<S*, NonVirtualBase*>::value, "");
65+
static_assert(!std::__libcpp_is_trivially_equality_comparable<Empty*, NonVirtualBase*>::value, "");

0 commit comments

Comments
 (0)