[libcxx-commits] [libcxx] [libc++] Optimize std::find_if (PR #167697)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 18 05:01:15 PST 2025
================
@@ -0,0 +1,51 @@
+#include <benchmark/benchmark.h>
+
+template <class Iter, class ValueT>
+Iter my_find(Iter first, Iter last, const ValueT& i) {
+ for (; first != last; ++first) {
+ if (*first == i)
+ break;
+ }
+ return first;
+}
+
+static auto bm_find_if_no_vectorization(benchmark::State& state) {
+ std::size_t const size = 8192;
+ std::vector<short> c(size, 0);
+
+ for ([[maybe_unused]] auto _ : state) {
+ benchmark::DoNotOptimize(c);
+ std::vector<short>::iterator result;
+ [[clang::noinline]] result = my_find(c.begin(), c.end(), 1);
+ benchmark::DoNotOptimize(result);
+ }
+}
+BENCHMARK(bm_find_if_no_vectorization);
+
+static auto bm_find_if_autovectorization(benchmark::State& state) {
+ std::size_t const size = 8192;
+ std::vector<short> c(size, 0);
+
+ for ([[maybe_unused]] auto _ : state) {
+ benchmark::DoNotOptimize(c);
+ std::vector<short>::iterator result;
+ [[clang::noinline]] result = find_if(c.begin(), c.end(), [](short i) { return i == 1; });
----------------
philnik777 wrote:
I've also noticed that this fails to vectorize: https://godbolt.org/z/s3rGvEW4K.
https://github.com/llvm/llvm-project/pull/167697
More information about the libcxx-commits
mailing list