Skip to content

[libc++] Optimize {std,ranges}::distance for segmented iterators #133612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

winner245
Copy link
Contributor

@winner245 winner245 commented Mar 30, 2025

This patch enhances the performance of std::distance and std::ranges::distance for non-random-access segmented iterators, e.g., std::join_view iterators. The original implementation operates in linear time, O(n), where n is the total number of elements. The optimized version reduces this to approximately O(n / segment_size) by leveraging segmented structure, where segment_size is the average size of each segment.

The table below summarizes the peak performance improvements observed across different segment sizes, with the total element count n ranging up to 1 << 20 (1,048,576 elements), based on benchmark results.

----------------------------------------------------------------------------------------
Container/n/segment_size                          std::distance     std::ranges::distance
----------------------------------------------------------------------------------------
join_view(vector<vector<int>>)/1048576/256	         401.6x	              422.9x
join_view(deque<deque<int>>)/1048576/256 	         112.1x	              132.6x
join_view(vector<vector<int>>)/1048576/1024         1669.2x              1559.1x
join_view(deque<deque<int>>)/1048576/1024            487.7x               497.4x

Benchmarks

Segment size = 1024

-----------------------------------------------------------------------------------------
Benchmark                                                    Before      After    Speedup
-----------------------------------------------------------------------------------------
std::distance(join_view(vector<vector<int>>))/50            38.8 ns     1.01 ns     38.4x
std::distance(join_view(vector<vector<int>>))/1024           660 ns     1.02 ns    647.1x
std::distance(join_view(vector<vector<int>>))/4096          2934 ns     1.98 ns   1481.8x
std::distance(join_view(vector<vector<int>>))/8192          5751 ns     3.92 ns   1466.8x
std::distance(join_view(vector<vector<int>>))/16384        11520 ns     7.06 ns   1631.7x
std::distance(join_view(vector<vector<int>>))/65536        46367 ns     32.2 ns   1440.6x
std::distance(join_view(vector<vector<int>>))/262144      182611 ns      114 ns   1601.9x
std::distance(join_view(vector<vector<int>>))/1048576     737785 ns      442 ns   1669.2x
std::distance(join_view(deque<deque<int>>))/50              53.1 ns     6.13 ns      8.7x
std::distance(join_view(deque<deque<int>>))/1024             854 ns     7.53 ns    113.4x
std::distance(join_view(deque<deque<int>>))/4096            3507 ns     14.7 ns    238.6x
std::distance(join_view(deque<deque<int>>))/8192            7114 ns     17.6 ns    404.2x
std::distance(join_view(deque<deque<int>>))/16384          13997 ns     30.7 ns    455.9x
std::distance(join_view(deque<deque<int>>))/65536          55598 ns      114 ns    487.7x
std::distance(join_view(deque<deque<int>>))/262144        214293 ns      480 ns    446.4x
std::distance(join_view(deque<deque<int>>))/1048576       833000 ns     2183 ns    381.6x
rng::distance(join_view(vector<vector<int>>))/50            39.1 ns     1.10 ns     35.5x
rng::distance(join_view(vector<vector<int>>))/1024           689 ns     1.14 ns    604.4x
rng::distance(join_view(vector<vector<int>>))/4096          2753 ns     2.15 ns   1280.5x
rng::distance(join_view(vector<vector<int>>))/8192          5530 ns     4.61 ns   1199.6x
rng::distance(join_view(vector<vector<int>>))/16384        10968 ns     7.97 ns   1376.2x
rng::distance(join_view(vector<vector<int>>))/65536        46009 ns     35.3 ns   1303.4x
rng::distance(join_view(vector<vector<int>>))/262144      190569 ns      124 ns   1536.9x
rng::distance(join_view(vector<vector<int>>))/1048576     746724 ns      479 ns   1559.1x
rng::distance(join_view(deque<deque<int>>))/50              51.6 ns     6.57 ns      7.9x
rng::distance(join_view(deque<deque<int>>))/1024             826 ns     6.50 ns    127.1x
rng::distance(join_view(deque<deque<int>>))/4096            3323 ns     12.5 ns    265.8x
rng::distance(join_view(deque<deque<int>>))/8192            6619 ns     19.1 ns    346.5x
rng::distance(join_view(deque<deque<int>>))/16384          13495 ns     33.2 ns    406.5x
rng::distance(join_view(deque<deque<int>>))/65536          53668 ns      114 ns    470.8x
rng::distance(join_view(deque<deque<int>>))/262144        236277 ns      475 ns    497.4x
rng::distance(join_view(deque<deque<int>>))/1048576       914177 ns     2157 ns    423.8x
-----------------------------------------------------------------------------------------

Segment size = 256

