8000 experimental: try a container growth factor of 1.5x by jsteemann · Pull Request #14811 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

experimental: try a container growth factor of 1.5x #14811

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

Merged
merged 7 commits into from
Oct 25, 2021
Merged
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
20 changes: 14 additions & 6 deletions lib/Containers/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,36 @@ namespace arangodb::containers {
struct Helpers {
/// @brief calculate capacity for the container for at least one more
/// element.
/// if this would exceed the container's capacity, use a factor-of-2
/// growth strategy to calculate the capacity.
/// if this would exceed the container's capacity, use a growth factor of
/// 1.5 to calculate the new capacity.
template<typename T>
static std::size_t nextCapacity(T const& container, std::size_t initialCapacity) {
std::size_t capacity;
if (container.empty()) {
// reserve some initial space
capacity = std::max(std::size_t(1), initialCapacity);
} else {
TRI_ASSERT(container.capacity() > 0);
// minimum requirement is that we have room for at least one more element.
capacity = container.size() + 1;
// allocate with power of 2 growth
if (capacity > container.capacity()) {
capacity *= 2;
// inspired by facebook/folly (https://github.com/facebook/folly/blob/master/folly/memory/Malloc.h):
constexpr size_t jemallocMinInPlaceExpandable = 4096;
if (container.capacity() < jemallocMinInPlaceExpandable / std::max(sizeof(typename T::value_type), alignof(typename T::value_type))) {
capacity = container.capacity() * 2;
} else {
// grow with a growth factor of 1.5
capacity = (container.size() * 3 + 1) / 2;
}
}
}
TRI_ASSERT(capacity > container.size());
return capacity;
}

/// @brief reserve space for at least one more element in the container.
/// if this would exceed the container's capacity, use a factor-of-2
/// growth strategy to grow the container's memory.
/// if this would exceed the container's capacity, use a growth factor of
/// 1.5 to grow the container's memory.
template<typename T>
static void reserveSpace(T& container, std::size_t initialCapacity) {
std::size_t capacity = nextCapacity(container, initialCapacity);
Expand Down
0