Skip to content

Commit 9d8f36b

Browse files
committed
[libc++] Optimize ranges::for_each for iterating over __trees
[libc++] Optimize std::for_each for __tree iterators
1 parent 10ceca8 commit 9d8f36b

File tree

10 files changed

+511
-12
lines changed

10 files changed

+511
-12
lines changed

libcxx/include/__algorithm/for_each.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___ALGORITHM_FOR_EACH_H
1212

1313
#include <__algorithm/for_each_segment.h>
14+
#include <__algorithm/specialized_algorithms.h>
1415
#include <__config>
1516
#include <__functional/identity.h>
1617
#include <__iterator/segmented_iterator.h>
@@ -27,7 +28,12 @@ template <class _InputIterator, class _Sent, class _Func, class _Proj>
2728
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
2829
__for_each(_InputIterator __first, _Sent __last, _Func& __func, _Proj& __proj) {
2930
#ifndef _LIBCPP_CXX03_LANG
30-
if constexpr (is_same<_InputIterator, _Sent>::value && __is_segmented_iterator_v<_InputIterator>) {
31+
if constexpr (using _SpecialAlg =
32+
__specialized_algorithm<_Algorithm::__for_each, __iterator_pair<_InputIterator, _Sent>>;
33+
_SpecialAlg::__has_algorithm) {
34+
_SpecialAlg()(__first, __last, __func, __proj);
35+
return __last;
36+
} else if constexpr (is_same<_InputIterator, _Sent>::value && __is_segmented_iterator_v<_InputIterator>) {
3137
using __local_iterator_t = typename __segmented_iterator_traits<_InputIterator>::__local_iterator;
3238
std::__for_each_segment(__first, __last, [&](__local_iterator_t __lfirst, __local_iterator_t __llast) {
3339
std::__for_each(__lfirst, __llast, __func, __proj);

libcxx/include/__algorithm/ranges_for_each.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <__algorithm/for_each.h>
1313
#include <__algorithm/for_each_n.h>
1414
#include <__algorithm/in_fun_result.h>
15+
#include <__algorithm/specialized_algorithms.h>
1516
#include <__concepts/assignable.h>
1617
#include <__config>
1718
#include <__functional/identity.h>
@@ -20,6 +21,7 @@
2021
#include <__ranges/access.h>
2122
#include <__ranges/concepts.h>
2223
#include <__ranges/dangling.h>
24+
#include <__type_traits/remove_cvref.h>
2325
#include <__utility/move.h>
2426

2527
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -71,7 +73,13 @@ struct __for_each {
7173
indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>> _Func>
7274
_LIBCPP_HIDE_FROM_ABI constexpr for_each_result<borrowed_iterator_t<_Range>, _Func>
7375
operator()(_Range&& __range, _Func __func, _Proj __proj = {}) const {
74-
return __for_each_impl(ranges::begin(__range), ranges::end(__range), __func, __proj);
76+
using _SpecialAlg = __specialized_algorithm<_Algorithm::__for_each, __single_range<remove_cvref_t<_Range>>>;
77+
if constexpr (_SpecialAlg::__has_algorithm) {
78+
auto [__iter, __func2] = _SpecialAlg()(__range, std::move(__func), std::move(__proj));
79+
return {std::move(__iter), std::move(__func)};
80+
} else {
81+
return __for_each_impl(ranges::begin(__range), ranges::end(__range), __func, __proj);
82+
}
7583
}
7684
};
7785

libcxx/include/__algorithm/specialized_algorithms.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
1919

2020
namespace _Algorithm {
2121
struct __fill_n {};
22+
struct __for_each {};
2223
} // namespace _Algorithm
2324

2425
template <class>
2526
struct __single_iterator;
2627

28+
template <class, class>
29+
struct __iterator_pair;
30+
31+
template <class>
32+
struct __single_range;
33+
2734
// This struct allows specializing algorithms for specific arguments. This is useful when we know a more efficient
2835
// algorithm implementation for e.g. library-defined iterators. _Alg is one of tags defined inside the _Algorithm
2936
// namespace above. _Ranges is an essentially arbitrary subset of the arguments to the algorithm that are used for

libcxx/include/__tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___TREE
1212

1313
#include <__algorithm/min.h>
14+
#include <__algorithm/specialized_algorithms.h>
1415
#include <__assert>
1516
#include <__config>
1617
#include <__fwd/pair.h>
@@ -36,6 +37,7 @@
3637
#include <__type_traits/is_swappable.h>
3738
#include <__type_traits/make_transparent.h>
3839
#include <__type_traits/remove_const.h>
40+
#include <__type_traits/remove_cvref.h>
3941
#include <__utility/forward.h>
4042
#include <__utility/lazy_synth_three_way_comparator.h>
4143
#include <__utility/move.h>
@@ -656,6 +658,54 @@ struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc> :
656658
};
657659
#endif
658660

661+
// Do an in-order traversal of the tree until `__break` returns true. Takes the root node of the tree.
662+
template <class _Reference, class _Break, class _NodePtr, class _Func, class _Proj>
663+
_LIBCPP_HIDE_FROM_ABI bool __tree_iterate_from_root(_Break __break, _NodePtr __root, _Func& __func, _Proj& __proj) {
664+
if (__root->__left_) {
665+
if (std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__left_), __func, __proj))
666+
return true;
667+
}
668+
if (__break(__root))
669+
return true;
670+
__func(static_cast<_Reference>(__root->__get_value()));
671+
if (__root->__right_)
672+
return std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__right_), __func, __proj);
673+
return false;
674+
}
675+
676+
// Do an in-order traversal of the tree from __first to __last.
677+
template <class _NodeIter, class _Func, class _Proj>
678+
_LIBCPP_HIDE_FROM_ABI void
679+
__tree_iterate_subrange(_NodeIter __first_it, _NodeIter __last_it, _Func& __func, _Proj& __proj) {
680+
using _NodePtr = _NodeIter::__node_pointer;
681+
using _Reference = _NodeIter::reference;
682+
683+
auto __first = __first_it.__ptr_;
684+
auto __last = __last_it.__ptr_;
685+
686+
while (true) {
687+
if (__first == __last)
688+
return;
689+
auto __nfirst = static_cast<_NodePtr>(__first);
690+
__func(static_cast<_Reference>(__nfirst->__get_value()));
691+
if (__nfirst->__right_) {
692+
if (std::__tree_iterate_from_root<_Reference>(
693+
[&](_NodePtr __node) -> bool { return __node == __last; },
694+
static_cast<_NodePtr>(__nfirst->__right_),
695+
__func,
696+
__proj))
697+
return;
698+
}
699+
if (std::__tree_is_left_child(__nfirst)) {
700+
__first = __nfirst->__parent_;
701+
} else {
702+
do {
703+
__first = __nfirst->__parent_;
704+
} while (!std::__tree_is_left_child(__nfirst));
705+
}
706+
}
707+
}
708+
659709
template <class _Tp, class _NodePtr, class _DiffType>
660710
class __tree_iterator {
661711
using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>;
@@ -715,8 +765,27 @@ private:
715765
friend class __tree;
716766
template <class, class, class>
717767
friend class __tree_const_iterator;
768+
769+
template <class _NodeIter, class _Func, class _Proj>
770+
friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&);
718771
};
719772

