Skip to content

Supervised Buffer implementation #21911

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

Draft
wants to merge 13 commits into
base: devel
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
46 changes: 46 additions & 0 deletions lib/Basics/SupervisedBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "velocypack/Buffer.h"
#include "Basics/ResourceUsage.h"

namespace arangodb::velocypack {

class SupervisedBuffer : public arangodb::velocypack::Buffer<uint8_t> {
public:
SupervisedBuffer() = default;

explicit SupervisedBuffer(ResourceMonitor& monitor)
: _usageScope(std::make_unique<ResourceUsageScope>(monitor)) {}

uint8_t* steal() override noexcept {
uint8_t* ptr = velocypack::Buffer<uint8_t>::steal();
if (_usageScope) { // assume it exists without checking?
_usageScope->steal();
}
return ptr;
}

private:
void grow(ValueLength length) override {
auto currentCapacity = this->capacity();
velocypack::Buffer<uint8_t>::grow(length);
auto newCapacity = this->capacity();
if (_usageScope && newCapacity > currentCapacity) {
_usageScope->increase(newCapacity - currentCapacity);
}
}

void clear() override noexcept {
auto before = this->capacity();
Buffer<uint8_t>::clear();
auto after = this->capacity();
// if before > after, means that it has released usage from the heap
if (_usageScope && before > after) {
_usageScope->decrease(before - after);
}
}

std::unique_ptr<ResourceUsageScope> _usageScope;
};

} // namespace arangodb::velocypack
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ add_library(arango_basic_utils STATIC
Basics/NumberUtils.cpp
Basics/ReadWriteSpinLock.cpp
Basics/ResourceUsage.cpp
Basics/SupervisedBuffer.h
Basics/StringBufferAdvanced.cpp
Basics/Utf8Helper.cpp
Basics/VelocyPackHelper.cpp
Expand Down