Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2] Add runtime benchmarks for Fixed*Map data structures #143

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1155,20 +1155,6 @@ cc_test(
copts = ["-std=c++20"],
)

cc_test(
name = "fixed_map_perf_test",
srcs = ["test/fixed_map_perf_test.cpp"],
deps = [
":consteval_compare",
":fixed_index_based_storage",
":fixed_map",
":fixed_red_black_tree",
"@com_google_googletest//:gtest_main",
"@com_google_benchmark//:benchmark_main",
],
copts = ["-std=c++20"],
)

cc_test(
name = "fixed_list_test",
srcs = ["test/fixed_list_test.cpp"],
Expand Down Expand Up @@ -1612,6 +1598,57 @@ cc_test(
copts = ["-std=c++20"],
)

cc_library(
name = "benchmark_map_utils",
hdrs = ["test/benchmarks/map_utils.hpp"],
)

cc_test(
name = "map_copy_bench",
srcs = ["test/benchmarks/map_copy.cpp"],
deps = [
":consteval_compare",
":fixed_map",
":fixed_unordered_map",
":mock_testing_types",
":benchmark_map_utils",
"@com_google_benchmark//:benchmark_main",
],
copts = ["-std=c++20"],
tags = ["benchmark"],
)

cc_test(
name = "map_lookup_bench",
srcs = ["test/benchmarks/map_lookup.cpp"],
deps = [
":consteval_compare",
":fixed_map",
":fixed_unordered_map",
":mock_testing_types",
":benchmark_map_utils",
"@com_google_benchmark//:benchmark_main",
],
copts = ["-std=c++20"],
tags = ["benchmark"],
)

cc_test(
name = "map_clear_bench",
srcs = ["test/benchmarks/map_clear.cpp"],
deps = [
":consteval_compare",
":fixed_map",
":fixed_unordered_map",
":mock_testing_types",
":benchmark_map_utils",
"@com_google_benchmark//:benchmark_main",
],
copts = ["-std=c++20"],
tags = ["benchmark"],
)

test_suite(
name = "all_tests",
tags = ["-benchmark"],
)
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ if(BUILD_TESTS)
add_test_dependencies(fixed_list_test)
add_executable(fixed_map_test test/fixed_map_test.cpp)
add_test_dependencies(fixed_map_test)
add_executable(fixed_map_perf_test test/fixed_map_perf_test.cpp)
add_test_dependencies(fixed_map_perf_test)
add_executable(fixed_red_black_tree_test test/fixed_red_black_tree_test.cpp)
add_test_dependencies(fixed_red_black_tree_test)
add_executable(fixed_red_black_tree_view_test test/fixed_red_black_tree_view_test.cpp)
Expand Down Expand Up @@ -227,6 +225,13 @@ if(BUILD_TESTS)
add_test_dependencies(tuples_test)
add_executable(type_name_test test/type_name_test.cpp)
add_test_dependencies(type_name_test)

add_executable(map_copy_bench test/benchmarks/map_copy.cpp)
add_test_dependencies(map_copy_bench)
add_executable(map_lookup_bench test/benchmarks/map_lookup.cpp)
add_test_dependencies(map_lookup_bench)
add_executable(map_clear_bench test/benchmarks/map_clear.cpp)
add_test_dependencies(map_clear_bench)
endif()

option(FIXED_CONTAINERS_OPT_INSTALL "Enable install target" ${PROJECT_IS_TOP_LEVEL})
Expand Down
133 changes: 133 additions & 0 deletions test/benchmarks/map_clear.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "fixed_containers/fixed_map.hpp"
#include "fixed_containers/fixed_unordered_map.hpp"
#include "fixed_containers/memory.hpp"

#include <benchmark/benchmark.h>

#include <array>
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <unordered_map>

namespace fixed_containers
{

namespace
{
template <typename MapType>
void benchmark_map_copy(benchmark::State& state)
{
const int64_t nelem = state.range(0);
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();

using KeyType = typename MapType::key_type;
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}

const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2, instance);
benchmark::DoNotOptimize(instance2);
}
}

template <typename MapType>
void benchmark_map_copy_then_clear(benchmark::State& state)
{
using KeyType = typename MapType::key_type;
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();
const int64_t nelem = state.range(0);
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}

const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2, instance);
instance2.clear();
benchmark::DoNotOptimize(instance2);
}
}

template <typename MapType>
void benchmark_map_copy_then_reconstruct(benchmark::State& state)
{
using KeyType = typename MapType::key_type;
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();
const int64_t nelem = state.range(0);
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}

const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2, instance);
memory::destroy_and_construct_at_address_of(instance2);
benchmark::DoNotOptimize(instance2);
}
}