773+
#ifndef _LIBCPP_CXX03_LANG
774+
template <class _Tp, class _NodePtr, class _DiffType>
775+
struct __specialized_algorithm<
776+
_Algorithm::__for_each,
777+
__iterator_pair<__tree_iterator<_Tp, _NodePtr, _DiffType>, __tree_iterator<_Tp, _NodePtr, _DiffType>>> {
778+
static const bool __has_algorithm = true;
779+
780+
using __iterator _LIBCPP_NODEBUG = __tree_iterator<_Tp, _NodePtr, _DiffType>;
781+
782+
template <class _Func, class _Proj>
783+
_LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) {
784+
std::__tree_iterate_subrange(__first, __last, __func, __proj);
785+
}
786+
};
787+
#endif
788+
720789
template <class _Tp, class _NodePtr, class _DiffType>
721790
class __tree_const_iterator {
722791
using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>;
@@ -780,8 +849,27 @@ private:
780849

781850
template <class, class, class>
782851
friend class __tree;
852+
853+
template <class _NodeIter, class _Func, class _Proj>
854+
friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&);
783855
};
784856

857+
#ifndef _LIBCPP_CXX03_LANG
858+
template <class _Tp, class _NodePtr, class _DiffType>
859+
struct __specialized_algorithm<
860+
_Algorithm::__for_each,
861+
__iterator_pair<__tree_const_iterator<_Tp, _NodePtr, _DiffType>, __tree_const_iterator<_Tp, _NodePtr, _DiffType>>> {
862+
static const bool __has_algorithm = true;
863+
864+
using __iterator _LIBCPP_NODEBUG = __tree_const_iterator<_Tp, _NodePtr, _DiffType>;
865+
866+
template <class _Func, class _Proj>
867+
_LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) {
868+
std::__tree_iterate_subrange(__first, __last, __func, __proj);
869+
}
870+
};
871+
#endif
872+
785873
template <class _Tp, class _Compare>
786874
#ifndef _LIBCPP_CXX03_LANG
787875
_LIBCPP_DIAGNOSE_WARNING(!__is_invocable_v<_Compare const&, _Tp const&, _Tp const&>,
@@ -1484,7 +1572,25 @@ private:
14841572
[](value_type& __lhs, value_type& __rhs) { __assign_value(__lhs, std::move(__rhs)); },
14851573
[this](__node_pointer __nd) { return __move_construct_tree(__nd); });
14861574
}
1575+
1576+
friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree> >;
1577+
};
1578+
1579+
#if _LIBCPP_STD_VER >= 14
1580+
template <class _Tp, class _Compare, class _Allocator>
1581+
struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree<_Tp, _Compare, _Allocator> > > {
1582+
static const bool __has_algorithm = true;
1583+
1584+
using __node_pointer _LIBCPP_NODEBUG = typename __tree<_Tp, _Compare, _Allocator>::__node_pointer;
1585+
1586+
template <class _Tree, class _Func, class _Proj>
1587+
_LIBCPP_HIDE_FROM_ABI static auto operator()(_Tree&& __range, _Func __func, _Proj __proj) {
1588+
std::__tree_iterate_from_root<__copy_cvref_t<_Tree, typename __remove_cvref_t<_Tree>::value_type>>(
1589+
[](__node_pointer) { return false; }, __range.__root(), __func, __proj);
1590+
return std::make_pair(__range.end(), std::move(__func));
1591+
}
14871592
};
1593+
#endif
14881594

