[libcxx-commits] [libcxx] [libc++] Optimize {set, map}::{lower, upper}_bound (PR #161366)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Oct 6 08:20:24 PDT 2025
================
@@ -1165,32 +1165,81 @@ public:
template <class _Key>
_LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const;
+ template <bool _LowerBound, class _Key>
+ _LIBCPP_HIDE_FROM_ABI __end_node_pointer __lower_upper_bound_unique_impl(const _Key& __v) const {
+ auto __rt = __root();
+ auto __result = __end_node();
+ auto __comp = __lazy_synth_three_way_comparator<_Compare, _Key, value_type>(value_comp());
+ while (__rt != nullptr) {
+ auto __comp_res = __comp(__v, __rt->__get_value());
+
+ if (__comp_res.__less()) {
+ __result = static_cast<__end_node_pointer>(__rt);
+ __rt = static_cast<__node_pointer>(__rt->__left_);
+ } else if (__comp_res.__greater()) {
+ __rt = static_cast<__node_pointer>(__rt->__right_);
+ } else if _LIBCPP_CONSTEXPR (_LowerBound) {
+ return static_cast<__end_node_pointer>(__rt);
+ } else {
+ return __rt->__right_ ? static_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) : __result;
+ }
+ }
+ return __result;
+ }
+
template <class _Key>
- _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Key& __v) {
- return __lower_bound(__v, __root(), __end_node());
+ _LIBCPP_HIDE_FROM_ABI iterator __lower_bound_unique(const _Key& __v) {
+ return iterator(__lower_upper_bound_unique_impl<true>(__v));
}
+
template <class _Key>
- _LIBCPP_HIDE_FROM_ABI iterator __lower_bound(const _Key& __v, __node_pointer __root, __end_node_pointer __result);
+ _LIBCPP_HIDE_FROM_ABI const_iterator __lower_bound_unique(const _Key& __v) const {
+ return const_iterator(__lower_upper_bound_unique_impl<true>(__v));
+ }
+
template <class _Key>
- _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Key& __v) const {
- return __lower_bound(__v, __root(), __end_node());
+ _LIBCPP_HIDE_FROM_ABI iterator __upper_bound_unique(const _Key& __v) {
+ return iterator(__lower_upper_bound_unique_impl<false>(__v));
+ }
+
+ template <class _Key>
+ _LIBCPP_HIDE_FROM_ABI const_iterator __upper_bound_unique(const _Key& __v) const {
+ return iterator(__lower_upper_bound_unique_impl<false>(__v));
}
+
+ template <class _Key>
+ _LIBCPP_HIDE_FROM_ABI iterator __lower_bound(const _Key& __v, __node_pointer __root, __end_node_pointer __result);
+
template <class _Key>
_LIBCPP_HIDE_FROM_ABI const_iterator
__lower_bound(const _Key& __v, __node_pointer __root, __end_node_pointer __result) const;
+
template <class _Key>
- _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Key& __v) {
- return __upper_bound(__v, __root(), __end_node());
+ _LIBCPP_HIDE_FROM_ABI iterator __lower_bound_multi(const _Key& __v) {
+ return __lower_bound(__v, __root(), __end_node());
}
template <class _Key>
- _LIBCPP_HIDE_FROM_ABI iterator __upper_bound(const _Key& __v, __node_pointer __root, __end_node_pointer __result);
+ _LIBCPP_HIDE_FROM_ABI const_iterator __lower_bound_multi(const _Key& __v) const {
+ return __lower_bound(__v, __root(), __end_node());
+ }
+
template <class _Key>
- _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Key& __v) const {
+ _LIBCPP_HIDE_FROM_ABI iterator __upper_bound_multi(const _Key& __v) {
return __upper_bound(__v, __root(), __end_node());
}
+
+ template <class _Key>
+ _LIBCPP_HIDE_FROM_ABI const_iterator __upper_bound_multi(const _Key& __v) const {
+ return __upper_bound(__v, __root(), __end_node());
+ }
+
+ template <class _Key>
+ _LIBCPP_HIDE_FROM_ABI iterator __upper_bound(const _Key& __v, __node_pointer __root, __end_node_pointer __result);
----------------
ldionne wrote:
I would rename this to `__upper_bound_multi` yet keep the `__root` and `__result` parameters. IMO that makes it clearer.
Also, can this be made `private`? Same comments for `__lower_bound`.
https://github.com/llvm/llvm-project/pull/161366
More information about the libcxx-commits
mailing list