[libcxx-commits] [libcxx] [libc++] optimization on ranges::drop_view::begin (#72883) (PR #72929)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 20 16:41:03 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Hongyu Ouyang (casavaca)

<details>
<summary>Changes</summary>

as pointed out from the github issue #<!-- -->72883 :
> ... the implementation only needs to return the value of ranges::next
  and does not need to obtain the value through ranges::advance, which will have
  O(n) complexity in the case of random-access-sized but non-common range.

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


1 Files Affected:

- (modified) libcxx/include/__ranges/drop_view.h (+2-1) 


``````````diff
diff --git a/libcxx/include/__ranges/drop_view.h b/libcxx/include/__ranges/drop_view.h
index f10476f0011e739..2557d030fd61145 100644
--- a/libcxx/include/__ranges/drop_view.h
+++ b/libcxx/include/__ranges/drop_view.h
@@ -104,7 +104,8 @@ namespace ranges {
     constexpr auto begin() const
       requires random_access_range<const _View> && sized_range<const _View>
     {
-      return ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
+      auto __dist = ranges::distance(__base_);
+      return ranges::begin(__base_) + std::min<range_difference_t<_View>>(__count_, __dist);
     }
 
     _LIBCPP_HIDE_FROM_ABI

``````````

</details>


https://github.com/llvm/llvm-project/pull/72929


More information about the libcxx-commits mailing list