template <typename ArrType>
void benchmark_array_clear(benchmark::State& state)
{
const std::unique_ptr<ArrType> instance_ptr = std::make_unique<ArrType>();
ArrType& instance = *instance_ptr.get();

for (auto _ : state)
{
instance.fill(0);
benchmark::DoNotOptimize(instance);
}
}

constexpr std::size_t MAXIMUM_SIZE_LIMIT = 8 << 13;
constexpr std::size_t START = 16;
} // namespace

BENCHMARK(benchmark_map_copy<std::map<int, int>>)->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_clear<std::map<int, int>>)->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_reconstruct<std::map<int, int>>)
->Range(START, MAXIMUM_SIZE_LIMIT);

BENCHMARK(benchmark_map_copy<std::unordered_map<int, int>>)->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_clear<std::unordered_map<int, int>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_reconstruct<std::unordered_map<int, int>>)
->Range(START, MAXIMUM_SIZE_LIMIT);

BENCHMARK(benchmark_map_copy<FixedMap<int, int, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_clear<FixedMap<int, int, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_reconstruct<FixedMap<int, int, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);

BENCHMARK(benchmark_map_copy<FixedUnorderedMap<int, int, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_clear<FixedUnorderedMap<int, int, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_copy_then_reconstruct<FixedUnorderedMap<int, int, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);

// more-or-less the theoretical best performance we could possibly get for a full FixedUnorderedMap
// (just 0 out every bucket)
BENCHMARK(benchmark_array_clear<std::array<long, (MAXIMUM_SIZE_LIMIT * 130ULL) / 100>>);

} // namespace fixed_containers

BENCHMARK_MAIN();
135 changes: 135 additions & 0 deletions test/benchmarks/map_copy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "map_utils.hpp"

#include "../mock_testing_types.hpp"
#include "fixed_containers/fixed_unordered_map.hpp"
#include "fixed_containers/memory.hpp"

#include <benchmark/benchmark.h>

#include <cstddef>
#include <cstdint>
#include <memory>

namespace fixed_containers
{

namespace
{
template <typename MapType>
void benchmark_map_copy_fresh(benchmark::State& state)
{
const int64_t nelem = state.range(0);
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();

using KeyType = typename MapType::key_type;
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}

const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2, instance);
benchmark::DoNotOptimize(instance2);
}
}

template <typename MapType>
void benchmark_map_iterate_copy_fresh(benchmark::State& state)
{
const int64_t nelem = state.range(0);
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();

using KeyType = typename MapType::key_type;
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}
const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2);
for (auto elem : instance)
{
instance2.try_emplace(elem.first, elem.second);
}
benchmark::DoNotOptimize(instance2);
}
}

template <typename MapType>
void benchmark_map_copy_shuffled(benchmark::State& state)
{
const int64_t nelem = state.range(0);
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();
map_benchmarks::make_shuffled_map<MapType>(instance);

using KeyType = typename MapType::key_type;
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}

const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2, instance);
benchmark::DoNotOptimize(instance2);
}
}

template <typename MapType>
void benchmark_map_iterate_copy_shuffled(benchmark::State& state)
{
const int64_t nelem = state.range(0);
const std::unique_ptr<MapType> instance_ptr = std::make_unique<MapType>();
MapType& instance = *instance_ptr.get();
map_benchmarks::make_shuffled_map<MapType>(instance);

using KeyType = typename MapType::key_type;
for (int64_t i = 0; i < nelem; i++)
{
instance.try_emplace(static_cast<KeyType>(i));
}

const std::unique_ptr<MapType> instance_ptr2 = std::make_unique<MapType>();
MapType& instance2 = *instance_ptr2.get();
for (auto _ : state)
{
memory::destroy_and_construct_at_address_of(instance2);
for (auto elem : instance)
{
instance2.try_emplace(elem.first, elem.second);
}
benchmark::DoNotOptimize(instance2);
}
}

constexpr std::size_t MAXIMUM_SIZE_LIMIT = 8 << 14;
constexpr std::size_t START = 512;
} // namespace

BENCHMARK(benchmark_map_copy_fresh<
FixedUnorderedMap<int, MockNonTrivialCopyConstructible, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_iterate_copy_fresh<
FixedUnorderedMap<int, MockNonTrivialCopyConstructible, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);

BENCHMARK(benchmark_map_copy_shuffled<
FixedUnorderedMap<int, MockNonTrivialCopyConstructible, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);
BENCHMARK(benchmark_map_iterate_copy_shuffled<
FixedUnorderedMap<int, MockNonTrivialCopyConstructible, MAXIMUM_SIZE_LIMIT>>)
->Range(START, MAXIMUM_SIZE_LIMIT);

} // namespace fixed_containers

BENCHMARK_MAIN();
Loading
Loading