8000 experimental: try a container growth factor of 1.5x (#14811) · arangodb/arangodb@187a32d · GitHub
[go: up one dir, main page]

Skip to content

Commit 187a32d

Browse files
jsteemanngoedderz
andauthored
experimental: try a container growth factor of 1.5x (#14811)
Co-authored-by: Tobias Gödderz <tobias@arangodb.com>
1 parent 7457392 commit 187a32d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/Containers/Helpers.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,36 @@ namespace arangodb::containers {
3333
struct Helpers {
3434
/// @brief calculate capacity for the container for at least one more
3535
/// element.
36-
/// if this would exceed the container's capacity, use a factor-of-2
37-
/// growth strategy to calculate the capacity.
36+
/// if this would exceed the container's capacity, use a growth factor of
37+
/// 1.5 to calculate the new capacity.
3838
template<typename T>
3939
static std::size_t nextCapacity(T const& container, std::size_t initialCapacity) {
4040
std::size_t capacity;
4141
if (container.empty()) {
4242
// reserve some initial space
4343
capacity = std::max(std::size_t(1), initialCapacity);
4444
} else {
45+
TRI_ASSERT(container.capacity() > 0);
46+
// minimum requirement is that we have room for at least one more element.
4547
capacity = container.size() + 1;
46-
// allocate with power of 2 growth
4748
if (capacity > container.capacity()) {
48-
capacity *= 2;
49+
// inspired by facebook/folly (https://github.com/facebook/folly/blob/master/folly/memory/Malloc.h):
50+
constexpr size_t jemallocMinInPlaceExpandable = 4096;
51+
if (container.capacity() < jemallocMinInPlaceExpandable / std::max(sizeof(typename T::value_type), alignof(typename T::value_type))) {
52+
capacity = container.capacity() * 2;
53+
} else {
54+
// grow with a growth factor of 1.5
55+
capacity = (container.size() * 3 + 1) / 2;
56+
}
4957
}
5058
}
5159
TRI_ASSERT(capacity > container.size());
5260
return capacity;
5361
}
5462

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

0 commit comments

Comments
 (0)
0