[libcxx-commits] [libcxx] d965960 - Revert "[libc++] Optimize for_each for segmented iterators"
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jun 5 10:00:15 PDT 2023
Author: Nikolas Klauser
Date: 2023-06-05T10:00:02-07:00
New Revision: d965960fcf901eddc6e89d95293b589cb7849fc1
URL: https://github.com/llvm/llvm-project/commit/d965960fcf901eddc6e89d95293b589cb7849fc1
DIFF: https://github.com/llvm/llvm-project/commit/d965960fcf901eddc6e89d95293b589cb7849fc1.diff
LOG: Revert "[libc++] Optimize for_each for segmented iterators"
This reverts commit b1dc43aa3a05c2f14725e2e6428544208ccbe161.
Added:
libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp
Modified:
libcxx/benchmarks/CMakeLists.txt
libcxx/include/__algorithm/for_each.h
libcxx/utils/data/ignore_format.txt
Removed:
libcxx/benchmarks/algorithms/for_each.bench.cpp
libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each.pass.cpp
################################################################################
diff --git a/libcxx/benchmarks/CMakeLists.txt b/libcxx/benchmarks/CMakeLists.txt
index 99964cf53eb52..daa6fa2665f7b 100644
--- a/libcxx/benchmarks/CMakeLists.txt
+++ b/libcxx/benchmarks/CMakeLists.txt
@@ -160,7 +160,6 @@ set(BENCHMARK_TESTS
algorithms.partition_point.bench.cpp
algorithms/equal.bench.cpp
algorithms/find.bench.cpp
- algorithms/for_each.bench.cpp
algorithms/lower_bound.bench.cpp
algorithms/make_heap.bench.cpp
algorithms/make_heap_then_sort_heap.bench.cpp
diff --git a/libcxx/benchmarks/algorithms/for_each.bench.cpp b/libcxx/benchmarks/algorithms/for_each.bench.cpp
deleted file mode 100644
index 7019dc13ca601..0000000000000
--- a/libcxx/benchmarks/algorithms/for_each.bench.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include <algorithm>
-#include <benchmark/benchmark.h>
-#include <deque>
-
-static void bm_deque_for_each(benchmark::State& state) {
- std::deque<char> vec1(state.range(), '1');
- for (auto _ : state) {
- benchmark::DoNotOptimize(vec1);
- benchmark::DoNotOptimize(
- std::for_each(vec1.begin(), vec1.end(), [](char& v) { v = std::clamp(v, (char)10, (char)100); }));
- }
-}
-BENCHMARK(bm_deque_for_each)->DenseRange(1, 8)->Range(16, 1 << 20);
-
-BENCHMARK_MAIN();
diff --git a/libcxx/include/__algorithm/for_each.h b/libcxx/include/__algorithm/for_each.h
index 5e273cf1b9b19..6564f31cd0e79 100644
--- a/libcxx/include/__algorithm/for_each.h
+++ b/libcxx/include/__algorithm/for_each.h
@@ -10,11 +10,7 @@
#ifndef _LIBCPP___ALGORITHM_FOR_EACH_H
#define _LIBCPP___ALGORITHM_FOR_EACH_H
-#include <__algorithm/for_each_segment.h>
#include <__config>
-#include <__iterator/segmented_iterator.h>
-#include <__type_traits/enable_if.h>
-#include <__utility/move.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -23,25 +19,14 @@
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIterator, class _Function>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
-for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function for_each(_InputIterator __first,
+ _InputIterator __last,
+ _Function __f) {
for (; __first != __last; ++__first)
__f(*__first);
return __f;
}
-#if _LIBCPP_STD_VER >= 20
-template <class _SegmentedIterator, class _Function>
- requires __is_segmented_iterator<_SegmentedIterator>::value
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
-for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func) {
- std::__for_each_segment(__first, __last, [&](auto __lfirst, auto __llast) {
- __func = std::for_each(__lfirst, __llast, std::move(__func));
- });
- return __func;
-}
-#endif // _LIBCPP_STD_VER >= 20
-
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___ALGORITHM_FOR_EACH_H
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each.pass.cpp
deleted file mode 100644
index 6b39bcd2f1574..0000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each.pass.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// template<InputIterator Iter, Callable<auto, Iter::reference> Function>
-// requires CopyConstructible<Function>
-// constexpr Function // constexpr after C++17
-// for_each(Iter first, Iter last, Function f);
-
-#include <algorithm>
-#include <cassert>
-#include <deque>
-
-#include "test_macros.h"
-#include "test_iterators.h"
-
-struct for_each_test {
- TEST_CONSTEXPR for_each_test(int c) : count(c) {}
- int count;
- TEST_CONSTEXPR_CXX14 void operator()(int& i) {
- ++i;
- ++count;
- }
-};
-
-TEST_CONSTEXPR_CXX20 bool test() {
- int ia[] = {0, 1, 2, 3, 4, 5};
- const unsigned s = sizeof(ia) / sizeof(ia[0]);
- for_each_test f = std::for_each(cpp17_input_iterator<int*>(ia), cpp17_input_iterator<int*>(ia + s), for_each_test(0));
- assert(f.count == s);
- for (unsigned i = 0; i < s; ++i)
- assert(ia[i] == static_cast<int>(i + 1));
-
- return true;
-}
-
-struct deque_test {
- std::deque<int>* d_;
- int* i_;
-
- deque_test(std::deque<int>& d, int& i) : d_(&d), i_(&i) {}
-
- void operator()(int& v) {
- assert(&(*d_)[(*i_)++] == &v);
- }
-};
-
-int main(int, char**) {
- test();
-#if TEST_STD_VER >= 20
- static_assert(test());
-#endif
-
- // check that segmented iterators work properly
- std::deque<int> d(50);
- int index = 0;
-
- std::for_each(d.begin(), d.end(), deque_test(d, index));
-
- return 0;
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp
new file mode 100644
index 0000000000000..d6b46554e95ec
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// template<InputIterator Iter, Callable<auto, Iter::reference> Function>
+// requires CopyConstructible<Function>
+// constexpr Function // constexpr after C++17
+// for_each(Iter first, Iter last, Function f);
+
+#include <algorithm>
+#include <cassert>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+#if TEST_STD_VER > 17
+TEST_CONSTEXPR bool test_constexpr() {
+ int ia[] = {1, 3, 6, 7};
+ int expected[] = {3, 5, 8, 9};
+
+ std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; });
+ return std::equal(std::begin(ia), std::end(ia), std::begin(expected))
+ ;
+ }
+#endif
+
+struct for_each_test
+{
+ for_each_test(int c) : count(c) {}
+ int count;
+ void operator()(int& i) {++i; ++count;}
+};
+
+int main(int, char**)
+{
+ int ia[] = {0, 1, 2, 3, 4, 5};
+ const unsigned s = sizeof(ia)/sizeof(ia[0]);
+ for_each_test f = std::for_each(cpp17_input_iterator<int*>(ia),
+ cpp17_input_iterator<int*>(ia+s),
+ for_each_test(0));
+ assert(f.count == s);
+ for (unsigned i = 0; i < s; ++i)
+ assert(ia[i] == static_cast<int>(i+1));
+
+#if TEST_STD_VER > 17
+ static_assert(test_constexpr());
+#endif
+
+ return 0;
+}
diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 1777c1d90d450..3e9728ab6f387 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -49,6 +49,7 @@ libcxx/include/__algorithm/fill.h
libcxx/include/__algorithm/fill_n.h
libcxx/include/__algorithm/find_end.h
libcxx/include/__algorithm/find_first_of.h
+libcxx/include/__algorithm/for_each.h
libcxx/include/__algorithm/for_each_n.h
libcxx/include/__algorithm/generate.h
libcxx/include/__algorithm/generate_n.h
More information about the libcxx-commits
mailing list