Skip to content

Commit 5fa5287

Browse files
liyuying0000copybara-github
authored andcommitted
Fix InsertMiss hot swissmap benchmarks.
This includes: 1. The single `BM_InsertMiss_Hot` implementation is split into two benchmarks: `BM_InsertMiss()` and `BM_FindMiss()`, while `BM_InsertMiss()` is unchanged. 1. For `BM_FindMiss()`, we add a pre-calculated array for all non-existent keys to prepare the insertion. 1. After each full insertion pass, we remove all inserted keys to ensure the operation is true misses. PiperOrigin-RevId: 757814662 Change-Id: Ia42dd7c9a07c77ccf396cdcde4897ad88cd3d690
1 parent 4b10192 commit 5fa5287

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

fleetbench/swissmap/hot_swissmap_benchmark.cc

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ namespace fleetbench {
3737

3838
using ::benchmark::DoNotOptimize;
3939

40-
// Helper function used to implement two similar benchmarks that the given input
41-
// key is not present in the set.
42-
template <template <class...> class SetT, size_t kValueSizeT, bool kLookup>
43-
void FindMiss_Hot(benchmark::State& state) {
40+
// Measures the time it takes to `find` a non-existent element.
41+
//
42+
// assert(set.find(key) == set.end());
43+
template <template <class...> class SetT, size_t kValueSizeT>
44+
static void BM_SWISSMAP_FindMiss_Hot(benchmark::State& state) {
4445
using Set = SetT<Value<kValueSizeT>, Hash, Eq>;
4546

4647
// The larger this value, the less the results will depend on randomness and
@@ -51,11 +52,8 @@ void FindMiss_Hot(benchmark::State& state) {
5152
static constexpr size_t kOpsPerKey = 512;
5253

5354
auto& sc = SetsCache<Set>::GetInstance();
54-
// If kLookup is false, we need to create a copy of the cached sets vector
55-
// because the benchmark loop modifies it.
56-
std::conditional_t<kLookup, std::vector<Set>&, std::vector<Set>> sets =
57-
sc.GetGeneratedSets(state.range(0), kMinTotalKeyCount,
58-
static_cast<Density>(state.range(1)));
55+
std::vector<Set>& sets = sc.GetGeneratedSets(
56+
state.range(0), kMinTotalKeyCount, static_cast<Density>(state.range(1)));
5957
const size_t keys_per_set = kMinTotalKeyCount / sets.size();
6058

6159
while (state.KeepRunningBatch(sets.size() * keys_per_set * kOpsPerKey)) {
@@ -65,33 +63,56 @@ void FindMiss_Hot(benchmark::State& state) {
6563
for (size_t j = 0; j != kOpsPerKey; ++j) {
6664
DoNotOptimize(set);
6765
DoNotOptimize(key);
68-
if (kLookup) {
69-
auto res = set.find(key);
70-
DoNotOptimize(res);
71-
} else {
72-
auto res = set.insert(key);
73-
DoNotOptimize(res);
74-
}
66+
auto res = set.find(key);
67+
DoNotOptimize(res);
7568
}
7669
}
7770
}
7871
}
7972
}
8073

81-
// Measures the time it takes to `find` an existent element.
82-
//
83-
// assert(set.find(key) == set.end());
84-
template <template <class...> class SetT, size_t kValueSizeT>
85-
static void BM_SWISSMAP_FindMiss_Hot(benchmark::State& state) {
86-
FindMiss_Hot<SetT, kValueSizeT, true>(state);
87-
}
88-
89-
// Measures the time it takes to `insert` an existent element.
74+
// Measures the time it takes to `insert` a non-existent element.
9075
//
9176
// assert(set.insert(key).second);
9277
template <template <class...> class SetT, size_t kValueSizeT>
9378
static void BM_SWISSMAP_InsertMiss_Hot(benchmark::State& state) {
94-
FindMiss_Hot<SetT, kValueSizeT, false>(state);
79+
using Set = SetT<Value<kValueSizeT>, Hash, Eq>;
80+
81+
// The larger this value, the less the results will depend on randomness and
82+
// the longer the benchmark will run.
83+
static constexpr size_t kMinTotalKeyCount = 64 << 10;
84+
85+
auto& sc = SetsCache<Set>::GetInstance();
86+
// We need to create a copy of the cached sets vector
87+
// because the benchmark loop modifies it.
88+
std::vector<Set> sets = sc.GetGeneratedSets(
89+
state.range(0), kMinTotalKeyCount, static_cast<Density>(state.range(1)));
90+
const size_t keys_per_set = kMinTotalKeyCount / sets.size();
91+
92+
std::vector<uint32_t> keys;
93+
keys.resize(keys_per_set);
94+
for (uint32_t& key : keys) key = RandomNonexistent();
95+
96+
while (state.KeepRunningBatch(sets.size() * keys_per_set)) {
97+
for (auto& set : sets) {
98+
for (uint32_t key : keys) {
99+
DoNotOptimize(set);
100+
DoNotOptimize(key);
101+
auto res = set.insert(key);
102+
DoNotOptimize(res);
103+
}
104+
}
105+
106+
// Since we are using the same set for all iterations, we need to reset it
107+
// to avoid `InsertHit` behavior.
108+
state.PauseTiming();
109+
for (auto& set : sets) {
110+
for (uint32_t& key : keys) {
111+
set.erase(key);
112+
}
113+
}
114+
state.ResumeTiming();
115+
}
95116
}
96117

97118
// Helper function used to implement two similar benchmarks defined below that

0 commit comments

Comments
 (0)