llvm.org GIT mirror llvm / 2a9a2db
After some discussion with djg, teach SmallVector to grow from a zero 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 John McCall 10 years ago
2 changed file(s) with 8 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
205205 void SmallVectorTemplateBase::grow(size_t MinSize) {
206206 size_t CurCapacity = this->capacity();
207207 size_t CurSize = this->size();
208 size_t NewCapacity = 2*CurCapacity;
208 size_t NewCapacity = 2*CurCapacity + 1; // Always grow, even from zero.
209209 if (NewCapacity < MinSize)
210210 NewCapacity = MinSize;
211211 T *NewElts = static_cast(malloc(NewCapacity*sizeof(T)));
711711 /// members are required.
712712 template
713713 class SmallVector : public SmallVectorImpl {
714 // SmallVector doesn't like growing from zero capacity. As a
715 // temporary workaround, avoid changing the growth algorithm by
716 // forcing capacity to be at least 1 in the constructors.
717
718714 public:
719 SmallVector() : SmallVectorImpl(0) {
720 this->reserve(1); // workaround
721 }
715 SmallVector() : SmallVectorImpl(0) {}
722716
723717 explicit SmallVector(unsigned Size, const T &Value = T())
724718 : SmallVectorImpl(0) {
725 this->reserve(Size ? Size : 1); // workaround
719 this->reserve(Size);
726720 while (Size--)
727721 this->push_back(Value);
728722 }
729723
730724 template
731725 SmallVector(ItTy S, ItTy E) : SmallVectorImpl(0) {
732 if (S == E) this->reserve(1); // workaround
733726 this->append(S, E);
734727 }
735728
736729 SmallVector(const SmallVector &RHS) : SmallVectorImpl(0) {
737 if (!RHS.empty())
738 SmallVectorImpl::operator=(RHS);
739 else
740 this->reserve(1); // workaround
741 }
742
743 const SmallVector &operator=(const SmallVector &RHS) {
744730 SmallVectorImpl::operator=(RHS);
745 return *this;
731 }
732
733 SmallVector &operator=(const SmallVectorImpl &RHS) {
734 return SmallVectorImpl::operator=(RHS);
746735 }
747736
748737 };
1717 /// on POD-like datatypes and is out of line to reduce code duplication.
1818 void SmallVectorBase::grow_pod(size_t MinSizeInBytes, size_t TSize) {
1919 size_t CurSizeBytes = size_in_bytes();
20 size_t NewCapacityInBytes = 2 * capacity_in_bytes();
20 size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
2121 if (NewCapacityInBytes < MinSizeInBytes)
2222 NewCapacityInBytes = MinSizeInBytes;
2323