After some discussion with djg, teach SmallVector to grow from a zero
authorJohn McCall <rjmccall@apple.com>
Thu, 2 Sep 2010 21:55:03 +0000 (21:55 +0000)
committerJohn McCall <rjmccall@apple.com>
Thu, 2 Sep 2010 21:55:03 +0000 (21:55 +0000)
capacity and remove the workaround in SmallVector<T,0>.  There are some
theoretical benefits to a N->2N+1 growth policy anyway.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112870 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/SmallVector.h
lib/Support/SmallVector.cpp

index a76e9c77f1642bd1f256d2f0322d3f27a463b3b7..1d6181a95da3640f34b58ab3fd5f90d7cce857a8 100644 (file)
@@ -206,7 +206,7 @@ template <typename T, bool isPodLike>
 void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
   size_t CurCapacity = this->capacity();
   size_t CurSize = this->size();
-  size_t NewCapacity = 2*CurCapacity;
+  size_t NewCapacity = 2*CurCapacity + 1; // Always grow, even from zero.
   if (NewCapacity < MinSize)
     NewCapacity = MinSize;
   T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
@@ -712,38 +712,27 @@ public:
 /// members are required.
 template <typename T>
 class SmallVector<T,0> : public SmallVectorImpl<T> {
-  // SmallVector doesn't like growing from zero capacity.  As a
-  // temporary workaround, avoid changing the growth algorithm by
-  // forcing capacity to be at least 1 in the constructors.
-
 public:
-  SmallVector() : SmallVectorImpl<T>(0) {
-    this->reserve(1); // workaround
-  }
+  SmallVector() : SmallVectorImpl<T>(0) {}
 
   explicit SmallVector(unsigned Size, const T &Value = T())
     : SmallVectorImpl<T>(0) {
-    this->reserve(Size ? Size : 1); // workaround
+    this->reserve(Size);
     while (Size--)
       this->push_back(Value);
   }
 
   template<typename ItTy>
   SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(0) {
-    if (S == E) this->reserve(1); // workaround
     this->append(S, E);
   }
 
   SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(0) {
-    if (!RHS.empty())
-      SmallVectorImpl<T>::operator=(RHS);
-    else
-      this->reserve(1); // workaround
+    SmallVectorImpl<T>::operator=(RHS);
   }
 
-  const SmallVector &operator=(const SmallVector &RHS) {
-    SmallVectorImpl<T>::operator=(RHS);
-    return *this;
+  SmallVector &operator=(const SmallVectorImpl<T> &RHS) {
+    return SmallVectorImpl<T>::operator=(RHS);
   }
 
 };
index 2e17af864155a98cccb34166d4ce627d89db4d97..a89f14957635e59c120cb1e6155f27db65c6341a 100644 (file)
@@ -18,7 +18,7 @@ using namespace llvm;
 /// on POD-like datatypes and is out of line to reduce code duplication.
 void SmallVectorBase::grow_pod(size_t MinSizeInBytes, size_t TSize) {
   size_t CurSizeBytes = size_in_bytes();
-  size_t NewCapacityInBytes = 2 * capacity_in_bytes();
+  size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
   if (NewCapacityInBytes < MinSizeInBytes)
     NewCapacityInBytes = MinSizeInBytes;