Files
Bigfoot/Bigfoot/Benchmarks/Utils/Containers/SlotMap.cpp
T
rboullard b7f4677c68
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 44s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 29s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 46s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 30s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 1m21s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 1m3s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m25s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 1m8s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 1m9s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m34s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 1m13s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 37s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 28s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 57s
Bigfoot / Sonarqube (push) Successful in 6m0s
sonarqube ERROR
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 49s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 30s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 52s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 36s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 1m23s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 44s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 59s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 51s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m5s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 56s
Bigfoot / Clang Format Checks (push) Successful in 13s
Bigfoot / Sonarqube (pull_request) Failing after 5m14s
BigFile/Sasquatch (#1)
Implementing an asset management system

BigFile is a sqlite database, for toolmode
Sasquatch is a custom binary file, for gamemode

Assets are built from flatbuffers
References allow to get an Asset, from an UUID, from anywhere in the code
HardReference refcount the asset and allow automatic removal

Reviewed-on: #1
Co-authored-by: Romain BOULLARD <romain.boullard@protonmail.com>
2026-09-01 16:32:01 +00:00

260 lines
7.3 KiB
C++

#include <Utils/Containers/SlotMap.hpp>
#include <ankerl/unordered_dense.h>
#include <benchmark/benchmark.h>
#include <effolkronium/random.hpp>
#include <cstdint>
#include <type_traits>
#include <utility>
#include <vector>
namespace Bigfoot
{
using Random = effolkronium::random_static;
struct MyComplexStruct
{
std::uint32_t m_actualValueForBench = 0; // field summed by the Iterate benchmark
float m_position[3] = {0.0f, 0.0f, 0.0f};
float m_rotation[4] = {0.0f, 0.0f, 0.0f, 1.0f};
float m_scale[3] = {1.0f, 1.0f, 1.0f};
std::uint64_t m_entityId = 0;
std::uint32_t m_parentSlot = 0;
std::uint32_t m_flags = 0;
MyComplexStruct() = default;
explicit MyComplexStruct(const std::uint32_t p_value):
m_actualValueForBench(p_value),
m_entityId(p_value)
{
}
};
static_assert(sizeof(MyComplexStruct) == 64, "Payload should be exactly one cache line.");
static_assert(std::is_trivially_copyable_v<MyComplexStruct>,
"Container moves should be a plain memcpy, not a heap-touching move.");
template<class SLOTMAP>
class SlotMapAdaptor
{
public:
using Key = SLOTMAP::SlotMapKey;
Key Add(MyComplexStruct&& p_value)
{
return m_container.Insert(std::move(p_value));
}
const MyComplexStruct* Find(const Key p_key) const
{
return m_container.Get(p_key);
}
void Remove(const Key p_key)
{
m_container.Remove(p_key);
}
void Clear()
{
m_container.Reset();
}
template<class FUNC>
void ForEachValue(FUNC&& p_func) const
{
for (const MyComplexStruct& value: m_container)
{
p_func(value);
}
}
private:
SLOTMAP m_container;
};
template<class MAP>
class HashMapHarnessAdaptor
{
public:
using Key = typename MAP::key_type;
Key Add(MyComplexStruct&& p_value)
{
const Key key = m_nextKey++;
m_container.emplace(key, std::move(p_value));
return key;
}
const MyComplexStruct* Find(const Key p_key) const
{
const auto it = m_container.find(p_key);
return it != m_container.end() ? &it->second : nullptr;
}
void Remove(const Key p_key)
{
m_container.erase(p_key);
}
void Clear()
{
m_container.clear();
m_nextKey = 0;
}
template<class FUNC>
void ForEachValue(FUNC&& p_func) const
{
for (const auto& keyValue: m_container)
{
p_func(keyValue.second);
}
}
private:
MAP m_container;
Key m_nextKey = 0;
};
template<class ADAPTOR>
void Insert(benchmark::State& state)
{
const auto count = static_cast<std::uint32_t>(state.range(0));
for (auto _: state)
{
ADAPTOR adaptor;
for (std::uint32_t i = 0; i < count; ++i)
{
benchmark::DoNotOptimize(adaptor.Add(MyComplexStruct {i}));
}
benchmark::DoNotOptimize(adaptor);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * count);
state.SetBytesProcessed(static_cast<std::int64_t>(state.iterations() * count * sizeof(MyComplexStruct)));
}
template<class ADAPTOR>
void Remove(benchmark::State& state)
{
const auto count = static_cast<std::uint32_t>(state.range(0));
ADAPTOR adaptor;
std::vector<typename ADAPTOR::Key> keys;
keys.reserve(count);
Random::seed(0x5EEDU);
for (auto _: state)
{
state.PauseTiming();
adaptor.Clear();
keys.clear();
for (std::uint32_t i = 0; i < count; ++i)
{
keys.push_back(adaptor.Add(MyComplexStruct {i}));
}
Random::shuffle(keys);
state.ResumeTiming();
for (const typename ADAPTOR::Key key: keys)
{
adaptor.Remove(key);
}
benchmark::DoNotOptimize(adaptor);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * count);
state.SetBytesProcessed(static_cast<std::int64_t>(state.iterations() * count * sizeof(MyComplexStruct)));
}
template<class ADAPTOR>
void Get(benchmark::State& state)
{
const auto count = static_cast<std::uint32_t>(state.range(0));
ADAPTOR adaptor;
std::vector<typename ADAPTOR::Key> keys;
keys.reserve(count);
for (std::uint32_t i = 0; i < count; ++i)
{
keys.push_back(adaptor.Add(MyComplexStruct {i}));
}
Random::seed(0x5EEDU);
Random::shuffle(keys);
for (auto _: state)
{
std::uint64_t sum = 0;
for (const typename ADAPTOR::Key key: keys)
{
if (const MyComplexStruct* const value = adaptor.Find(key))
{
sum += value->m_actualValueForBench;
}
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
state.SetBytesProcessed(static_cast<std::int64_t>(state.iterations() * count * sizeof(MyComplexStruct)));
}
template<class ADAPTOR>
void Iterate(benchmark::State& state)
{
const auto count = static_cast<std::uint32_t>(state.range(0));
ADAPTOR adaptor;
for (std::uint32_t i = 0; i < count; ++i)
{
adaptor.Add(MyComplexStruct {i});
}
for (auto _: state)
{
std::uint64_t sum = 0;
adaptor.ForEachValue(
[&sum](const MyComplexStruct& value)
{
sum += value.m_actualValueForBench;
});
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
state.SetBytesProcessed(static_cast<std::int64_t>(state.iterations() * count * sizeof(MyComplexStruct)));
}
#define BIGFOOT_REGISTER_BENCHMARKS(ADAPTOR, NAME) \
BENCHMARK_TEMPLATE(Insert, ADAPTOR)->Name(NAME "/Insert")->Arg(512)->Arg(8192)->Arg(262'144)->Arg(4'194'304); \
BENCHMARK_TEMPLATE(Remove, ADAPTOR)->Name(NAME "/Remove")->Arg(512)->Arg(8192)->Arg(262'144)->Arg(4'194'304); \
BENCHMARK_TEMPLATE(Get, ADAPTOR)->Name(NAME "/Get")->Arg(512)->Arg(8192)->Arg(262'144)->Arg(4'194'304); \
BENCHMARK_TEMPLATE(Iterate, ADAPTOR)->Name(NAME "/Iterate")->Arg(512)->Arg(8192)->Arg(262'144)->Arg(4'194'304);
using DenseSlotMapHarness = SlotMapAdaptor<DenseSlotMap<MyComplexStruct>>;
using StableSlotMapHarness = SlotMapAdaptor<StableSlotMap<MyComplexStruct>>;
using UnorderedDenseMapHarness = HashMapHarnessAdaptor<ankerl::unordered_dense::map<std::uint64_t, MyComplexStruct>>;
using UnorderedDenseSegementedMapHarness =
HashMapHarnessAdaptor<ankerl::unordered_dense::segmented_map<std::uint64_t, MyComplexStruct>>;
using UnorderedMapHarness = HashMapHarnessAdaptor<std::unordered_map<std::uint64_t, MyComplexStruct>>;
using MapHarness = HashMapHarnessAdaptor<std::map<std::uint64_t, MyComplexStruct>>;
BIGFOOT_REGISTER_BENCHMARKS(DenseSlotMapHarness, "Bigfoot::DenseSlotMap");
BIGFOOT_REGISTER_BENCHMARKS(StableSlotMapHarness, "Bigfoot::StableSlotMap");
BIGFOOT_REGISTER_BENCHMARKS(UnorderedDenseMapHarness, "ankerl::unordered_dense::map");
BIGFOOT_REGISTER_BENCHMARKS(UnorderedDenseSegementedMapHarness, "ankerl::unordered_dense::segmented_map");
BIGFOOT_REGISTER_BENCHMARKS(UnorderedMapHarness, "std::unordered_map");
BIGFOOT_REGISTER_BENCHMARKS(MapHarness, "std::map");
#undef BIGFOOT_REGISTER_BENCHMARKS
#undef BIGFOOT_CACHE_TIER_ARGS
} // namespace Bigfoot