Skip to content

Commit 02d0f1f

Browse files
Add "reserved" benchmark for preallocated vector
1 parent 452f71a commit 02d0f1f

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-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
@@ -2,6 +2,7 @@
22
#include "detail/fused.hpp"
33
#include "detail/lazy.hpp"
44
#include "detail/products.hpp"
5+
#include "detail/reserved.hpp"
56

67
#include <benchmark/benchmark.h>
78

@@ -55,8 +56,22 @@ static void BM_all_lazy(benchmark::State& state) {
5556
}
5657
}
5758

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

6277
BENCHMARK_MAIN();

tests/beman/any_view/detail/eager.cpp

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

66
for (const auto& product : products) {
77
if (product.quantity >= query.min_quantity) {
8-
results.push_back(product.name);
8+
results.emplace_back(product.name);
99
}
1010
}
1111

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

tests/beman/any_view/take.benchmark.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "detail/fused.hpp"
33
#include "detail/lazy.hpp"
44
#include "detail/products.hpp"
5+
#include "detail/reserved.hpp"
56

67
#include <benchmark/benchmark.h>
78

@@ -55,8 +56,22 @@ static void BM_take_lazy(benchmark::State& state) {
5556
}
5657
}
5758

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

6277
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)