From: Giuseppe Ottaviano Date: Wed, 25 Nov 2015 21:39:14 +0000 (-0800) Subject: Skip reallocation in shrink_to_fit for empty fbvector X-Git-Tag: deprecate-dynamic-initializer~228 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ac06a3093239f271ec81255ed8ad6c48b85e8f16;p=folly.git Skip reallocation in shrink_to_fit for empty fbvector Reviewed By: philippv Differential Revision: D2696314 fb-gh-sync-id: 105de2d99bb9f5ac82310e37f9302022c103befc --- diff --git a/folly/FBVector.h b/folly/FBVector.h index 22d76034..c773a7f9 100644 --- a/folly/FBVector.h +++ b/folly/FBVector.h @@ -966,6 +966,12 @@ public: } void shrink_to_fit() noexcept { + if (empty()) { + // Just skip reallocation. + *this = fbvector(); + return; + } + auto const newCapacityBytes = folly::goodMallocSize(size() * sizeof(T)); auto const newCap = newCapacityBytes / sizeof(T); auto const oldCap = capacity();