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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 21 11:49:27 PST 2023


================
@@ -81,6 +81,36 @@ constexpr bool test() {
 
   static_assert(!BeginInvocable<const ForwardView>);
 
+  {
+    // non-common non-simple view,
+    // random_access_range<const V> && sized_range<const V>
+    // The wording of the standard is:
+    // Returns: ranges​::​next(ranges​::​begin(base_), count_, ranges​::​end(base_))
+    // Note that "Returns" is used here, meaning that we don't have to do it this way.
+    // In fact, this will use ranges::advance that has O(n) on non-common range.
+    // Here, we test by counting how many times sentinel eq function is called.
+    // If ranges::advance is used, it will be called N times,
+    // but if we optimized it, it will be called 0 times.
+    int sentinel_cmp_calls = 0;
+    using NonCommonView    = MaybeSimpleNonCommonView<false>;
+    static_assert(std::ranges::random_access_range<const NonCommonView>);
+    static_assert(std::ranges::sized_range<const NonCommonView>);
+    std::ranges::drop_view dropView9(NonCommonView{{}, 0, &sentinel_cmp_calls}, 4);
+    assert(dropView9.begin() == globalBuff + 4);
+    assert(sentinel_cmp_calls == 0);
+  }
+
+  {
+    // non-common simple view, same as above.
+    int sentinel_cmp_calls = 0;
+    using NonCommonView    = MaybeSimpleNonCommonView<true>;
+    static_assert(std::ranges::random_access_range<const NonCommonView>);
+    static_assert(std::ranges::sized_range<const NonCommonView>);
+    std::ranges::drop_view dropView9(NonCommonView{{}, 0, &sentinel_cmp_calls}, 4);
+    assert(dropView9.begin() == globalBuff + 4);
+    assert(sentinel_cmp_calls == 0);
----------------
huixie90 wrote:

The tests under libcxx/std are used by other implementations too (e.g. MSVC STL). It is not required by the spec that it has to be called zero times. I would instead relax this check a bit. For example , calling begin in the loop and check that we is not grow linearly ?

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


More information about the libcxx-commits mailing list