-----------------------------------------------------------------------------------------
Benchmark                                                    Before      After    Speedup
-----------------------------------------------------------------------------------------
std::distance(join_view(vector<vector<int>>))/50            38.1 ns     1.02 ns     37.4x
std::distance(join_view(vector<vector<int>>))/1024           689 ns     2.06 ns    334.5x
std::distance(join_view(vector<vector<int>>))/4096          2815 ns     7.01 ns    401.6x
std::distance(join_view(vector<vector<int>>))/8192          5507 ns     14.3 ns    385.1x
std::distance(join_view(vector<vector<int>>))/16384        11050 ns     33.7 ns    327.9x
std::distance(join_view(vector<vector<int>>))/65536        44197 ns      118 ns    374.6x
std::distance(join_view(vector<vector<int>>))/262144      175793 ns      449 ns    391.5x
std::distance(join_view(vector<vector<int>>))/1048576     703242 ns     2140 ns    328.7x
std::distance(join_view(deque<deque<int>>))/50              50.2 ns     6.12 ns      8.2x
std::distance(join_view(deque<deque<int>>))/1024             835 ns     11.4 ns     73.2x
std::distance(join_view(deque<deque<int>>))/4096            3353 ns     32.9 ns    101.9x
std::distance(join_view(deque<deque<int>>))/8192            6711 ns     64.2 ns    104.5x
std::distance(join_view(deque<deque<int>>))/16384          13231 ns      118 ns    112.1x
std::distance(join_view(deque<deque<int>>))/65536          53523 ns      556 ns     96.3x
std::distance(join_view(deque<deque<int>>))/262144        219101 ns     2166 ns    101.2x
std::distance(join_view(deque<deque<int>>))/1048576       880277 ns    15852 ns     55.5x
rng::distance(join_view(vector<vector<int>>))/50            37.7 ns     1.13 ns     33.4x
rng::distance(join_view(vector<vector<int>>))/1024           697 ns     2.14 ns    325.7x
rng::distance(join_view(vector<vector<int>>))/4096          2804 ns     7.52 ns    373.0x
rng::distance(join_view(vector<vector<int>>))/8192          5749 ns     15.2 ns    378.2x
rng::distance(join_view(vector<vector<int>>))/16384        11742 ns     34.8 ns    337.4x
rng::distance(join_view(vector<vector<int>>))/65536        47274 ns      116 ns    407.7x
rng::distance(join_view(vector<vector<int>>))/262144      187774 ns      444 ns    422.9x
rng::distance(join_view(vector<vector<int>>))/1048576     749724 ns     2109 ns    355.5x
rng::distance(join_view(deque<deque<int>>))/50              53.0 ns     6.09 ns      8.7x
rng::distance(join_view(deque<deque<int>>))/1024             895 ns     11.0 ns     81.4x
rng::distance(join_view(deque<deque<int>>))/4096            3825 ns     30.6 ns    125.0x
rng::distance(join_view(deque<deque<int>>))/8192            7550 ns     60.5 ns    124.8x
rng::distance(join_view(deque<deque<int>>))/16384          14847 ns      112 ns    132.6x
rng::distance(join_view(deque<deque<int>>))/65536          56888 ns      453 ns    125.6x
rng::distance(join_view(deque<deque<int>>))/262144        231395 ns     2034 ns    113.8x
rng::distance(join_view(deque<deque<int>>))/1048576       933093 ns    15012 ns     62.2x
-----------------------------------------------------------------------------------------

Addresses a subtask of #102817.

@winner245 winner245 force-pushed the segmented-distance branch 9 times, most recently from 25effb0 to 5795a5a Compare March 31, 2025 00:03
@winner245 winner245 marked this pull request as ready for review March 31, 2025 14:27
@winner245 winner245 requested a review from a team as a code owner March 31, 2025 14:27
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 31, 2025

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

Changes

This patch enhances the performance of std::distance and std::ranges::distance for non-random-access segmented iterators. The original implementation operates in linear time, O(n), where n is the total number of elements. The optimized version reduces this to approximately O(n / segment_size) by leveraging segmented structure, where segment_size is the average size of each segment.

The table below summarizes the peak performance improvements observed across different segment sizes, with the total element count n ranging up to 1 &lt;&lt; 20 (1,048,576 elements), based on benchmark results.

----------------------------------------------------------------------------
Contaier/n/segment_size              std::distance     std::ranges::distance
----------------------------------------------------------------------------
vector&lt;vector&lt;int&gt;&gt;/1048576/256	         401.6x	              422.9x
deque&lt;deque&lt;int&gt;&gt;/1048576/256 	         112.1x	              132.6x
vector&lt;vector&lt;int&gt;&gt;/1048576/1024        1669.2x              1559.1x
deque&lt;deque&lt;int&gt;&gt;/1048576/1024           487.7x               497.4x

Benchmarks

