From 94fce358e890eb92072c8083cf15b4852feedacf Mon Sep 17 00:00:00 2001 From: Philip Pronin Date: Wed, 16 Oct 2013 20:13:33 -0700 Subject: [PATCH] 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 --- folly/io/TypedIOBuf.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/folly/io/TypedIOBuf.h b/folly/io/TypedIOBuf.h index 3e8d2297..90e17126 100644 --- a/folly/io/TypedIOBuf.h +++ b/folly/io/TypedIOBuf.h @@ -20,6 +20,8 @@ #include #include #include + +#include "folly/Malloc.h" #include "folly/io/IOBuf.h" namespace folly { @@ -164,8 +166,14 @@ class TypedIOBuf { */ template 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); } -- 2.34.1