[libcxx-commits] [libcxx] Libcxx nth element small ranges (PR #215698)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Aug 12 06:04:29 PDT 2026
================
@@ -84,6 +84,55 @@ int main(int argc, char** argv) {
register_bm(support::single_element_data<int>, "repeated");
}
+ // Benchmark std::nth_element on many independent fixed-size windows. This models
+ // the median-of-3 / median-of-5 filter use case from #214238, where the window
+ // size is a compile-time constant and the specialized sorting-network path is
+ // expected to constant-fold away the general algorithm.
+ {
+ auto bm = []<std::size_t Window>(std::string name, auto generate_data) {
+ benchmark::RegisterBenchmark(
+ name,
+ [generate_data](auto& st) {
+ std::size_t const num_windows = st.range(0);
+ constexpr std::size_t BatchSize = 32;
+ std::vector<int> flat = generate_data(num_windows * Window);
+ std::array<std::vector<std::array<int, Window>>, BatchSize> batches;
+ for (std::size_t b = 0; b != BatchSize; ++b) {
+ batches[b].resize(num_windows);
+ for (std::size_t i = 0; i != num_windows; ++i) {
+ std::copy_n(flat.begin() + static_cast<std::ptrdiff_t>(i * Window), Window, batches[b][i].begin());
+ }
+ }
----------------
ldionne wrote:
This seems simpler:
```suggestion
for (std::size_t b = 0; b != BatchSize; ++b) {
batches[b].resize(num_windows);
for (std::size_t i = 0; i != num_windows; ++i) {
auto data = generate_data(Window);
std::fill_n(data.begin(), Window, batches[b][i].begin())
}
}
```
WDYT? This is setup code so efficiency doesn't matter too much.
https://github.com/llvm/llvm-project/pull/215698
More information about the libcxx-commits
mailing list