Segment size = 1024

-----------------------------------------------------------------------------------------
Benchmark                                                    Before      After    Speedup
-----------------------------------------------------------------------------------------
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/50            38.8 ns     1.01 ns     38.4x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1024           660 ns     1.02 ns    647.1x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/4096          2934 ns     1.98 ns   1481.8x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/8192          5751 ns     3.92 ns   1466.8x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/16384        11520 ns     7.06 ns   1631.7x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/65536        46367 ns     32.2 ns   1440.6x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/262144      182611 ns      114 ns   1601.9x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1048576     737785 ns      442 ns   1669.2x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/50              53.1 ns     6.13 ns      8.7x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1024             854 ns     7.53 ns    113.4x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/4096            3507 ns     14.7 ns    238.6x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/8192            7114 ns     17.6 ns    404.2x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/16384          13997 ns     30.7 ns    455.9x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/65536          55598 ns      114 ns    487.7x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/262144        214293 ns      480 ns    446.4x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1048576       833000 ns     2183 ns    381.6x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/50            39.1 ns     1.10 ns     35.5x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1024           689 ns     1.14 ns    604.4x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/4096          2753 ns     2.15 ns   1280.5x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/8192          5530 ns     4.61 ns   1199.6x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/16384        10968 ns     7.97 ns   1376.2x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/65536        46009 ns     35.3 ns   1303.4x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/262144      190569 ns      124 ns   1536.9x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1048576     746724 ns      479 ns   1559.1x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/50              51.6 ns     6.57 ns      7.9x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1024             826 ns     6.50 ns    127.1x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/4096            3323 ns     12.5 ns    265.8x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/8192            6619 ns     19.1 ns    346.5x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/16384          13495 ns     33.2 ns    406.5x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/65536          53668 ns      114 ns    470.8x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/262144        236277 ns      475 ns    497.4x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1048576       914177 ns     2157 ns    423.8x
-----------------------------------------------------------------------------------------

Segment size = 256

-----------------------------------------------------------------------------------------
Benchmark                                                    Before      After    Speedup
-----------------------------------------------------------------------------------------
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/50            38.1 ns     1.02 ns     37.4x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1024           689 ns     2.06 ns    334.5x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/4096          2815 ns     7.01 ns    401.6x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/8192          5507 ns     14.3 ns    385.1x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/16384        11050 ns     33.7 ns    327.9x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/65536        44197 ns      118 ns    374.6x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/262144      175793 ns      449 ns    391.5x
std::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1048576     703242 ns     2140 ns    328.7x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/50              50.2 ns     6.12 ns      8.2x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1024             835 ns     11.4 ns     73.2x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/4096            3353 ns     32.9 ns    101.9x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/8192            6711 ns     64.2 ns    104.5x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/16384          13231 ns      118 ns    112.1x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/65536          53523 ns      556 ns     96.3x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/262144        219101 ns     2166 ns    101.2x
std::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1048576       880277 ns    15852 ns     55.5x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/50            37.7 ns     1.13 ns     33.4x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1024           697 ns     2.14 ns    325.7x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/4096          2804 ns     7.52 ns    373.0x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/8192          5749 ns     15.2 ns    378.2x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/16384        11742 ns     34.8 ns    337.4x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/65536        47274 ns      116 ns    407.7x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/262144      187774 ns      444 ns    422.9x
rng::distance(join_view(vector&lt;vector&lt;int&gt;&gt;))/1048576     749724 ns     2109 ns    355.5x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/50              53.0 ns     6.09 ns      8.7x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1024             895 ns     11.0 ns     81.4x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/4096            3825 ns     30.6 ns    125.0x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/8192            7550 ns     60.5 ns    124.8x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/16384          14847 ns      112 ns    132.6x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/65536          56888 ns      453 ns    125.6x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/262144        231395 ns     2034 ns    113.8x
rng::distance(join_view(deque&lt;deque&lt;int&gt;&gt;))/1048576       933093 ns    15012 ns     62.2x
-----------------------------------------------------------------------------------------

Full diff: https://github.com/llvm/llvm-project/pull/133612.diff

4 Files Affected:

  • (modified) libcxx/include/__iterator/distance.h (+53-15)
  • (added) libcxx/test/benchmarks/iterators/distance.bench.cpp (+71)
  • (modified) libcxx/test/std/iterators/iterator.primitives/iterator.operations/distance.pass.cpp (+56-21)
  • (modified) libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.distance/iterator_sentinel.pass.cpp (+65-26)
diff --git a/libcxx/include/__iterator/distance.h b/libcxx/include/__iterator/distance.h
index 1732aa527f64a..8d14299191e30 100644
--- a/libcxx/include/__iterator/distance.h
+++ b/libcxx/include/__iterator/distance.h
@@ -10,41 +10,82 @@
 #ifndef _LIBCPP___ITERATOR_DISTANCE_H
 #define _LIBCPP___ITERATOR_DISTANCE_H
 
