Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libcxx/docs/ReleaseNotes/22.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Improvements and New Features
in chunks into a buffer.
- Multiple internal types have been refactored to use ``[[no_unique_address]]``, resulting in faster compile times and
reduced debug information.
- The performance of ``deque::append_range`` has been improved by up to 3.4x

- The performance of ``std::find`` has been improved by up to 2x for integral types

Expand Down
17 changes: 16 additions & 1 deletion libcxx/include/__vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <__memory/temp_value.h>
#include <__memory/uninitialized_algorithms.h>
#include <__ranges/access.h>
#include <__ranges/as_rvalue_view.h>
#include <__ranges/concepts.h>
#include <__ranges/container_compatible_range.h>
#include <__ranges/from_range.h>
Expand Down Expand Up @@ -489,7 +490,21 @@ class vector {
#if _LIBCPP_STD_VER >= 23
template <_ContainerCompatibleRange<_Tp> _Range>
_LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
insert_range(end(), std::forward<_Range>(__range));
if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
auto __len = ranges::distance(__range);
if (__len < __cap_ - __end_) {
__construct_at_end(ranges::begin(__range), ranges::end(__range), __len);
} else {
__split_buffer<value_type, allocator_type&> __buffer(__recommend(size() + __len), size(), __alloc_);
__buffer.__construct_at_end_with_size(ranges::begin(__range), __len);
__swap_out_circular_buffer(__buffer);
}
} else {
vector __buffer(__alloc_);
for (auto&& __val : __range)
__buffer.emplace_back(std::forward<decltype(__val)>(__val));
append_range(ranges::as_rvalue_view(__buffer));
}
}
#endif

Expand Down
6 changes: 5 additions & 1 deletion libcxx/include/deque
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,11 @@ public:

template <_ContainerCompatibleRange<_Tp> _Range>
_LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
insert_range(end(), std::forward<_Range>(__range));
if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
__append_with_size(ranges::begin(__range), ranges::distance(__range));
} else {
__append_with_sentinel(ranges::begin(__range), ranges::end(__range));
}
}
# endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void sequence_container_benchmarks(std::string container) {
}

/////////////////////////
// Variations of push_back
// Appending elements
/////////////////////////
static constexpr bool has_push_back = requires(Container c, ValueType v) { c.push_back(v); };
static constexpr bool has_capacity = requires(Container c) { c.capacity(); };
Expand Down Expand Up @@ -399,6 +399,53 @@ void sequence_container_benchmarks(std::string container) {
st.ResumeTiming();
}
});

#if TEST_STD_VER >= 23
for (auto gen : generators)
bench("append_range()" + tostr(gen), [gen](auto& state) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bench("append_range()" + tostr(gen), [gen](auto& state) {
bench("append_range() (empty)" + tostr(gen), [gen](auto& state) {

Or do we want to instead benchmark it with some elements already in the sequence?

auto const size = state.range(0);
std::vector<ValueType> in;
std::generate_n(std::back_inserter(in), size, gen);
DoNotOptimizeData(in);

Container c;
DoNotOptimizeData(c);
for (auto _ : state) {
c.append_range(in);
DoNotOptimizeData(c);

state.PauseTiming();
c.clear();
state.ResumeTiming();
}
});
#endif
}

/////////////////////////
// Prepending elements
/////////////////////////
static constexpr bool has_prepend_range = requires(Container c, std::vector<ValueType> v) { c.prepend_range(v); };

if constexpr (has_prepend_range) {
for (auto gen : generators)
bench("prepend_range()" + tostr(gen), [gen](auto& state) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bench("prepend_range()" + tostr(gen), [gen](auto& state) {
bench("prepend_range() (empty)" + tostr(gen), [gen](auto& state) {

Same comment as above.

auto const size = state.range(0);
std::vector<ValueType> in;
std::generate_n(std::back_inserter(in), size, gen);
DoNotOptimizeData(in);

Container c;
DoNotOptimizeData(c);
for (auto _ : state) {
c.prepend_range(in);
DoNotOptimizeData(c);

state.PauseTiming();
c.clear();
state.ResumeTiming();
}
});
}

/////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions libcxx/test/std/containers/insert_range_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ constexpr void for_all_iterators_and_allocators(Func f) {
f.template operator()<Iter, sentinel_wrapper<Iter>, min_allocator<T>>();
f.template operator()<Iter, sentinel_wrapper<Iter>, safe_allocator<T>>();

f.template operator()<Iter, sized_sentinel<Iter>, std::allocator<T>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this can have an adverse effect on our tests compile-time. Yes it increases coverage, but the only row in the cross product that we really want to test is cpp20_input_iterator with a sized_sentinel. Maybe we should do that instead (and let's add a comment explaining why we're cherry-picking that row).

f.template operator()<Iter, sized_sentinel<Iter>, test_allocator<T>>();
f.template operator()<Iter, sized_sentinel<Iter>, min_allocator<T>>();
f.template operator()<Iter, sized_sentinel<Iter>, safe_allocator<T>>();

if constexpr (std::sentinel_for<Iter, Iter>) {
f.template operator()<Iter, Iter, std::allocator<T>>();
f.template operator()<Iter, Iter, test_allocator<T>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main(int, char**) {
});
});
test_sequence_append_range_move_only<std::deque>();
test_sequence_append_range_emplace_constructible<std::deque>();

test_append_range_exception_safety_throwing_copy<std::deque>();
test_append_range_exception_safety_throwing_allocator<std::deque, int>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ int main(int, char**) {
});
});
test_sequence_prepend_range_move_only<std::deque>();
// FIXME: This should work - see https://llvm.org/PR162605
// test_sequence_prepend_range_emplace_constructible<std::deque>();

