[libcxx-commits] [libcxx] [libc++][NFC] Simplify __split_buffer a bit (PR #114224)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 5 05:16:05 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
This is possible since we're not using `__compressed_pair` anymore.
---
Full diff: https://github.com/llvm/llvm-project/pull/114224.diff
3 Files Affected:
- (modified) libcxx/include/__split_buffer (+54-60)
- (modified) libcxx/include/__vector/vector.h (+2-2)
- (modified) libcxx/include/deque (+11-11)
``````````diff
diff --git a/libcxx/include/__split_buffer b/libcxx/include/__split_buffer
index 102df63e814ae4..2a2f2625c748b2 100644
--- a/libcxx/include/__split_buffer
+++ b/libcxx/include/__split_buffer
@@ -108,12 +108,6 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __alloc_; }
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT { return __alloc_; }
-
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_; }
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT { return __end_cap_; }
-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
@@ -129,7 +123,7 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
- return static_cast<size_type>(__end_cap() - __first_);
+ return static_cast<size_type>(__end_cap_ - __first_);
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
@@ -137,7 +131,7 @@ public:
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
- return static_cast<size_type>(__end_cap() - __end_);
+ return static_cast<size_type>(__end_cap_ - __end_);
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
@@ -196,7 +190,7 @@ public:
private:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
_NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
- __alloc() = std::move(__c.__alloc());
+ __alloc_ = std::move(__c.__alloc_);
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
@@ -225,14 +219,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants
return false;
if (__end_ != nullptr)
return false;
- if (__end_cap() != nullptr)
+ if (__end_cap_ != nullptr)
return false;
} else {
if (__begin_ < __first_)
return false;
if (__end_ < __begin_)
return false;
- if (__end_cap() < __end_)
+ if (__end_cap_ < __end_)
return false;
}
return true;
@@ -247,7 +241,7 @@ template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
_ConstructTransaction __tx(&this->__end_, __n);
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
- __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
+ __alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_));
}
}
@@ -262,7 +256,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
_ConstructTransaction __tx(&this->__end_, __n);
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
- __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
+ __alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), __x);
}
}
@@ -277,14 +271,14 @@ template <class _Tp, class _Allocator>
template <class _Iterator, class _Sentinel>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
- __alloc_rr& __a = this->__alloc();
+ __alloc_rr& __a = __alloc_;
for (; __first != __last; ++__first) {
- if (__end_ == __end_cap()) {
- size_type __old_cap = __end_cap() - __first_;
+ if (__end_ == __end_cap_) {
+ size_type __old_cap = __end_cap_ - __first_;
size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
__split_buffer __buf(__new_cap, 0, __a);
for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
- __alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
+ __alloc_traits::construct(__buf.__alloc_, std::__to_address(__buf.__end_), std::move(*__p));
swap(__buf);
}
__alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
@@ -304,7 +298,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
_ConstructTransaction __tx(&this->__end_, __n);
for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
- __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
+ __alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), *__first);
}
}
@@ -312,7 +306,7 @@ template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
while (__begin_ != __new_begin)
- __alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
+ __alloc_traits::destroy(__alloc_, std::__to_address(__begin_++));
}
template <class _Tp, class _Allocator>
@@ -325,7 +319,7 @@ template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
while (__new_last != __end_)
- __alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
+ __alloc_traits::destroy(__alloc_, std::__to_address(--__end_));
}
template <class _Tp, class _Allocator>
@@ -341,19 +335,19 @@ __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __sta
if (__cap == 0) {
__first_ = nullptr;
} else {
- auto __allocation = std::__allocate_at_least(__alloc(), __cap);
+ auto __allocation = std::__allocate_at_least(__alloc_, __cap);
__first_ = __allocation.ptr;
__cap = __allocation.count;
}
__begin_ = __end_ = __first_ + __start;
- __end_cap() = __first_ + __cap;
+ __end_cap_ = __first_ + __cap;
}
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
clear();
if (__first_)
- __alloc_traits::deallocate(__alloc(), __first_, capacity());
+ __alloc_traits::deallocate(__alloc_, __first_, capacity());
}
template <class _Tp, class _Allocator>
@@ -364,30 +358,30 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__
__end_(std::move(__c.__end_)),
__end_cap_(std::move(__c.__end_cap_)),
__alloc_(std::move(__c.__alloc_)) {
- __c.__first_ = nullptr;
- __c.__begin_ = nullptr;
- __c.__end_ = nullptr;
- __c.__end_cap() = nullptr;
+ __c.__first_ = nullptr;
+ __c.__begin_ = nullptr;
+ __c.__end_ = nullptr;
+ __c.__end_cap_ = nullptr;
}
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20
__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
: __end_cap_(nullptr), __alloc_(__a) {
- if (__a == __c.__alloc()) {
- __first_ = __c.__first_;
- __begin_ = __c.__begin_;
- __end_ = __c.__end_;
- __end_cap() = __c.__end_cap();
- __c.__first_ = nullptr;
- __c.__begin_ = nullptr;
- __c.__end_ = nullptr;
- __c.__end_cap() = nullptr;
+ if (__a == __c.__alloc_) {
+ __first_ = __c.__first_;
+ __begin_ = __c.__begin_;
+ __end_ = __c.__end_;
+ __end_cap_ = __c.__end_cap_;
+ __c.__first_ = nullptr;
+ __c.__begin_ = nullptr;
+ __c.__end_ = nullptr;
+ __c.__end_cap_ = nullptr;
} else {
- auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
+ auto __allocation = std::__allocate_at_least(__alloc_, __c.size());
__first_ = __allocation.ptr;
__begin_ = __end_ = __first_;
- __end_cap() = __first_ + __allocation.count;
+ __end_cap_ = __first_ + __allocation.count;
typedef move_iterator<iterator> _Ip;
__construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
}
@@ -401,12 +395,12 @@ __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
!__alloc_traits::propagate_on_container_move_assignment::value) {
clear();
shrink_to_fit();
- __first_ = __c.__first_;
- __begin_ = __c.__begin_;
- __end_ = __c.__end_;
- __end_cap() = __c.__end_cap();
+ __first_ = __c.__first_;
+ __begin_ = __c.__begin_;
+ __end_ = __c.__end_;
+ __end_cap_ = __c.__end_cap_;
__move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
- __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
+ __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap_ = nullptr;
return *this;
}
@@ -416,19 +410,19 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split
std::swap(__first_, __x.__first_);
std::swap(__begin_, __x.__begin_);
std::swap(__end_, __x.__end_);
- std::swap(__end_cap(), __x.__end_cap());
- std::__swap_allocator(__alloc(), __x.__alloc());
+ std::swap(__end_cap_, __x.__end_cap_);
+ std::__swap_allocator(__alloc_, __x.__alloc_);
}
template <class _Tp, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
if (__n < capacity()) {
- __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
+ __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
- std::swap(__end_cap(), __t.__end_cap());
+ std::swap(__end_cap_, __t.__end_cap_);
}
}
@@ -438,13 +432,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fi
#if _LIBCPP_HAS_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_EXCEPTIONS
- __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
+ __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
__t.__end_ = __t.__begin_ + (__end_ - __begin_);
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
- std::swap(__end_cap(), __t.__end_cap());
+ std::swap(__end_cap_, __t.__end_cap_);
#if _LIBCPP_HAS_EXCEPTIONS
} catch (...) {
}
@@ -456,45 +450,45 @@ template <class _Tp, class _Allocator>
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
if (__begin_ == __first_) {
- if (__end_ < __end_cap()) {
- difference_type __d = __end_cap() - __end_;
+ if (__end_ < __end_cap_) {
+ difference_type __d = __end_cap_ - __end_;
__d = (__d + 1) / 2;
__begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
__end_ += __d;
} else {
- size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
- __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
+ size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
+ __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
- std::swap(__end_cap(), __t.__end_cap());
+ std::swap(__end_cap_, __t.__end_cap_);
}
}
- __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
+ __alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
--__begin_;
}
template <class _Tp, class _Allocator>
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
- if (__end_ == __end_cap()) {
+ if (__end_ == __end_cap_) {
if (__begin_ > __first_) {
difference_type __d = __begin_ - __first_;
__d = (__d + 1) / 2;
__end_ = std::move(__begin_, __end_, __begin_ - __d);
__begin_ -= __d;
} else {
- size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
- __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
+ size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap_ - __first_), 1);
+ __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
__t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
std::swap(__first_, __t.__first_);
std::swap(__begin_, __t.__begin_);
std::swap(__end_, __t.__end_);
- std::swap(__end_cap(), __t.__end_cap());
+ std::swap(__end_cap_, __t.__end_cap_);
}
}
- __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
+ __alloc_traits::construct(__alloc_, std::__to_address(__end_), std::forward<_Args>(__args)...);
++__end_;
}
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 7d95e5deef5f3b..55853d37e270ba 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -743,7 +743,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
__end_ = __begin_; // All the objects have been destroyed by relocating them.
std::swap(this->__begin_, __v.__begin_);
std::swap(this->__end_, __v.__end_);
- std::swap(this->__end_cap(), __v.__end_cap());
+ std::swap(this->__end_cap(), __v.__end_cap_);
__v.__first_ = __v.__begin_;
__annotate_new(size());
}
@@ -773,7 +773,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
std::swap(this->__begin_, __v.__begin_);
std::swap(this->__end_, __v.__end_);
- std::swap(this->__end_cap(), __v.__end_cap());
+ std::swap(this->__end_cap(), __v.__end_cap_);
__v.__first_ = __v.__begin_;
__annotate_new(size());
return __ret;
diff --git a/libcxx/include/deque b/libcxx/include/deque
index 1a83465b39ef81..0ded69cffdeaba 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -1229,8 +1229,8 @@ private:
clear();
shrink_to_fit();
}
- __alloc() = __c.__alloc();
- __map_.__alloc() = __c.__map_.__alloc();
+ __alloc() = __c.__alloc();
+ __map_.__alloc_ = __c.__map_.__alloc_;
}
_LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque&, false_type) {}
@@ -1309,7 +1309,7 @@ deque<_Tp, _Allocator>::deque(const deque& __c)
: __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
__start_(0),
__size_(0),
- __alloc_(__map_.__alloc()) {
+ __alloc_(__map_.__alloc_) {
__annotate_new(0);
__append(__c.begin(), __c.end());
}
@@ -2061,7 +2061,7 @@ void deque<_Tp, _Allocator>::__add_front_capacity() {
// Else need to allocate 1 buffer, *and* we need to reallocate __map_.
else {
__split_buffer<pointer, __pointer_allocator&> __buf(
- std::max<size_type>(2 * __map_.capacity(), 1), 0, __map_.__alloc());
+ std::max<size_type>(2 * __map_.capacity(), 1), 0, __map_.__alloc_);
typedef __allocator_destructor<_Allocator> _Dp;
unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
@@ -2073,7 +2073,7 @@ void deque<_Tp, _Allocator>::__add_front_capacity() {
std::swap(__map_.__first_, __buf.__first_);
std::swap(__map_.__begin_, __buf.__begin_);
std::swap(__map_.__end_, __buf.__end_);
- std::swap(__map_.__end_cap(), __buf.__end_cap());
+ std::swap(__map_.__end_cap_, __buf.__end_cap_);
__start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
}
__annotate_whole_block(0, __asan_poison);
@@ -2124,7 +2124,7 @@ void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
else {
size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
__split_buffer<pointer, __pointer_allocator&> __buf(
- std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__alloc());
+ std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__alloc_);
#if _LIBCPP_HAS_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_EXCEPTIONS
@@ -2150,7 +2150,7 @@ void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
std::swap(__map_.__first_, __buf.__first_);
std::swap(__map_.__begin_, __buf.__begin_);
std::swap(__map_.__end_, __buf.__end_);
- std::swap(__map_.__end_cap(), __buf.__end_cap());
+ std::swap(__map_.__end_cap_, __buf.__end_cap_);
__start_ += __ds;
}
}
@@ -2184,7 +2184,7 @@ void deque<_Tp, _Allocator>::__add_back_capacity() {
// Else need to allocate 1 buffer, *and* we need to reallocate __map_.
else {
__split_buffer<pointer, __pointer_allocator&> __buf(
- std::max<size_type>(2 * __map_.capacity(), 1), __map_.size(), __map_.__alloc());
+ std::max<size_type>(2 * __map_.capacity(), 1), __map_.size(), __map_.__alloc_);
typedef __allocator_destructor<_Allocator> _Dp;
unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
@@ -2196,7 +2196,7 @@ void deque<_Tp, _Allocator>::__add_back_capacity() {
std::swap(__map_.__first_, __buf.__first_);
std::swap(__map_.__begin_, __buf.__begin_);
std::swap(__map_.__end_, __buf.__end_);
- std::swap(__map_.__end_cap(), __buf.__end_cap());
+ std::swap(__map_.__end_cap_, __buf.__end_cap_);
__annotate_whole_block(__map_.size() - 1, __asan_poison);
}
}
@@ -2249,7 +2249,7 @@ void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
__split_buffer<pointer, __pointer_allocator&> __buf(
std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()),
__map_.size() - __front_capacity,
- __map_.__alloc());
+ __map_.__alloc_);
#if _LIBCPP_HAS_EXCEPTIONS
try {
#endif // _LIBCPP_HAS_EXCEPTIONS
@@ -2275,7 +2275,7 @@ void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
std::swap(__map_.__first_, __buf.__first_);
std::swap(__map_.__begin_, __buf.__begin_);
std::swap(__map_.__end_, __buf.__end_);
- std::swap(__map_.__end_cap(), __buf.__end_cap());
+ std::swap(__map_.__end_cap_, __buf.__end_cap_);
__start_ -= __ds;
}
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/114224
More information about the libcxx-commits
mailing list