+#include <__algorithm/for_each_segment.h>
 #include <__config>
 #include <__iterator/concepts.h>
 #include <__iterator/incrementable_traits.h>
 #include <__iterator/iterator_traits.h>
+#include <__iterator/segmented_iterator.h>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
 #include <__ranges/size.h>
 #include <__type_traits/decay.h>
+#include <__type_traits/enable_if.h>
 #include <__type_traits/remove_cvref.h>
+#include <__utility/move.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _InputIter>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 typename iterator_traits<_InputIter>::difference_type
-__distance(_InputIter __first, _InputIter __last, input_iterator_tag) {
-  typename iterator_traits<_InputIter>::difference_type __r(0);
+#if _LIBCPP_STD_VER >= 20
+template <class _Iter>
+using __iter_distance_t _LIBCPP_NODEBUG = std::iter_difference_t<_Iter>;
+#else
+template <class _Iter>
+using __iter_distance_t _LIBCPP_NODEBUG = typename iterator_traits<_Iter>::difference_type;
+#endif
+
+template <class _InputIter, class _Sent>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 __iter_distance_t<_InputIter>
+__distance(_InputIter __first, _Sent __last) {
+  __iter_distance_t<_InputIter> __r(0);
   for (; __first != __last; ++__first)
     ++__r;
   return __r;
 }
 
-template <class _RandIter>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 typename iterator_traits<_RandIter>::difference_type
-__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag) {
+template <class _RandIter, __enable_if_t<__has_random_access_iterator_category<_RandIter>::value, int> = 0>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 __iter_distance_t<_RandIter>
+__distance(_RandIter __first, _RandIter __last) {
   return __last - __first;
 }
 
+template <class _SegmentedIter, class _Difference>
+struct __segment_distance {
+  using _Traits _LIBCPP_NODEBUG = __segmented_iterator_traits<_SegmentedIter>;
+
+  _Difference& __r_;
+
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __segment_distance(_Difference& __r) : __r_(__r) {}
+
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
+  operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
+    __r_ += std::__distance(__lfirst, __llast);
+  }
+};
+
+template <class _SegmentedIter,
+          __enable_if_t<!__has_random_access_iterator_category<_SegmentedIter>::value &&
+                            __is_segmented_iterator<_SegmentedIter>::value,
+                        int> = 0>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 __iter_distance_t<_SegmentedIter>
+__distance(_SegmentedIter __first, _SegmentedIter __last) {
+  using __difference_type = __iter_distance_t<_SegmentedIter>;
+  __difference_type __r(0);
+  std::__for_each_segment(__first, __last, std::__segment_distance<_SegmentedIter, __difference_type>(__r));
+  return __r;
+}
+
 template <class _InputIter>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 typename iterator_traits<_InputIter>::difference_type
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 __iter_distance_t<_InputIter>
 distance(_InputIter __first, _InputIter __last) {
-  return std::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
+  return std::__distance(__first, __last);
 }
 
 #if _LIBCPP_STD_VER >= 20
