- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15k
[libc++] Optimize std::for_each_n for segmented iterators #135468
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
          
     Merged
      
        
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|  | ||
| #ifndef _LIBCPP___ALGORITHM_FOR_EACH_N_SEGMENT_H | ||
| #define _LIBCPP___ALGORITHM_FOR_EACH_N_SEGMENT_H | ||
|  | ||
| #include <__config> | ||
| #include <__iterator/iterator_traits.h> | ||
| #include <__iterator/segmented_iterator.h> | ||
|  | ||
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
| # pragma GCC system_header | ||
| #endif | ||
|  | ||
| _LIBCPP_BEGIN_NAMESPACE_STD | ||
|  | ||
| // __for_each_n_segment optimizes linear iteration over segmented iterators. It processes a segmented | ||
| // input range [__first, __first + __n) by applying the functor __func to each element within the segment. | ||
| // The return value of __func is ignored, and the function returns an iterator pointing to one past the | ||
| // last processed element in the input range. | ||
|  | ||
| template <class _SegmentedIterator, class _Size, class _Functor> | ||
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator | ||
| __for_each_n_segment(_SegmentedIterator __first, _Size __orig_n, _Functor __func) { | ||
| static_assert(__is_segmented_iterator<_SegmentedIterator>::value && | ||
| __has_random_access_iterator_category< | ||
| typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator>::value, | ||
| "__for_each_n_segment only works with segmented iterators with random-access local iterators"); | ||
| if (__orig_n <= 0) | ||
| return __first; | ||
|  | ||
| using _Traits = __segmented_iterator_traits<_SegmentedIterator>; | ||
| using __local_iter_t = typename _Traits::__local_iterator; | ||
| using __difference_t = typename std::iterator_traits<__local_iter_t>::difference_type; | ||
| __difference_t __n = __orig_n; | ||
| auto __seg = _Traits::__segment(__first); | ||
| auto __local_first = _Traits::__local(__first); | ||
| __local_iter_t __local_last; | ||
|  | ||
| while (__n > 0) { | ||
| __local_last = _Traits::__end(__seg); | ||
| auto __seg_size = __local_last - __local_first; | ||
| if (__n <= __seg_size) { | ||
| __local_last = __local_first + __n; | ||
| __func(__local_first, __local_last); | ||
| break; | ||
| } | ||
| __func(__local_first, __local_last); | ||
| __n -= __seg_size; | ||
| __local_first = _Traits::__begin(++__seg); | ||
| } | ||
|  | ||
| return _Traits::__compose(__seg, __local_last); | ||
| } | ||
|  | ||
| _LIBCPP_END_NAMESPACE_STD | ||
|  | ||
| #endif // _LIBCPP___ALGORITHM_FOR_EACH_N_SEGMENT_H | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            98 changes: 98 additions & 0 deletions
          
          98 
        
  libcxx/test/benchmarks/algorithms/nonmodifying/for_each_n.bench.cpp
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 <algorithm> | ||
| #include <cstddef> | ||
| #include <deque> | ||
| #include <list> | ||
| #include <ranges> | ||
| #include <string> | ||
| #include <vector> | ||
|  | ||
| #include <benchmark/benchmark.h> | ||
|  | ||
| int main(int argc, char** argv) { | ||
| auto std_for_each_n = [](auto first, auto n, auto f) { return std::for_each_n(first, n, f); }; | ||
|  | ||
| // std::for_each_n | ||
| { | ||
| auto bm = []<class Container>(std::string name, auto for_each_n) { | ||
| using ElemType = typename Container::value_type; | ||
| benchmark::RegisterBenchmark( | ||
| name, | ||
| [for_each_n](auto& st) { | ||
| std::size_t const n = st.range(0); | ||
| Container c(n, 1); | ||
| auto first = c.begin(); | ||
|  | ||
| for ([[maybe_unused]] auto _ : st) { | ||
| benchmark::DoNotOptimize(c); | ||
| auto result = for_each_n(first, n, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); }); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
| }) | ||
| ->Arg(8) | ||
| ->Arg(32) | ||
| ->Arg(50) // non power-of-two | ||
| ->Arg(1024) | ||
| ->Arg(4096) | ||
| ->Arg(8192) | ||
| ->Arg(1 << 14) | ||
| ->Arg(1 << 16) | ||
| ->Arg(1 << 18); | ||
| }; | ||
| bm.operator()<std::vector<int>>("std::for_each_n(vector<int>)", std_for_each_n); | ||
| bm.operator()<std::deque<int>>("std::for_each_n(deque<int>)", std_for_each_n); | ||
| bm.operator()<std::list<int>>("std::for_each_n(list<int>)", std_for_each_n); | ||
| } | ||
|  | ||
| // std::for_each_n for join_view | ||
| { | ||
| auto bm = []<class Container>(std::string name, auto for_each_n) { | ||
| using C1 = typename Container::value_type; | ||
| using ElemType = typename C1::value_type; | ||
| benchmark::RegisterBenchmark( | ||
| name, | ||
| [for_each_n](auto& st) { | ||
| std::size_t const size = st.range(0); | ||
| std::size_t const seg_size = 256; | ||
| 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), ElemType(1)); | ||
| } | ||
|  | ||
| auto view = c | std::views::join; | ||
| auto first = view.begin(); | ||
|  | ||
| for ([[maybe_unused]] auto _ : st) { | ||
| benchmark::DoNotOptimize(c); | ||
| auto result = for_each_n(first, size, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); }); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
| }) | ||
| ->Arg(8) | ||
| ->Arg(32) | ||
| ->Arg(50) // non power-of-two | ||
| ->Arg(1024) | ||
| ->Arg(4096) | ||
| ->Arg(8192) | ||
| ->Arg(1 << 14) | ||
| ->Arg(1 << 16) | ||
| ->Arg(1 << 18); | ||
| }; | ||
| bm.operator()<std::vector<std::vector<int>>>("std::for_each_n(join_view(vector<vector<int>>))", std_for_each_n); | ||
| } | ||
|  | ||
| benchmark::Initialize(&argc, argv); | ||
| benchmark::RunSpecifiedBenchmarks(); | ||
| benchmark::Shutdown(); | ||
| return 0; | ||
| } | 
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.