Skip reallocation in shrink_to_fit for empty fbvector
[folly.git] / 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();