Skip to content

Commit

Permalink
added benchmark to access the bitset data
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Feb 9, 2024
1 parent a98091f commit e0829af
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ else()
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g3 -ggdb")
endif()

# set(CMAKE_BUILD_TYPE "Release")

message(STATUS "Build configuration: ${CMAKE_BUILD_TYPE}")


Expand Down
103 changes: 85 additions & 18 deletions benchmarks/bitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,113 @@

namespace flatmemory::benchmarks
{
static void BM_BitsetBuilder(benchmark::State& state) {
static auto create_builders(
const size_t num_bitsets,
const size_t bitset_size) {
// Create num_bitset many bitsets
std::vector<Builder<Bitset<uint64_t>>> builders;
builders.reserve(num_bitsets);
for (size_t i = 0; i < num_bitsets; ++i) {
auto builder = Builder<Bitset<uint64_t>>();
builder.get_blocks().resize(bitset_size);
builders.push_back(std::move(builder));
}
return builders;
}

static void BM_BitsetBuilderCreate(benchmark::State& state) {
const size_t num_bitsets = state.range(0);
const size_t bitset_size = state.range(1);

for (auto _ : state) {
// Create num_bitset many bitsets
std::vector<Builder<Bitset<uint64_t>>> builders;
builders.reserve(num_bitsets);
for (size_t i = 0; i < num_bitsets; ++i) {
auto builder = Builder<Bitset<uint64_t>>();
builder.get_blocks().resize(bitset_size);
builders.push_back(std::move(builder));
auto builders = create_builders(num_bitsets, bitset_size);

benchmark::DoNotOptimize(builders);
}
}

static void BM_BitsetBuilderAccess(benchmark::State& state) {
const size_t num_bitsets = state.range(0);
const size_t bitset_size = state.range(1);

// Create num_bitset many bitsets
auto builders = create_builders(num_bitsets, bitset_size);

for (auto _ : state) {
// Access the data
for (auto& builder : builders) {
auto& default_bit_value = builder.get_default_bit_value();
benchmark::DoNotOptimize(default_bit_value);

auto& blocks = builder.get_blocks();
for (size_t i = 0; i < blocks.size(); ++i) {
auto block = blocks[i];
benchmark::DoNotOptimize(block);
}

}

benchmark::DoNotOptimize(builders);
}
}


static void BM_BitsetView(benchmark::State& state) {
static auto create_views(
const size_t num_bitsets,
const size_t bitset_size) {
// Create num_bitset many views
VariableSizedTypeVector<Bitset<uint64_t>> views;
auto builder = Builder<Bitset<uint64_t>>();
builder.get_blocks().resize(bitset_size);
builder.finish();
for (size_t i = 0; i < num_bitsets; ++i) {
views.push_back(builder);
}
return views;
}

static void BM_BitsetViewCreate(benchmark::State& state) {
const size_t num_bitsets = state.range(0);
const size_t bitset_size = state.range(1);

for (auto _ : state) {
// Create num_bitset many views
VariableSizedTypeVector<Bitset<uint64_t>> views;
auto builder = Builder<Bitset<uint64_t>>();
builder.get_blocks().resize(bitset_size);
builder.finish();
for (size_t i = 0; i < num_bitsets; ++i) {
views.push_back(builder);
}
auto views = create_views(num_bitsets, bitset_size);

benchmark::DoNotOptimize(views);
}
}

static void BM_BitsetViewAccess(benchmark::State& state) {
const size_t num_bitsets = state.range(0);
const size_t bitset_size = state.range(1);

auto views = create_views(num_bitsets, bitset_size);

for (auto _ : state) {
// Access the data
for (size_t i = 0; i < views.size(); ++i) {
auto view = views[i];
auto& default_bit_value = view.get_default_bit_value();
benchmark::DoNotOptimize(default_bit_value);

auto blocks = view.get_blocks();
size_t size = blocks.size();
for (size_t i = 0; i < size; ++i) {
auto block = blocks[i];
benchmark::DoNotOptimize(block);
}

}
}
}
}


BENCHMARK(flatmemory::benchmarks::BM_BitsetBuilder)->Args({100000, 10});
BENCHMARK(flatmemory::benchmarks::BM_BitsetView)->Args({100000, 10});
BENCHMARK(flatmemory::benchmarks::BM_BitsetBuilderCreate)->Args({100000, 10});
BENCHMARK(flatmemory::benchmarks::BM_BitsetBuilderAccess)->Args({100000, 10});
BENCHMARK(flatmemory::benchmarks::BM_BitsetViewCreate)->Args({100000, 10});
BENCHMARK(flatmemory::benchmarks::BM_BitsetViewAccess)->Args({100000, 10});


BENCHMARK_MAIN();
20 changes: 10 additions & 10 deletions include/flatmemory/details/containers/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ class VariableSizedTypeVector
}

[[nodiscard]] View<T> operator[](size_t pos) {
assert(pos <= static_cast<int>(get_size()));
assert(pos <= static_cast<int>(size()));
return m_data[pos];
}

[[nodiscard]] const View<T> operator[](size_t pos) const {
assert(pos <= static_cast<int>(get_size()));
assert(pos <= static_cast<int>(size()));
return m_data[pos];
}

[[nodiscard]] size_t get_size() const {
[[nodiscard]] size_t size() const {
return m_data.size();
}
};
Expand Down Expand Up @@ -91,7 +91,7 @@ class FixedSizedTypeVector
FixedSizedTypeVector& operator=(FixedSizedTypeVector&& other) = default;

void push_back(const Builder<T>& builder) {
m_data.push_back(View<T>(m_storage.write(builder.get_data(), builder.get_size()), builder.get_size()));
m_data.push_back(View<T>(m_storage.write(builder.get_data(), builder.size()), builder.size()));
}

[[nodiscard]] View<T> back() {
Expand All @@ -103,29 +103,29 @@ class FixedSizedTypeVector
}

[[nodiscard]] View<T> operator[](size_t pos) {
if (pos >= get_size()) {
if (pos >= size()) {
resize(pos);
}
return m_data[pos];
}

[[nodiscard]] const View<T> operator[](size_t pos) const {
if (pos >= get_size()) {
if (pos >= size()) {
resize(pos);
}
return m_data[pos];
}

void resize(size_t size) {
void resize(size_t count) {
const uint8_t* default_data = m_default_builder.get_data();
size_t default_size = m_default_builder.get_size();
while (get_size() <= size) {
size_t default_size = m_default_builder.size();
while (size() <= count) {
uint8_t* written_data = m_storage.write(default_data, default_size);
m_data.push_back(View<T>(written_data));
}
}

[[nodiscard]] size_t get_size() const {
[[nodiscard]] constexpr size_t size() const {
return m_data.size();
}
};
Expand Down
15 changes: 15 additions & 0 deletions include/flatmemory/details/types/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ namespace flatmemory
[[nodiscard]] const ByteStream& get_buffer_impl() const { return m_buffer; }

public:
/**
* empty
*/
[[nodiscard]] bool empty() const {
return m_data.empty();
}


/**
* size
*/
[[nodiscard]] size_t size() const {
return m_data.size();
}

/* operator[] stl */
[[nodiscard]] T_& operator[](size_t pos) {
assert(pos < m_data.size());
Expand Down

0 comments on commit e0829af

Please sign in to comment.