Skip to content

Commit 96cce8f

Browse files
Add "reserved" benchmark for preallocated vector
1 parent 6ec116c commit 96cce8f

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

tests/beman/any_view/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function(beman_add_test)
2929
gtest_discover_tests(${target})
3030
endfunction()
3131

32-
beman_add_benchmark(TARGET all SOURCES all.benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp)
33-
beman_add_benchmark(TARGET take SOURCES take.benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp)
32+
beman_add_benchmark(TARGET all SOURCES all.benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp detail/reserved.cpp)
33+
beman_add_benchmark(TARGET take SOURCES take.benchmark.cpp detail/eager.cpp detail/fused.cpp detail/lazy.cpp detail/products.cpp detail/reserved.cpp)
3434

3535
beman_add_test(TARGET concepts SOURCES concepts.test.cpp)
3636
beman_add_test(TARGET constexpr SOURCES constexpr.test.cpp)

tests/beman/any_view/all.benchmark.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "detail/fused.hpp"
55
#include "detail/lazy.hpp"
66
#include "detail/products.hpp"
7+
#include "detail/reserved.hpp"
78

89
#include <benchmark/benchmark.h>
910

@@ -57,8 +58,22 @@ static void BM_all_lazy(benchmark::State& state) {
5758
}
5859
}
5960

61+
static void BM_all_reserved(benchmark::State& state) {
62+
const auto size = state.range(0);
63+
const auto begin = global_products.begin();
64+
65+
reserved::database db{.products = {begin, begin + size}};
66+
67+
for (auto _ : state) {
68+
for (std::string_view name : db.get_products({.min_quantity = 10})) {
69+
use(name);
70+
}
71+
}
72+
}
73+
6074
BENCHMARK(BM_all_eager)->RangeMultiplier(2)->Range(1 << 10, max_size);
6175
BENCHMARK(BM_all_fused)->RangeMultiplier(2)->Range(1 << 10, max_size);
6276
BENCHMARK(BM_all_lazy)->RangeMultiplier(2)->Range(1 << 10, max_size);
77+
BENCHMARK(BM_all_reserved)->RangeMultiplier(2)->Range(1 << 10, max_size);
6378

6479
BENCHMARK_MAIN();

tests/beman/any_view/detail/eager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ auto eager::database::get_products(query_t query) const -> names_t {
77

88
for (const auto& product : products) {
99
if (product.quantity >= query.min_quantity) {
10-
results.push_back(product.name);
10+
results.emplace_back(product.name);
1111
}
1212
}
1313

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
#include "reserved.hpp"
4+
5+
#include <algorithm>
6+
7+
auto reserved::database::get_products(query_t query) const -> names_t {
8+
const auto capacity = std::ranges::count_if(
9+
products, [=](const product_t& product) -> bool { return product.quantity >= query.min_quantity; });
10+
11+
names_t results;
12+
results.reserve(capacity);
13+
14+
for (const auto& product : products) {
15+
if (product.quantity >= query.min_quantity) {
16+
results.emplace_back(product.name);
17+
}
18+
}
19+
20+
return results;
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
#pragma once
4+
5+
#include "product.hpp"
6+
7+
#include <vector>
8+
9+
namespace reserved {
10+
11+
using names_t = std::vector<std::string_view>;
12+
13+
struct database {
14+
std::vector<product_t> products;
15+
16+
auto get_products(query_t) const -> names_t;
17+
};
18+
19+
} // namespace reserved

tests/beman/any_view/take.benchmark.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "detail/fused.hpp"
55
#include "detail/lazy.hpp"
66
#include "detail/products.hpp"
7+
#include "detail/reserved.hpp"
78

89
#include <benchmark/benchmark.h>
910

@@ -57,8 +58,22 @@ static void BM_take_lazy(benchmark::State& state) {
5758
}
5859
}
5960

61+
static void BM_take_reserved(benchmark::State& state) {
62+
const auto size = state.range(0);
63+
const auto begin = global_products.begin();
64+
65+
reserved::database db{.products = {begin, begin + size}};
66+
67+
for (auto _ : state) {
68+
for (std::string_view name : db.get_products({.min_quantity = 10}) | std::views::take(100)) {
69+
use(name);
70+
}
71+
}
72+
}
73+
6074
BENCHMARK(BM_take_eager)->RangeMultiplier(2)->Range(1 << 10, max_size);
6175
BENCHMARK(BM_take_fused)->RangeMultiplier(2)->Range(1 << 10, max_size);
6276
BENCHMARK(BM_take_lazy)->RangeMultiplier(2)->Range(1 << 10, max_size);
77+
BENCHMARK(BM_take_reserved)->RangeMultiplier(2)->Range(1 << 10, max_size);
6378

6479
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)