@@ -56,12 +97,7 @@ struct __distance {
   template <class _Ip, sentinel_for<_Ip> _Sp>
     requires(!sized_sentinel_for<_Sp, _Ip>)
   _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip> operator()(_Ip __first, _Sp __last) const {
-    iter_difference_t<_Ip> __n = 0;
-    while (__first != __last) {
-      ++__first;
-      ++__n;
-    }
-    return __n;
+    return std::__distance(std::move(__first), std::move(__last));
   }
 
   template <class _Ip, sized_sentinel_for<decay_t<_Ip>> _Sp>
@@ -92,4 +128,6 @@ inline constexpr auto distance = __distance{};
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ITERATOR_DISTANCE_H
diff --git a/libcxx/test/benchmarks/iterators/distance.bench.cpp b/libcxx/test/benchmarks/iterators/distance.bench.cpp
new file mode 100644
index 0000000000000..db9e6a9201658
--- /dev/null
+++ b/libcxx/test/benchmarks/iterators/distance.bench.cpp
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <cstddef>
+#include <deque>
+#include <iterator>
+#include <ranges>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+int main(int argc, char** argv) {
+  auto std_distance = [](auto first, auto last) { return std::distance(first, last); };
+
+  // {std,ranges}::distance
+  {
+    auto bm = []<class Container>(std::string name, auto distance, std::size_t seg_size) {
+      benchmark::RegisterBenchmark(
+          name,
+          [distance, seg_size](auto& st) {
+            std::size_t const size     = st.range(0);
+            std::size_t const segments = (size + seg_size - 1) / seg_size;
+            Container c(segments);
+            for (std::size_t i = 0, n = size; i < segments; ++i, n -= seg_size) {
+              c[i].resize(std::min(seg_size, n));
+            }
+
+            auto view  = c | std::views::join;
+            auto first = view.begin();
+            auto last  = view.end();
+
+            for ([[maybe_unused]] auto _ : st) {
+              benchmark::DoNotOptimize(c);
+              auto result = distance(first, last);
+              benchmark::DoNotOptimize(result);
+            }
+          })
+          ->Arg(50) // non power-of-two
+          ->Arg(1024)
+          ->Arg(4096)
+          ->Arg(8192)
+          ->Arg(1 << 14)
+          ->Arg(1 << 16)
+          ->Arg(1 << 18)
+          ->Arg(1 << 20);
+    };
+    bm.operator()<std::vector<std::vector<int>>>("std::distance(join_view(vector<vector<int>>))", std_distance, 256);
+    bm.operator()<std::deque<std::deque<int>>>("std::distance(join_view(deque<deque<int>>))", std_distance, 256);
+    bm.operator()<std::vector<std::vector<int>>>(
+        "rng::distance(join_view(vector<vector<int>>)", std::ranges::distance, 256);
+    bm.operator()<std::deque<std::deque<int>>>(
+        "rng::distance(join_view(deque<deque<int>>)", std::ranges::distance, 256);
+
+    // bm.operator()<std::vector<std::vector<int>>>("std::distance(join_view(vector<vector<int>>))", std_distance, 1024);
+    // bm.operator()<std::deque<std::deque<int>>>("std::distance(join_view(deque<deque<int>>))", std_distance, 1024);
+    // bm.operator()<std::vector<std::vector<int>>>("rng::distance(join_view(vector<vector<int>>)", std::ranges::distance, 1024);
+    // bm.operator()<std::deque<std::deque<int>>>("rng::distance(join_view(deque<deque<int>>)", std::ranges::distance, 1024);
+  }
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}
diff --git a/libcxx/test/std/iterators/iterator.primitives/iterator.operations/distance.pass.cpp b/libcxx/test/std/iterators/iterator.primitives/iterator.operations/distance.pass.cpp
index 13caefff92365..bcaeee14cad6d 100644
--- a/libcxx/test/std/iterators/iterator.primitives/iterator.operations/distance.pass.cpp
+++ b/libcxx/test/std/iterators/iterator.primitives/iterator.operations/distance.pass.cpp
@@ -16,38 +16,73 @@
 //   Iter::difference_type
 //   distance(Iter first, Iter last); // constexpr in C++17
 
-#include <iterator>
+#include <array>
 #include <cassert>
+#include <deque>
+#include <iterator>
+#include <vector>
 #include <type_traits>
 
 #include "test_macros.h"
 #include "test_iterators.h"
 
 template <class It>