test_prepend_range_exception_safety_throwing_copy<std::deque>();
test_prepend_range_exception_safety_throwing_allocator<std::deque, int>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ TEST_CONSTEXPR_CXX26 bool test() {
});
});
test_sequence_prepend_range_move_only<std::forward_list>();
test_sequence_prepend_range_emplace_constructible<std::forward_list>();

if (!TEST_IS_CONSTANT_EVALUATED) {
test_prepend_range_exception_safety_throwing_copy<std::forward_list>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,62 @@ constexpr void test_sequence_assign_range_move_only() {
c.assign_range(in);
}

struct InPlaceOnly {
constexpr InPlaceOnly() {}
InPlaceOnly(const InPlaceOnly&) = delete;
InPlaceOnly(InPlaceOnly&&) = delete;
InPlaceOnly& operator=(const InPlaceOnly&) = delete;
InPlaceOnly& operator=(InPlaceOnly&&) = delete;
};

struct EmplaceConstructible {
EmplaceConstructible(const EmplaceConstructible&) = delete;
EmplaceConstructible& operator=(const EmplaceConstructible&) = delete;
EmplaceConstructible& operator=(EmplaceConstructible&&) = delete;
EmplaceConstructible(EmplaceConstructible&&) = delete;
constexpr EmplaceConstructible(const InPlaceOnly&) {}
};

template <template <class...> class Container>
constexpr void test_sequence_append_range_emplace_constructible() {
InPlaceOnly input[5];
types::for_each(types::cpp20_input_iterator_list<InPlaceOnly*>{}, [&]<class Iter> {
std::ranges::subrange in(Iter(input), sentinel_wrapper<Iter>(Iter(input + 5)));
Container<EmplaceConstructible> c;
c.append_range(in);
});
}

template <template <class...> class Container>
constexpr void test_sequence_prepend_range_emplace_constructible() {
InPlaceOnly input[5];
types::for_each(types::cpp20_input_iterator_list<InPlaceOnly*>{}, [&]<class Iter> {
std::ranges::subrange in(Iter(input), sentinel_wrapper<Iter>(Iter(input + 5)));
Container<EmplaceConstructible> c;
c.prepend_range(in);
});
}

// vector has a special requirement that the type also has to be Cpp17MoveInsertable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

struct EmplaceConstructibleAndMoveInsertable {
EmplaceConstructibleAndMoveInsertable(const EmplaceConstructibleAndMoveInsertable&) = delete;
EmplaceConstructibleAndMoveInsertable& operator=(const EmplaceConstructibleAndMoveInsertable&) = delete;
EmplaceConstructibleAndMoveInsertable& operator=(EmplaceConstructibleAndMoveInsertable&&) = delete;
constexpr EmplaceConstructibleAndMoveInsertable(EmplaceConstructibleAndMoveInsertable&&) {}
constexpr EmplaceConstructibleAndMoveInsertable(const InPlaceOnly&) {}
};

template <template <class...> class Container>
constexpr void test_sequence_append_range_emplace_constructible_and_move_insertable() {
InPlaceOnly input[5];
types::for_each(types::cpp20_input_iterator_list<InPlaceOnly*>{}, [&]<class Iter> {
std::ranges::subrange in(Iter(input), sentinel_wrapper<Iter>(Iter(input + 5)));
Container<EmplaceConstructibleAndMoveInsertable> c;
c.append_range(in);
});
}

// Exception safety.

template <template <class...> class Container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ TEST_CONSTEXPR_CXX26 bool test() {
});
});
test_sequence_append_range_move_only<std::list>();
test_sequence_append_range_emplace_constructible<std::list>();

if (!TEST_IS_CONSTANT_EVALUATED) {
test_append_range_exception_safety_throwing_copy<std::list>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ TEST_CONSTEXPR_CXX26 bool test() {
});
});
test_sequence_prepend_range_move_only<std::list>();
test_sequence_prepend_range_emplace_constructible<std::list>();

if (!TEST_IS_CONSTANT_EVALUATED) {
test_prepend_range_exception_safety_throwing_copy<std::list>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ constexpr bool test() {
});
});
test_sequence_append_range_move_only<std::vector>();
test_sequence_append_range_emplace_constructible_and_move_insertable<std::vector>();

{ // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
{ // Ensure reallocation happens.
Expand Down
Loading