14891595
// Precondition: __size_ != 0
14901596
template <class _Tp, class _Compare, class _Allocator>

libcxx/include/map

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred); // C++20
577577
# include <__algorithm/equal.h>
578578
# include <__algorithm/lexicographical_compare.h>
579579
# include <__algorithm/lexicographical_compare_three_way.h>
580+
# include <__algorithm/specialized_algorithms.h>
580581
# include <__assert>
581582
# include <__config>
582583
# include <__functional/binary_function.h>
@@ -1370,6 +1371,8 @@ private:
13701371
# ifdef _LIBCPP_CXX03_LANG
13711372
_LIBCPP_HIDE_FROM_ABI __node_holder __construct_node_with_key(const key_type& __k);
13721373
# endif
1374+
1375+
friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<map> >;
13731376
};
13741377

13751378
# if _LIBCPP_STD_VER >= 17
@@ -1422,6 +1425,22 @@ map(initializer_list<pair<_Key, _Tp>>, _Allocator)
14221425
-> map<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
14231426
# endif
14241427

1428+
# if _LIBCPP_STD_VER >= 14
1429+
template <class _Key, class _Tp, class _Compare, class _Allocator>
1430+
struct __specialized_algorithm<_Algorithm::__for_each, __single_range<map<_Key, _Tp, _Compare, _Allocator>>> {
1431+
using __map _LIBCPP_NODEBUG = map<_Key, _Tp, _Compare, _Allocator>;
1432+
1433+
static const bool __has_algorithm = true;
1434+
1435+
template <class _Map, class _Func, class _Proj>
1436+
_LIBCPP_HIDE_FROM_ABI static auto operator()(_Map&& __map, _Func __func, _Proj __proj) {
1437+
auto [_, __func2] = __specialized_algorithm<_Algorithm::__for_each, __single_range<typename __map::__base>>()(
1438+
__map.__tree_, std::move(__func), std::move(__proj));
1439+
return std::make_pair(__map.end(), std::move(__func2));
1440+
}
1441+
};
1442+
# endif
1443+
14251444
# ifndef _LIBCPP_CXX03_LANG
14261445
template <class _Key, class _Tp, class _Compare, class _Allocator>
14271446
_Tp& map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k) {
@@ -1920,6 +1939,8 @@ private:
19201939

19211940
typedef __map_node_destructor<__node_allocator> _Dp;
19221941
typedef unique_ptr<__node, _Dp> __node_holder;
1942+
1943+
friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<multimap> >;
19231944
};
19241945

19251946
# if _LIBCPP_STD_VER >= 17
@@ -1972,6 +1993,22 @@ multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
19721993
-> multimap<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
19731994
# endif
19741995

