Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignition committed Apr 30, 2024
1 parent 37625c3 commit 990cd94
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions libs/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ cd ..
absl_ref="20230125.3"
repo_clone_try_double "${primary_urls[absl]}" "${secondary_urls[absl]}" "absl" "$absl_ref"

# jemalloc ea6b3e973b477b8061e0076bb257dbd7f3faa756
JEMALLOC_COMMIT_VERSION="5.2.1"
# jemalloc 54eaed1d8b56b1aa528be3bdd1877e59c56fa90c
JEMALLOC_COMMIT_VERSION="5.3.0"
repo_clone_try_double "${primary_urls[jemalloc]}" "${secondary_urls[jemalloc]}" "jemalloc" "$JEMALLOC_COMMIT_VERSION"

# this is hack for cmake in libs to set path, and for FindJemalloc to use Jemalloc_INCLUDE_DIR
Expand Down
4 changes: 4 additions & 0 deletions src/memory/global_memory_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#if USE_JEMALLOC
#include "jemalloc/jemalloc.h"
#else
#include <malloc.h>
#endif

namespace memgraph::memory {
Expand Down Expand Up @@ -276,6 +278,8 @@ void UnsetHooks() {
void PurgeUnusedMemory() {
#if USE_JEMALLOC
mallctl("arena." STRINGIFY(MALLCTL_ARENAS_ALL) ".purge", nullptr, nullptr, nullptr, 0);
#else
malloc_trim(0);
#endif
}

Expand Down
21 changes: 9 additions & 12 deletions src/storage/v2/delta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct DeltaMemoryResource : std::pmr::memory_resource {
auto earlest_slab_position = alignSize(sizeof(header), alignment);
auto max_slab_capacity = PAGE_SIZE - earlest_slab_position;
if (max_slab_capacity < bytes) [[unlikely]] {
// dedicated allocation
auto required_bytes = bytes + earlest_slab_position;
auto *newmem = reinterpret_cast<header *>(aligned_alloc(alignment, required_bytes));
if (!newmem) throw std::bad_alloc{};
Expand Down Expand Up @@ -272,20 +273,16 @@ struct Delta {
command_id(0),
old_disk_key{.value = opt_str{old_disk_key, res}} {}

Delta(DeleteObjectTag /*tag*/, std::atomic<uint64_t> *timestamp, uint64_t command_id,
std::pmr::memory_resource * /*res*/)
Delta(DeleteObjectTag /*tag*/, std::atomic<uint64_t> *timestamp, uint64_t command_id)
: timestamp(timestamp), command_id(command_id), action(Action::DELETE_OBJECT) {}

Delta(RecreateObjectTag /*tag*/, std::atomic<uint64_t> *timestamp, uint64_t command_id,
std::pmr::memory_resource * /*res*/)
Delta(RecreateObjectTag /*tag*/, std::atomic<uint64_t> *timestamp, uint64_t command_id)
: timestamp(timestamp), command_id(command_id), action(Action::RECREATE_OBJECT) {}

Delta(AddLabelTag /*tag*/, LabelId label, std::atomic<uint64_t> *timestamp, uint64_t command_id,
std::pmr::memory_resource * /*res*/)
Delta(AddLabelTag /*tag*/, LabelId label, std::atomic<uint64_t> *timestamp, uint64_t command_id)
: timestamp(timestamp), command_id(command_id), label{.action = Action::ADD_LABEL, .value = label} {}

Delta(RemoveLabelTag /*tag*/, LabelId label, std::atomic<uint64_t> *timestamp, uint64_t command_id,
std::pmr::memory_resource * /*res*/)
Delta(RemoveLabelTag /*tag*/, LabelId label, std::atomic<uint64_t> *timestamp, uint64_t command_id)
: timestamp(timestamp), command_id(command_id), label{.action = Action::REMOVE_LABEL, .value = label} {}

Delta(SetPropertyTag /*tag*/, PropertyId key, PropertyValue value, std::atomic<uint64_t> *timestamp,
Expand All @@ -299,25 +296,25 @@ struct Delta {
} {}

Delta(AddInEdgeTag /*tag*/, EdgeTypeId edge_type, Vertex *vertex, EdgeRef edge, std::atomic<uint64_t> *timestamp,
uint64_t command_id, std::pmr::memory_resource * /*res*/)
uint64_t command_id)
: timestamp(timestamp),
command_id(command_id),
vertex_edge{.action = Action::ADD_IN_EDGE, .edge_type = edge_type, vertex, edge} {}

Delta(AddOutEdgeTag /*tag*/, EdgeTypeId edge_type, Vertex *vertex, EdgeRef edge, std::atomic<uint64_t> *timestamp,
uint64_t command_id, std::pmr::memory_resource * /*res*/)
uint64_t command_id)
: timestamp(timestamp),
command_id(command_id),
vertex_edge{.action = Action::ADD_OUT_EDGE, .edge_type = edge_type, vertex, edge} {}

Delta(RemoveInEdgeTag /*tag*/, EdgeTypeId edge_type, Vertex *vertex, EdgeRef edge, std::atomic<uint64_t> *timestamp,
uint64_t command_id, std::pmr::memory_resource * /*res*/)
uint64_t command_id)
: timestamp(timestamp),
command_id(command_id),
vertex_edge{.action = Action::REMOVE_IN_EDGE, .edge_type = edge_type, vertex, edge} {}

Delta(RemoveOutEdgeTag /*tag*/, EdgeTypeId edge_type, Vertex *vertex, EdgeRef edge, std::atomic<uint64_t> *timestamp,
uint64_t command_id, std::pmr::memory_resource * /*res*/)
uint64_t command_id)
: timestamp(timestamp),
command_id(command_id),
vertex_edge{.action = Action::REMOVE_OUT_EDGE, .edge_type = edge_type, vertex, edge} {}
Expand Down
29 changes: 17 additions & 12 deletions src/storage/v2/delta_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ using PageAlignedList = std::forward_list<T, PageAlignedAllocator<T>>;
template <std::size_t N>
using delta_slab = memgraph::utils::static_vector<Delta, N>;

// This is for how many deltas can exist in delta_slab no larger than PAGE_SIZE
// This is for how many deltas can exist in delta_slab no larger than PAGE_SIZE * 4
// assumption `sizeof(void *)` if for the node pointer inside forward_list's node
constexpr auto kMaxDeltas = (PAGE_SIZE - sizeof(void *) - delta_slab<0>::header_size()) / sizeof(Delta);
// We can do 4 pages, because if unused, those pages would
// not contribute to RSS (static_vector ensures buffer in uninitialised)
constexpr auto kMaxDeltas = (PAGE_SIZE * 4 - sizeof(void *) - delta_slab<0>::header_size()) / sizeof(Delta);

// Flattern iterators used here becasue we can't use
// `std::views::join` becasue stack-use-after-scope
Expand Down Expand Up @@ -262,13 +264,20 @@ struct delta_container {
template <typename... Args>
auto emplace(Args &&...args) -> Delta & {
auto do_emplace = [&]() -> Delta & {
if (!memory_resource_) [[unlikely]] {
// TODO: only do for actions that maybe require allocation
memory_resource_ = std::make_unique<DeltaMemoryResource>();
if constexpr (std::is_constructible_v<Delta, Args...>) {
// no need for memory_resource
auto &delta = deltas_.front().emplace(std::forward<Args>(args)...);
++size_;
return delta;
} else {
// requires memory_resource
if (!memory_resource_) [[unlikely]] {
memory_resource_ = std::make_unique<DeltaMemoryResource>();
}
auto &delta = deltas_.front().emplace(std::forward<Args>(args)..., memory_resource_.get());
++size_;
return delta;
}
auto &delta = deltas_.front().emplace(std::forward<Args>(args)..., memory_resource_.get());
++size_;
return delta;
};

if (deltas_.empty() || deltas_.front().is_full()) [[unlikely]] {
Expand All @@ -290,10 +299,6 @@ struct delta_container {
size_ = 0;
}

void unlink() {
// Do I move GC code here?
}

bool empty() const { return deltas_.empty(); }

auto size() const -> std::size_t { return size_; }
Expand Down
5 changes: 3 additions & 2 deletions src/utils/static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ struct static_vector {
static_vector(static_vector const &other) requires(!std::is_copy_constructible_v<T>) = delete;

static_vector &operator=(static_vector const &other) requires(std::is_copy_assignable_v<T>) {
if (this == std::addressof(other)) return *this;
auto const b = begin();
auto const ob = other.begin();
if (other.size_ < size_) {
Expand Down Expand Up @@ -151,15 +152,15 @@ struct static_vector {
assert(!is_full());
auto *new_item = &*(begin() + size_);
std::construct_at(new_item, std::forward<Args>(args)...);
++size_; // increment after sucessful construction
++size_; // increment after successful construction
return *std::launder(new_item);
}

static constexpr auto header_size() -> std::size_t { return sizeof(size_); }

private:
std::size_t size_ = 0;
// deliberatly an uninitialised buffer
// deliberately an uninitialised buffer
alignas(alignof(T)) std::byte buffer_[N * sizeof(T)];
};

Expand Down

0 comments on commit 990cd94

Please sign in to comment.