Skip to content
Open
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
9 changes: 9 additions & 0 deletions benchmark/btree_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,18 @@ using STLMultiMap = std::multimap<T, T>;
REGISTER_BENCHMARK_FUNCTIONS(container, std::int64_t); \
REGISTER_BENCHMARK_FUNCTIONS(container, std::string);

#define REGISTER_TRIVIAL_SHIFT_BENCHMARKS(container, type) \
STL_AND_BTREE_BENCHMARK(container, type, Insert); \
STL_AND_BTREE_BENCHMARK(container, type, Delete);

REGISTER_BENCHMARK_TYPES(Set);
REGISTER_BENCHMARK_TYPES(MultiSet);
REGISTER_BENCHMARK_TYPES(Map);
REGISTER_BENCHMARK_TYPES(MultiMap);

REGISTER_TRIVIAL_SHIFT_BENCHMARKS(Set, int);
REGISTER_TRIVIAL_SHIFT_BENCHMARKS(Set, std::pair<int, int>);
REGISTER_TRIVIAL_SHIFT_BENCHMARKS(Map, int);
REGISTER_TRIVIAL_SHIFT_BENCHMARKS(Map, std::pair<int, int>);

BENCHMARK_MAIN();
21 changes: 19 additions & 2 deletions include/platanus/internal/btree_base_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <memory>
#include <type_traits>

#include "btree_node_fwd.hpp"
#include "btree_util.hpp"
Expand Down Expand Up @@ -287,7 +290,14 @@ class btree_base_node {
assert(last + shift <= max_values_count());
auto begin = std::next(begin_values(), first);
auto end = std::next(begin_values(), last);
std::move_backward(begin, end, std::next(end, shift));
if constexpr (std::is_trivially_copyable_v<mutable_value_type>) {
auto* src = std::to_address(begin);
// std::memmove handles overlapping ranges, so right shifts (destination starts inside source)
// remain well-defined for trivially copyable values.
std::memmove(src + shift, src, static_cast<std::size_t>(last - first) * sizeof(*src));
} else {
std::move_backward(begin, end, std::next(end, shift));
}
}

// Shift values from first to last by shift toward left.
Expand All @@ -299,7 +309,14 @@ class btree_base_node {
assert(0 <= first - shift);
auto begin = std::next(begin_values(), first);
auto end = std::next(begin_values(), last);
std::move(begin, end, std::prev(begin, shift));
if constexpr (std::is_trivially_copyable_v<mutable_value_type>) {
auto* src = std::to_address(begin);
// std::memmove handles overlapping ranges, so left shifts (destination ends inside source)
// remain well-defined for trivially copyable values.
std::memmove(src - shift, src, static_cast<std::size_t>(last - first) * sizeof(*src));
} else {
std::move(begin, end, std::prev(begin, shift));
}
}

private:
Expand Down
Loading