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

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 22 23:26:14 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_);
----------------
huixie90 wrote:

> > Only narrowing conversion can be explicit-only. IIUC if size_t -> difeerence_t is narrowing conversion, I think we have other problems.
> 
> The standard does _not_ require the signedness of the return value of `ranges::size`. The only guarantee is that it is an integer-like type. So in theory, the types returned by `ranges::size` and `ranges::distance` can be unrelated and only be _explicitly_ converted.
> 
> > The problem of ranges::distance is that it is not for the purpose of calculating the size in constant time.
> 
> This is why the `sized_range` needs to be satisfied, isn't it? in which case `ranges::distance` is _guaranteed_ to be constant-time by its definition.

Anyway I think our discussion about coding style should not block this patch to be landed

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


More information about the libcxx-commits mailing list