Skip to content

Commit 0714125

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

File tree

10 files changed

+555
-5
lines changed

10 files changed

+555
-5
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: 104 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,50 @@ 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+
const 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+
while (!std::__tree_is_left_child(static_cast<_NodePtr>(__first)))
700+
__first = static_cast<_NodePtr>(__first)->__parent_;
701+
__first = static_cast<_NodePtr>(__first)->__parent_;
702+
}
703+
}
704+
659705
template <class _Tp, class _NodePtr, class _DiffType>
660706
class __tree_iterator {
661707
using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>;
@@ -715,8 +761,28 @@ private:
715761
friend class __tree;
716762
template <class, class, class>
717763
friend class __tree_const_iterator;
764+
765+
template <class _NodeIter, class _Func, class _Proj>
766+
friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&);
718767
};
719768

769+
#ifndef _LIBCPP_CXX03_LANG
770+
// This also handles {multi,}set::iterator, since they're just aliases to __tree::iterator
771+
template <class _Tp, class _NodePtr, class _DiffType>
772+
struct __specialized_algorithm<
773+
_Algorithm::__for_each,
774+
__iterator_pair<__tree_iterator<_Tp, _NodePtr, _DiffType>, __tree_iterator<_Tp, _NodePtr, _DiffType>>> {
775+
static const bool __has_algorithm = true;
776+
777+
using __iterator _LIBCPP_NODEBUG = __tree_iterator<_Tp, _NodePtr, _DiffType>;
778+
779+
template <class _Func, class _Proj>
780+
_LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) {
781+
std::__tree_iterate_subrange(__first, __last, __func, __proj);
782+
}
783+
};
784+
#endif
785+
720786
template <class _Tp, class _NodePtr, class _DiffType>
721787
class __tree_const_iterator {
722788
using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>;
@@ -780,7 +846,27 @@ private:
780846

781847
template <class, class, class>
782848
friend class __tree;
849+
850+
template <class _NodeIter, class _Func, class _Proj>
851+
friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&);
852+
};
853+
854+
#ifndef _LIBCPP_CXX03_LANG
855+
// This also handles {multi,}set::const_iterator, since they're just aliases to __tree::iterator
856+
template <class _Tp, class _NodePtr, class _DiffType>
857+
struct __specialized_algorithm<
858+
_Algorithm::__for_each,
859+
__iterator_pair<__tree_const_iterator<_Tp, _NodePtr, _DiffType>, __tree_const_iterator<_Tp, _NodePtr, _DiffType>>> {
860+
static const bool __has_algorithm = true;
861+
862+
using __iterator _LIBCPP_NODEBUG = __tree_const_iterator<_Tp, _NodePtr, _DiffType>;
863+
864+
template <class _Func, class _Proj>
865+
_LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) {
866+
std::__tree_iterate_subrange(__first, __last, __func, __proj);
867+
}
783868
};
869+
#endif
784870

785871
template <class _Tp, class _Compare>
786872
#ifndef _LIBCPP_CXX03_LANG
@@ -1484,7 +1570,25 @@ private:
14841570
[](value_type& __lhs, value_type& __rhs) { __assign_value(__lhs, std::move(__rhs)); },
14851571
[this](__node_pointer __nd) { return __move_construct_tree(__nd); });
14861572
}
1573+
1574+
friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree> >;
1575+
};
1576+
1577+
#if _LIBCPP_STD_VER >= 14
1578+
template <class _Tp, class _Compare, class _Allocator>
1579+
struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree<_Tp, _Compare, _Allocator> > > {
1580+
static const bool __has_algorithm = true;
1581+
1582+
using __node_pointer _LIBCPP_NODEBUG = typename __tree<_Tp, _Compare, _Allocator>::__node_pointer;
1583+
1584+
template <class _Tree, class _Func, class _Proj>
1585+
_LIBCPP_HIDE_FROM_ABI static auto operator()(_Tree&& __range, _Func __func, _Proj __proj) {
1586+
std::__tree_iterate_from_root<__copy_cvref_t<_Tree, typename __remove_cvref_t<_Tree>::value_type>>(
1587+
[](__node_pointer) { return false; }, __range.__root(), __func, __proj);
1588+
return std::make_pair(__range.end(), std::move(__func));
1589+
}
14871590
};
1591+
#endif
14881592

14891593
// Precondition: __size_ != 0
14901594
template <class _Tp, class _Compare, class _Allocator>

libcxx/include/map

Lines changed: 77 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>
@@ -818,7 +819,26 @@ public:
818819
friend class multimap;
819820
template <class>
820821
friend class __map_const_iterator;
822+
823+
template <class, class...>
824+
friend struct __specialized_algorithm;
825+
};
826+
827+
# ifndef _LIBCPP_CXX03_LANG
828+
template <class _Alg, class _TreeIterator>
829+
struct __specialized_algorithm<_Alg, __iterator_pair<__map_iterator<_TreeIterator>, __map_iterator<_TreeIterator>>> {
830+
using __base _LIBCPP_NODEBUG = __specialized_algorithm<_Alg, __iterator_pair<_TreeIterator, _TreeIterator>>;
831+
832+
static const bool __has_algorithm = __base::__has_algorithm;
833+
834+
using __iterator _LIBCPP_NODEBUG = __map_iterator<_TreeIterator>;
835+
836+
template <class... _Args>
837+
_LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Args&&... __args) {
838+
__base()(__first.__i_, __last.__i_, std::forward<_Args>(__args)...);
839+
}
821840
};
841+
# endif
822842

