Skip to content

Commit e774294

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

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
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: 12 additions & 8 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,16 +1245,16 @@ 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) {
1251-
return __tree_.find(__k);
1253+
return __tree_.find(__unique_tag(), __k);
12521254
}
12531255
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
12541256
_LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
1255-
return __tree_.find(__k);
1257+
return __tree_.find(__unique_tag(), __k);
12561258
}
12571259
# endif
12581260

@@ -1571,6 +1573,8 @@ private:
15711573

15721574
__base __tree_;
15731575

1576+
using __multi_tag = __base::__multi_tag;
1577+
15741578
public:
15751579
typedef typename __alloc_traits::pointer pointer;
15761580
typedef typename __alloc_traits::const_pointer const_pointer;
@@ -1819,16 +1823,16 @@ public:
18191823
__tree_.swap(__m.__tree_);
18201824
}
18211825

1822-
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
1823-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
1826+
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__multi_tag(), __k); }
1827+
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__multi_tag(), __k); }
18241828
# if _LIBCPP_STD_VER >= 14
18251829
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
18261830
_LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {
1827-
return __tree_.find(__k);
1831+
return __tree_.find(__multi_tag(), __k);
18281832
}
18291833
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
18301834
_LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
1831-
return __tree_.find(__k);
1835+
return __tree_.find(__multi_tag(), __k);
18321836
}
18331837
# endif
18341838

libcxx/include/set

Lines changed: 12 additions & 8 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,16 +828,16 @@ 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) {
834-
return __tree_.find(__k);
836+
return __tree_.find(__unique_tag(), __k);
835837
}
836838
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
837839
_LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
838-
return __tree_.find(__k);
840+
return __tree_.find(__unique_tag(), __k);
839841
}
840842
# endif
841843

@@ -1055,6 +1057,8 @@ private:
10551057

10561058
__base __tree_;
10571059

1060+
using __multi_tag = __base::__multi_tag;
1061+
10581062
public:
10591063
typedef typename __base::pointer pointer;
10601064
typedef typename __base::const_pointer const_pointer;
@@ -1287,16 +1291,16 @@ public:
12871291
_LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return __tree_.value_comp(); }
12881292

12891293
// set operations:
1290-
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
1291-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
1294+
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__multi_tag(), __k); }
1295+
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__multi_tag(), __k); }
12921296
# if _LIBCPP_STD_VER >= 14
12931297
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
12941298
_LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {
1295-
return __tree_.find(__k);
1299+
return __tree_.find(__multi_tag(), __k);
12961300
}
12971301
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
12981302
_LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
1299-
return __tree_.find(__k);
1303+
return __tree_.find(__multi_tag(), __k);
13001304
}
13011305
# endif
13021306

0 commit comments

Comments
 (0)