Commit 94fce358 authored by Philip Pronin's avatar Philip Pronin Committed by Sara Golemon

exponential capacity growth for io::TypedIOBuf

Summary:
Amortized cost of `TypedIOBuf::push()` is `O(N)` at this moment.
Unless we're using jemalloc where we can rely on `realloc()`.  In this
diff we switch to exponential growth otherwise.

Test Plan:
built unicorn/test with ASan (and thus without jemalloc), ran
end-to-end tests and verified we no longer spend minutes to build di4

@override-unit-failures

Reviewed By: tudorb@fb.com

FB internal diff: D1015233
parent 681e4677
......@@ -20,6 +20,8 @@
#include <algorithm>
#include <iterator>
#include <type_traits>
#include "folly/Malloc.h"
#include "folly/io/IOBuf.h"
namespace folly {
......@@ -164,8 +166,14 @@ class TypedIOBuf {
*/
template <class IT>
void push(IT begin, IT end) {
auto n = std::distance(begin, end);
reserve(headroom(), n);
uint32_t n = std::distance(begin, end);
if (usingJEMalloc()) {
// Rely on rallocm() and avoid exponential growth to limit
// amount of memory wasted.
reserve(headroom(), n);
} else if (tailroom() < n) {
reserve(headroom(), std::max(n, 3 + size() / 2));
}
std::copy(begin, end, writableTail());
append(n);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment