@@ -33,28 +33,36 @@ namespace arangodb::containers {
33
33
struct Helpers {
34
34
// / @brief calculate capacity for the container for at least one more
35
35
// / 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.
38
38
template <typename T>
39
39
static std::size_t nextCapacity (T const & container, std::size_t initialCapacity) {
40
40
std::size_t capacity;
41
41
if (container.empty ()) {
42
42
// reserve some initial space
43
43
capacity = std::max (std::size_t (1 ), initialCapacity);
44
44
} else {
45
+ TRI_ASSERT (container.capacity () > 0 );
46
+ // minimum requirement is that we have room for at least one more element.
45
47
capacity = container.size () + 1 ;
46
- // allocate with power of 2 growth
47
48
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
+ }
49
57
}
50
58
}
51
59
TRI_ASSERT (capacity > container.size ());
52
60
return capacity;
53
61
}
54
62
55
63
// / @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.
58
66
template <typename T>
59
67
static void reserveSpace (T& container, std::size_t initialCapacity) {
60
68
std::size_t capacity = nextCapacity (container, initialCapacity);
0 commit comments