1996+
# if _LIBCPP_STD_VER >= 14
1997+
template <class _Key, class _Tp, class _Compare, class _Allocator>
1998+
struct __specialized_algorithm<_Algorithm::__for_each, __single_range<multimap<_Key, _Tp, _Compare, _Allocator>>> {
1999+
using __map _LIBCPP_NODEBUG = multimap<_Key, _Tp, _Compare, _Allocator>;
2000+
2001+
static const bool __has_algorithm = true;
2002+
2003+
template <class _Map, class _Func, class _Proj>
2004+
_LIBCPP_HIDE_FROM_ABI static auto operator()(_Map&& __map, _Func __func, _Proj __proj) {
2005+
auto [_, __func2] = __specialized_algorithm<_Algorithm::__for_each, __single_range<typename __map::__base>>()(
2006+
__map.__tree_, std::move(__func), std::move(__proj));
2007+
return std::make_pair(__map.end(), std::move(__func2));
2008+
}
2009+
};
2010+
# endif
2011+
19752012
template <class _Key, class _Tp, class _Compare, class _Allocator>
19762013
inline _LIBCPP_HIDE_FROM_ABI bool
19772014
operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {

libcxx/include/set

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred); // C++20
518518
# include <__algorithm/equal.h>
519519
# include <__algorithm/lexicographical_compare.h>
520520
# include <__algorithm/lexicographical_compare_three_way.h>
521+
# include <__algorithm/specialized_algorithms.h>
521522
# include <__assert>
522523
# include <__config>
523524
# include <__functional/is_transparent.h>
@@ -898,6 +899,9 @@ public:
898899
return __tree_.__equal_range_multi(__k);
899900
}
900901
# endif
902+
903+
template <class, class...>
904+
friend struct __specialized_algorithm;
901905
};
902906

903907
# if _LIBCPP_STD_VER >= 17
@@ -944,6 +948,23 @@ template <class _Key, class _Allocator, class = enable_if_t<__is_allocator_v<_Al
944948
set(initializer_list<_Key>, _Allocator) -> set<_Key, less<_Key>, _Allocator>;
945949
# endif
946950

951+
# if _LIBCPP_STD_VER >= 14
952+
template <class _Alg, class _Key, class _Compare, class _Allocator>
953+
struct __specialized_algorithm<_Alg, __single_range<set<_Key, _Compare, _Allocator>>> {
954+
using __set _LIBCPP_NODEBUG = set<_Key, _Compare, _Allocator>;
955+
956+
static const bool __has_algorithm =
957+
__specialized_algorithm<_Alg, __single_range<typename __set::__base>>::__has_algorithm;
958+
959+
// set's begin() and end() are identical with and without const qualification
960+
template <class... _Args>
961+
_LIBCPP_HIDE_FROM_ABI static auto operator()(const __set& __set, _Args&&... __args) {
962+
return __specialized_algorithm<_Alg, __single_range<typename __set::__base>>()(
963+
__set.__tree_, std::forward<_Args>(__args)...);
964+
}
965+
};
966+
# endif
967+
947968
template <class _Key, class _Compare, class _Allocator>
948969
inline _LIBCPP_HIDE_FROM_ABI bool
949970
operator==(const set<_Key, _Compare, _Allocator>& __x, const set<_Key, _Compare, _Allocator>& __y) {
@@ -1342,6 +1363,9 @@ public:
13421363
return __tree_.__equal_range_multi(__k);
13431364
}
13441365
# endif
1366+
1367+
template <class, class...>
1368+
friend struct __specialized_algorithm;
13451369
};
13461370

13471371
# if _LIBCPP_STD_VER >= 17
@@ -1389,6 +1413,23 @@ template <class _Key, class _Allocator, class = enable_if_t<__is_allocator_v<_Al
13891413
multiset(initializer_list<_Key>, _Allocator) -> multiset<_Key, less<_Key>, _Allocator>;
13901414
# endif
13911415

1416+
# if _LIBCPP_STD_VER >= 14
1417+
template <class _Alg, class _Key, class _Compare, class _Allocator>
1418+
struct __specialized_algorithm<_Alg, __single_range<multiset<_Key, _Compare, _Allocator>>> {
1419+
using __set _LIBCPP_NODEBUG = multiset<_Key, _Compare, _Allocator>;
1420+
1421+
static const bool __has_algorithm =
1422+
__specialized_algorithm<_Alg, __single_range<typename __set::__base>>::__has_algorithm;
1423+
1424+
// set's begin() and end() are identical with and without const qualification
1425+
template <class... _Args>
1426+
_LIBCPP_HIDE_FROM_ABI static auto operator()(const __set& __set, _Args&&... __args) {
1427+
return __specialized_algorithm<_Alg, __single_range<typename __set::__base>>()(
1428+
__set.__tree_, std::forward<_Args>(__args)...);
1429+
}
1430+
};
1431+
# endif
1432+
13921433
template <class _Key, class _Compare, class _Allocator>
13931434
inline _LIBCPP_HIDE_FROM_ABI bool
13941435
operator==(const multiset<_Key, _Compare, _Allocator>& __x, const multiset<_Key, _Compare, _Allocator>& __y) {

0 commit comments

Comments
 (0)