Skip to content

Commit 03855dd

Browse files
committed
[libc++] Optimize __tree::__erase_unique
1 parent 3b10b9a commit 03855dd

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

libcxx/include/__tree

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,14 @@ public:
766766
typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
767767
typedef allocator_traits<__node_allocator> __node_traits;
768768

769+
enum class __uniqueness {
770+
__unique,
771+
__multi,
772+
};
773+
774+
using __unique_tag = integral_constant<__uniqueness, __uniqueness::__unique>;
775+
using __multi_tag = integral_constant<__uniqueness, __uniqueness::__multi>;
776+
769777
// TODO(LLVM 22): Remove this check
770778
#ifndef _LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB
771779
static_assert(sizeof(__node_base_pointer) == sizeof(__end_node_pointer) && _LIBCPP_ALIGNOF(__node_base_pointer) ==
@@ -1009,9 +1017,27 @@ public:
10091017
__insert_node_at(__end_node_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT;
10101018

10111019
template <class _Key>
1012-
_LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __v);
1020+
_LIBCPP_HIDE_FROM_ABI iterator find(__multi_tag, const _Key& __v);
1021+
template <class _Key>
1022+
_LIBCPP_HIDE_FROM_ABI const_iterator find(__multi_tag, const _Key& __v) const;
1023+
1024+
template <class _Key>
1025+
_LIBCPP_HIDE_FROM_ABI iterator find(__unique_tag, const _Key& __key) {
1026+
__end_node_pointer __parent;
1027+
__node_base_pointer __match = __find_equal(__parent, __key);
1028+
if (__match == nullptr)
1029+
return end();
1030+
return iterator(static_cast<__node_pointer>(__match));
1031+
}
1032+
10131033
template <class _Key>
1014-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __v) const;
1034+
_LIBCPP_HIDE_FROM_ABI const_iterator find(__unique_tag, const _Key& __key) const {
1035+
__end_node_pointer __parent;
1036+
__node_base_pointer __match = __find_equal(__parent, __key);
1037+
if (__match == nullptr)
1038+
return end();
1039+
return const_iterator(static_cast<__node_pointer>(__match));
1040+
}
10151041

10161042
template <class _Key>
10171043
_LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const;
@@ -1914,7 +1940,7 @@ __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(const_iterator __
19141940
template <class _Tp, class _Compare, class _Allocator>
19151941
template <class _NodeHandle>
19161942
_LIBCPP_HIDE_FROM_ABI _NodeHandle __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key) {
1917-
iterator __it = find(__key);
1943+
iterator __it = find(__multi_tag(), __key);
19181944
if (__it == end())
19191945
return _NodeHandle();
19201946
return __node_handle_extract<_NodeHandle>(__it);
@@ -2013,7 +2039,7 @@ template <class _Tp, class _Compare, class _Allocator>
20132039
template <class _Key>
20142040
typename __tree<_Tp, _Compare, _Allocator>::size_type
20152041
__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) {
2016-
iterator __i = find(__k);
2042+
iterator __i = find(__unique_tag(), __k);
20172043
if (__i == end())
20182044
return 0;
20192045
erase(__i);
@@ -2033,7 +2059,8 @@ __tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) {
20332059

20342060
template <class _Tp, class _Compare, class _Allocator>
20352061
template <class _Key>
2036-
typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) {
2062+
typename __tree<_Tp, _Compare, _Allocator>::iterator
2063+
__tree<_Tp, _Compare, _Allocator>::find(__multi_tag, const _Key& __v) {
20372064
iterator __p = __lower_bound(__v, __root(), __end_node());
20382065
if (__p != end() && !value_comp()(__v, *__p))
20392066
return __p;
@@ -2043,7 +2070,7 @@ typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allo
20432070
template <class _Tp, class _Compare, class _Allocator>
20442071
template <class _Key>
20452072
typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2046-
__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const {
2073+
__tree<_Tp, _Compare, _Allocator>::find(__multi_tag, const _Key& __v) const {
20472074
const_iterator __p = __lower_bound(__v, __root(), __end_node());
20482075
if (__p != end() && !value_comp()(__v, *__p))
20492076
return __p;

libcxx/include/map

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,8 @@ private:
903903

904904
__base __tree_;
905905

906+
using __unique_tag = __base::__unique_tag;
907+
906908
public:
907909
typedef typename __alloc_traits::pointer pointer;
908910
typedef typename __alloc_traits::const_pointer const_pointer;
@@ -1243,8 +1245,8 @@ public:
12431245

12441246
_LIBCPP_HIDE_FROM_ABI void swap(map& __m) _NOEXCEPT_(__is_nothrow_swappable_v<__base>) { __tree_.swap(__m.__tree_); }
12451247

1246-
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
1247-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
1248+
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__unique_tag(), __k); }
1249+
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__unique_tag(), __k); }
12481250
# if _LIBCPP_STD_VER >= 14
12491251
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
12501252
_LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {

libcxx/include/set

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ private:
595595

596596
__base __tree_;
597597

598+
using __unique_tag = __base::__unique_tag;
599+
598600
public:
599601
typedef typename __base::pointer pointer;
600602
typedef typename __base::const_pointer const_pointer;
@@ -826,8 +828,8 @@ public:
826828
_LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return __tree_.value_comp(); }
827829

828830
// set operations:
829-
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
830-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
831+
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__unique_tag(), __k); }
832+
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__unique_tag(), __k); }
831833
# if _LIBCPP_STD_VER >= 14
832834
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
833835
_LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {

0 commit comments

Comments
 (0)