-TEST_CONSTEXPR_CXX17
-void check_distance(It first, It last, typename std::iterator_traits<It>::difference_type dist)
-{
-    typedef typename std::iterator_traits<It>::difference_type Difference;
-    static_assert(std::is_same<decltype(std::distance(first, last)), Difference>::value, "");
-    assert(std::distance(first, last) == dist);
+TEST_CONSTEXPR_CXX17 void check_distance(It first, It last, typename std::iterator_traits<It>::difference_type dist) {
+  typedef typename std::iterator_traits<It>::difference_type Difference;
+  static_assert(std::is_same<decltype(std::distance(first, last)), Difference>::value, "");
+  assert(std::distance(first, last) == dist);
 }
 
-TEST_CONSTEXPR_CXX17 bool tests()
-{
-    const char* s = "1234567890";
-    check_distance(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 10);
-    check_distance(forward_iterator<const char*>(s), forward_iterator<const char*>(s+10), 10);
-    check_distance(bidirectional_iterator<const char*>(s), bidirectional_iterator<const char*>(s+10), 10);
-    check_distance(random_access_iterator<const char*>(s), random_access_iterator<const char*>(s+10), 10);
-    check_distance(s, s+10, 10);
-    return true;
+#if TEST_STD_VER >= 20
+void test_deque() {
+  using Container = std::deque<std::deque<double>>;
+  Container c;
+  auto view                    = c | std::views::join;
+  Container::difference_type n = 0;
+  for (std::size_t i = 0; i < 10; ++i) {
+    n += i;
+    c.push_back(Container::value_type(i));
+  }
+  assert(std::distance(view.begin(), view.end()) == n);
+}
+#endif
+
+TEST_CONSTEXPR_CXX17 bool tests() {
+  const char* s = "1234567890";
+  check_distance(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), 10);
+  check_distance(forward_iterator<const char*>(s), forward_iterator<const char*>(s + 10), 10);
+  check_distance(bidirectional_iterator<const char*>(s), bidirectional_iterator<const char*>(s + 10), 10);
+  check_distance(random_access_iterator<const char*>(s), random_access_iterator<const char*>(s + 10), 10);
+  check_distance(s, s + 10, 10);
+
+#if TEST_STD_VER >= 20
+  {
+    using Container = std::vector<std::vector<int>>;
+    Container c;
+    auto view                    = c | std::views::join;
+    Container::difference_type n = 0;
+    for (std::size_t i = 0; i < 10; ++i) {
+      n += i;
+      c.push_back(Container::value_type(i));
+    }
+    assert(std::distance(view.begin(), view.end()) == n);
+  }
+  {
+    using Container = std::array<std::array<char, 3>, 10>;
+    Container c;
+    auto view = c | std::views::join;
+    assert(std::distance(view.begin(), view.end()) == 30);
+  }
+  if (!TEST_IS_CONSTANT_EVALUATED)
+    test_deque();
+#endif
+  return true;
 }
 
-int main(int, char**)
-{
-    tests();
+int main(int, char**) {
+  tests();
 #if TEST_STD_VER >= 17
-    static_assert(tests(), "");
+  static_assert(tests(), "");
 #endif
-    return 0;
+  return 0;
 }
diff --git a/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.distance/iterator_sentinel.pass.cpp b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.distance/iterator_sentinel.pass.cpp
index b4199b73ad76a..a5fcf337387c2 100644
--- a/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.distance/iterator_sentinel.pass.cpp
+++ b/libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.distance/iterator_sentinel.pass.cpp
@@ -15,18 +15,21 @@
 // template<class I, sized_sentinel_for<decay_t<I>> S>
 //   constexpr iter_difference_t<I> ranges::distance(I&& first, S last); // TODO: update when LWG3664 is resolved
 
-#include <iterator>
+#include <array>
 #include <cassert>
+#include <deque>
+#include <iterator>
+#include <vector>
 
 #include "test_iterators.h"
 #include "test_macros.h"
 
-template<class It, class Sent>
+template <class It, class Sent>
 constexpr void test_unsized() {
   static_assert(std::sentinel_for<Sent, It> && !std::sized_sentinel_for<Sent, It>);
-  int a[3] = {1,2,3};
+  int a[3] = {1, 2, 3};
   {
-    It first = It(a);
+    It first  = It(a);
     auto last = Sent(It(a));
     assert(std::ranges::distance(first, last) == 0);
     assert(std::ranges::distance(It(a), last) == 0);
@@ -36,7 +39,7 @@ constexpr void test_unsized() {
   }
   {
     auto check = [&a]<class ItQual, class SentQual> {
-      It first = It(a);
+      It first  = It(a);
       Sent last = Sent(It(a + 3));
       assert(std::ranges::distance(static_cast<ItQual>(first), static_cast<SentQual>(last)) == 3);
     };
@@ -61,13 +64,13 @@ constexpr void test_unsized() {
   }
 }
 
-template<class It, class Sent>
+template <class It, class Sent>
 constexpr void test_sized() {
   static_assert(std::sized_sentinel_for<Sent, It>);
-  int a[] = {1,2,3};
+  int a[] = {1, 2, 3};
   {
     auto check = [&a]<class ItQual, class SentQual> {
-      It first = It(a + 3);
+      It first  = It(a + 3);
       Sent last = Sent(It(a));
       assert(std::ranges::distance(static_cast<ItQual>(first), static_cast<SentQual>(last)) == -3);
     };
@@ -91,7 +94,7 @@ constexpr void test_sized() {
     check.template operator()<const It&&, const Sent&&>();
   }
   {
-    It first = It(a);
+    It first  = It(a);
     auto last = Sent(It(a));
     assert(std::ranges::distance(first, last) == 0);
     assert(std::ranges::distance(It(a), last) == 0);
@@ -100,7 +103,7 @@ constexpr void test_sized() {
     ASSERT_SAME_TYPE(decltype(std::ranges::distance(It(a), Sent(It(a)))), std::iter_difference_t<It>);
   }
   {
-    It first = It(a);
+    It first  = It(a);
     auto last = Sent(It(a + 3));
     assert(std::ranges::distance(first, last) == 3);
     assert(std::ranges::distance(It(a), last) == 3);
@@ -110,13 +113,17 @@ constexpr void test_sized() {
 }
 
 struct StrideCounter {
-  int *it_;
-  int *inc_;
-  using value_type = int;
+  int* it_;
+  int* inc_;
+  using value_type      = int;
   using difference_type = int;
   explicit StrideCounter();
-  constexpr explicit StrideCounter(int *it, int *inc) : it_(it), inc_(inc) {}
-  constexpr auto& operator++() { ++it_; *inc_ += 1; return *this; }
+  constexpr explicit StrideCounter(int* it, int* inc) : it_(it), inc_(inc) {}
+  constexpr auto& operator++() {
+    ++it_;
+    *inc_ += 1;
+    return *this;
+  }
   StrideCounter operator++(int);
   int& operator*() const;
   bool operator==(StrideCounter) const;
@@ -125,11 +132,11 @@ static_assert(std::forward_iterator<StrideCounter>);
 static_assert(!std::sized_sentinel_for<StrideCounter, StrideCounter>);
 
 struct SizedStrideCounter {
-  int *it_;
-  int *minus_;
+  int* it_;
+  int* minus_;
   using value_type = int;
   explicit SizedStrideCounter();
-  constexpr explicit SizedStrideCounter(int *it, int *minus) : it_(it), minus_(minus) {}
+  constexpr explicit SizedStrideCounter(int* it, int* minus) : it_(it), minus_(minus) {}
   SizedStrideCounter& operator++();
   SizedStrideCounter operator++(int);
   int& operator*() const;
@@ -147,22 +154,34 @@ constexpr void test_stride_counting() {
     int a[] = {1, 2, 3};
     int inc = 0;
     StrideCounter first(a, &inc);
-    StrideCounter last(a+3, nullptr);
+    StrideCounter last(a + 3, nullptr);
     std::same_as<int> auto result = std::ranges::distance(first, last);
     assert(result == 3);
     assert(inc == 3);
   }
   {
-    int a[] = {1, 2, 3};
+    int a[]   = {1, 2, 3};
     int minus = 0;
     SizedStrideCounter first(a, &minus);
-    SizedStrideCounter last(a+3, nullptr);
+    SizedStrideCounter last(a + 3, nullptr);
     std::same_as<int> auto result = std::ranges::distance(first, last);
     assert(result == 3);
     assert(minus == 1);
   }
 }
 
+void test_deque() {
+  using Container = std::deque<std::deque<double>>;
+  Container c;
+  auto view                    = c | std::views::join;
+  Container::difference_type n = 0;
+  for (std::size_t i = 0; i < 10; ++i) {
+    n += i;
+    c.push_back(Container::value_type(i));
+  }
+  assert(std::ranges::distance(view.begin(), view.end()) == n);
+}
+
 constexpr bool test() {
   {
     int a[] = {1, 2, 3};
@@ -197,7 +216,7 @@ constexpr bool test() {
   test_sized<contiguous_iterator<int*>, contiguous_iterator<int*>>();
 
   {
-    using It = cpp20_input_iterator<int*>;  // non-copyable, thus not a sentinel for itself
+    using It = cpp20_input_iterator<int*>; // non-copyable, thus not a sentinel for itself
     static_assert(!std::is_copy_constructible_v<It>);
     static_assert(!std::sentinel_for<It, It>);
     static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&>);
@@ -206,10 +225,10 @@ constexpr bool test() {
     static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&&, It&&>);
   }
   {
-    using It = cpp20_input_iterator<int*>;  // non-copyable
-    using Sent = sentinel_wrapper<It>;  // not a sized sentinel
+    using It   = cpp20_input_iterator<int*>; // non-copyable
+    using Sent = sentinel_wrapper<It>;       // not a sized sentinel
     static_assert(std::sentinel_for<Sent, It> && !std::sized_sentinel_for<Sent, It>);
-    int a[] = {1,2,3};
+    int a[]   = {1, 2, 3};
     Sent last = Sent(It(a + 3));
     static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, Sent&>);
     static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, Sent&&>);
@@ -217,7 +236,7 @@ constexpr bool test() {
     assert(std::ranges::distance(It(a), Sent(It(a + 3))) == 3);
   }
   {
-    using It = cpp17_input_iterator<int*>;  // not a sentinel for itself
+    using It = cpp17_input_iterator<int*>; // not a sentinel for itself
     static_assert(!std::sentinel_for<It, It>);
     static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&>);
     static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&&>);
@@ -231,6 +250,26 @@ constexpr bool test() {
   static_assert(!std::is_invocable_v<decltype(std::ranges::distance), int, int*>);
   static_assert(!std::is_invocable_v<decltype(std::ranges::distance), int*, char*>);
 
+  {
+    using Container = std::vector<std::vector<int>>;
+    Container c;
+    auto view                    = c | std::views::join;
+    Container::difference_type n = 0;
+    for (std::size_t i = 0; i < 10; ++i) {
+      n += i;
+      c.push_back(Container::value_type(i));
+    }
+    assert(std::ranges::distance(view.begin(), view.end()) == n);
+  }
+  {
+    using Container = std::array<std::array<char, 3>, 10>;
+    Container c;
+    auto view = c | std::views::join;
+    assert(std::ranges::distance(view.begin(), view.end()) == 30);
+  }
+  if (!TEST_IS_CONSTANT_EVALUATED)
+    test_deque();
+
   return true;
 }
 

@winner245 winner245 force-pushed the segmented-distance branch from 7f48baf to ec7986a Compare June 16, 2025 18:00
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically LGTM with a question/suggestion.

typename iterator_traits<_InputIter>::difference_type __r(0);
#if _LIBCPP_STD_VER >= 20
template <class _Iter>
using __iter_distance_t _LIBCPP_NODEBUG = std::iter_difference_t<_Iter>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have __iter_diff_t in iterator_traits.h. It doesn't do exactly what you want, but I think we should make it do what you need instead of introducing a new thing that's essentially identical.

Basically, we should try re-defining __iter_diff_t in iterator_traits.h as:

#if _LIBCPP_STD_VER >= 20
template <class _Iter>
using __iter_diff_t _LIBCPP_NODEBUG = std::iter_difference_t<_Iter>;
#else
template <class _Iter>
using __iter_diff_t _LIBCPP_NODEBUG = typename iterator_traits<_Iter>::difference_type;
#endif

And if that's not possible for some reason, it would be interesting to understand why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the standard hasn't requires iterator_traits<I>::difference_type to be same as iter_difference_t<I> yet when I is a non-random-access iterator type. One is allowed to make them different by specializing incrementable_traits. This is perhaps a defect, for which I've submitted LWG4080.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the potential mismatch between iterator_traits<I>::difference_type and iter_difference_t<I> (as pointed out by @frederick-vs-ja), I am now consistently using iterator_traits<I>::difference_type as the return type for the non-range std::__distance algorithm, and iter_difference_t<I> for the range algorithm.

To enable the range algorithm to reuse the same optimization introduced for the non-range std::__distance, I am now adding a static_cast to explicitly cast the result of std::__distance to the desired type iter_difference_t<I>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am now consistently using iterator_traits<I>::difference_type as the return type for the non-range std::__distance algorithm.

However, this approach fails when the range algorithm std::ranges::distance forwards to std::__distance with I = cpp20_input_iterator, resulting in a hard substitution error: no type named 'difference_type' in 'std::iterator_traits<cpp20_input_iterator<int *>>', as captured by the tests range.pass.cpp and iterator_sentinel.pass.cpp (see the full log: stage1 (generic-modules, clang-21, clang++-21)).

The root cause for this error is that cpp20_input_iterator does not have the member type iterator_category (although it provides the iterator_concept member type), leading to a hard substitution failure in std::iterator_traits<cpp20_input_iterator<Iter>>::difference_type (See A reproducer).

To resolve this, I still need the above internal definition of __iter_distance_t, which is used as the return type of the internal function std::__distance. This allows both the non-range std::distance and range version std::ranges::distance to share the same implementation and optimization. However, to handle the potential mismatch between iterator_traits<I>::difference_type and iter_difference_t<I>, I explicitly specified iterator_traits<I>::difference_type as the return type of std::distance and iter_difference_t<I> as the return type of std::ranges::distance. This way the underlying implicit conversion would automatically take place when there is a type mismatch.

Basically, we should try re-defining __iter_diff_t in iterator_traits.h as:

#if _LIBCPP_STD_VER >= 20
template <class _Iter>
using __iter_diff_t _LIBCPP_NODEBUG = std::iter_difference_t<_Iter>;
#else
template <class _Iter>
using __iter_diff_t _LIBCPP_NODEBUG = typename iterator_traits<_Iter>::difference_type;
#endif

And if that's not possible for some reason, it would be interesting to understand why.

I am a bit hesitant to make this change because __iter_diff_t is currently unconditionally defined as iterator_traits<_Iter>::difference_type, and it is used by many algorithms. Making this change would introduce the potential type mismatch to many algorithms. My current approach with the introduction of __iter_distance_t ensures that the potential type mismatch is confined and handled within distance.h.

Comment on lines 49 to 52
// ->Arg(1 << 14)
// ->Arg(1 << 16)
// ->Arg(1 << 18)
// ->Arg(1 << 20);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave those commented? Let's either keep them uncommented or drop them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've dropped these large benchmark sizes, which were only used when running the benchmarks for this patch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not attached to this file: let's add a release note!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

return __last - __first;
}

template <class _SegmentedIter, class _Difference>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to apply this optimization before C++20? AFAICT this only applies to join_view and guarding this for C++20 would make the whole thing a lot simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've now made this optimization only for >= C++20.

@winner245 winner245 force-pushed the segmented-distance branch from 35476ae to dd5db8c Compare June 26, 2025 21:19
@winner245 winner245 force-pushed the segmented-distance branch from dd5db8c to 4ef096d Compare June 27, 2025 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants