[libcxx-commits] [libcxx] [libc++] Optimize ranges::move{, _backward} for vector<bool>::iterator (PR #121109)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 11 14:07:19 PST 2025
================
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// 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, c++20
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <ranges>
+#include <vector>
+
+template <bool aligned>
+void bm_ranges_move_vb(benchmark::State& state) {
+ auto n = state.range();
+ std::vector<bool> v1(n, true);
+ std::vector<bool> v2(n, false);
+ benchmark::DoNotOptimize(v1);
+ benchmark::DoNotOptimize(v2);
+ std::vector<bool>* in = &v1;
+ std::vector<bool>* out = &v2;
+ for (auto _ : state) {
+ if constexpr (aligned) {
+ benchmark::DoNotOptimize(std::ranges::move(*in, std::ranges::begin(*out)));
+ } else {
+ benchmark::DoNotOptimize(std::ranges::move(*in | std::views::drop(4), std::ranges::begin(*out)));
+ }
----------------
winner245 wrote:
The tests and benchmarks are implemented using `std::views::counted`, which internally calls `std::ranges::subrange` for random-access iterators (true for `vector<bool>::iterator`). I chose `std::views::counted` instead of directly calling `std::ranges::subrange` because it leads to the minimum changes compared to the previous version that used `drop_view`.
https://github.com/llvm/llvm-project/pull/121109
More information about the libcxx-commits
mailing list