Skip reallocation in shrink_to_fit for empty fbvector
authorGiuseppe Ottaviano <ott@fb.com>
Wed, 25 Nov 2015 21:39:14 +0000 (13:39 -0800)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Wed, 25 Nov 2015 22:20:21 +0000 (14:20 -0800)
Reviewed By: philippv

Differential Revision: D2696314

fb-gh-sync-id: 105de2d99bb9f5ac82310e37f9302022c103befc

folly/FBVector.h

index 22d76034bf084d5ce249fe50665e286c13f7777a..c773a7f963dc023b01942eba114345d1449002cf 100644 (file)
@@ -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();