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

Hewill Kang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 22 23:56:34 PST 2023


================
@@ -90,6 +90,10 @@ namespace ranges {
       requires (!(__simple_view<_View> &&
                   random_access_range<const _View> && sized_range<const _View>))
     {
+      if constexpr (random_access_range<_View> && sized_range<_View>) {
+        range_difference_t<_View> __dist = ranges::distance(__base_);
----------------
hewillk wrote:

> If you have a helper
> 
> Void helper() { if (c) F(); else G(); }
> 
> When you definitely don’t want to call G(), why do you write
> 
> if(c) helper()
> 
> Rather than call f() directly ?

This example is not quite accurate because the return type of `ranges::distance` is what we want, which reduces the type conversion since the former has already been done for us. Using `ranges::size` requires an additional `static_cast`.

I believe the standard prefers using `rangs::begin(r) + rangs::distance(r)` rather than `rangs::begin(r) + static_cast<range_difference_t<R>>(rangs::size(r))`, given the former's heavy presence in the `<ranges>`'s wording.

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


More information about the libcxx-commits mailing list