823843
template <class _TreeIterator>
824844
class __map_const_iterator {
@@ -873,7 +893,28 @@ public:
873893
friend class multimap;
874894
template <class, class, class>
875895
friend class __tree_const_iterator;
896+
897+
template <class, class...>
898+
friend struct __specialized_algorithm;
899+
};
900+
901+
# ifndef _LIBCPP_CXX03_LANG
902+
template <class _Alg, class _TreeIterator>
903+
struct __specialized_algorithm<
904+
_Alg,
905+
__iterator_pair<__map_const_iterator<_TreeIterator>, __map_const_iterator<_TreeIterator>>> {
906+
using __base _LIBCPP_NODEBUG = __specialized_algorithm<_Alg, __iterator_pair<_TreeIterator, _TreeIterator>>;
907+
908+
static const bool __has_algorithm = __base::__has_algorithm;
909+
910+
using __iterator _LIBCPP_NODEBUG = __map_const_iterator<_TreeIterator>;
911+
912+
template <class... _Args>
913+
_LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Args&&... __args) {
914+
__base()(__first.__i_, __last.__i_, std::forward<_Args>(__args)...);
915+
}
876916
};
917+
# endif
877918

878919
template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
879920
class multimap;
@@ -1370,6 +1411,8 @@ private:
13701411
# ifdef _LIBCPP_CXX03_LANG
13711412
_LIBCPP_HIDE_FROM_ABI __node_holder __construct_node_with_key(const key_type& __k);
13721413
# endif
1414+
1415+
friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<map> >;
13731416
};
13741417

13751418
# if _LIBCPP_STD_VER >= 17
@@ -1422,6 +1465,22 @@ map(initializer_list<pair<_Key, _Tp>>, _Allocator)
14221465
-> map<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
14231466
# endif
14241467

1468+
# if _LIBCPP_STD_VER >= 14
1469+
template <class _Key, class _Tp, class _Compare, class _Allocator>
1470+
struct __specialized_algorithm<_Algorithm::__for_each, __single_range<map<_Key, _Tp, _Compare, _Allocator>>> {
1471+
using __map _LIBCPP_NODEBUG = map<_Key, _Tp, _Compare, _Allocator>;
1472+
1473+
static const bool __has_algorithm = true;
1474+
1475+
template <class _Map, class _Func, class _Proj>
1476+
_LIBCPP_HIDE_FROM_ABI static auto operator()(_Map&& __map, _Func __func, _Proj __proj) {
1477+
auto [_, __func2] = __specialized_algorithm<_Algorithm::__for_each, __single_range<typename __map::__base>>()(
1478+
__map.__tree_, std::move(__func), std::move(__proj));
1479+
return std::make_pair(__map.end(), std::move(__func2));
1480+
}
1481+
};
1482+
# endif
1483+
14251484
# ifndef _LIBCPP_CXX03_LANG
14261485
template <class _Key, class _Tp, class _Compare, class _Allocator>
14271486
_Tp& map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k) {
@@ -1920,6 +1979,8 @@ private:
19201979

19211980
typedef __map_node_destructor<__node_allocator> _Dp;
19221981
typedef unique_ptr<__node, _Dp> __node_holder;
1982+
1983+
friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<multimap> >;
19231984
};
19241985

19251986
# if _LIBCPP_STD_VER >= 17
@@ -1972,6 +2033,22 @@ multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
19722033
-> multimap<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
19732034
# endif
19742035

2036+
# if _LIBCPP_STD_VER >= 14
2037+
template <class _Key, class _Tp, class _Compare, class _Allocator>
2038+
struct __specialized_algorithm<_Algorithm::__for_each, __single_range<multimap<_Key, _Tp, _Compare, _Allocator>>> {
2039+
using __map _LIBCPP_NODEBUG = multimap<_Key, _Tp, _Compare, _Allocator>;
2040+
2041+
static const bool __has_algorithm = true;
2042+
2043+
template <class _Map, class _Func, class _Proj>
2044+
_LIBCPP_HIDE_FROM_ABI static auto operator()(_Map&& __map, _Func __func, _Proj __proj) {
2045+
auto [_, __func2] = __specialized_algorithm<_Algorithm::__for_each, __single_range<typename __map::__base>>()(
2046+
__map.__tree_, std::move(__func), std::move(__proj));
2047+
return std::make_pair(__map.end(), std::move(__func2));
2048+
}
2049+
};
2050+
# endif
2051+
19752052
template <class _Key, class _Tp, class _Compare, class _Allocator>
19762053
inline _LIBCPP_HIDE_FROM_ABI bool
19772054
operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, const multimap<_Key, _Tp, _Compare, _Allocator>& __y) {

0 commit comments

Comments
 (0)