diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h index 61d6ae70e1d9..6b99b6113861 100644 --- a/llvm/include/llvm/ADT/DenseMap.h +++ b/llvm/include/llvm/ADT/DenseMap.h @@ -53,13 +53,13 @@ public: CopyFrom(other); } - explicit DenseMap(unsigned NumInitBuckets = 64) { + explicit DenseMap(unsigned NumInitBuckets = 0) { init(NumInitBuckets); } template DenseMap(const InputIt &I, const InputIt &E) { - init(64); + init(NextPowerOf2(std::distance(I, E))); insert(I, E); } @@ -98,7 +98,10 @@ public: unsigned size() const { return NumEntries; } /// Grow the densemap so that it has at least Size buckets. Does not shrink - void resize(size_t Size) { grow(Size); } + void resize(size_t Size) { + if (Size > NumBuckets) + grow(Size); + } void clear() { if (NumEntries == 0 && NumTombstones == 0) return; @@ -313,6 +316,11 @@ private: unsigned ProbeAmt = 1; BucketT *BucketsPtr = Buckets; + if (NumBuckets == 0) { + FoundBucket = 0; + return false; + } + // FoundTombstone - Keep track of whether we find a tombstone while probing. BucketT *FoundTombstone = 0; const KeyT EmptyKey = getEmptyKey(); @@ -354,6 +362,12 @@ private: NumEntries = 0; NumTombstones = 0; NumBuckets = InitBuckets; + + if (InitBuckets == 0) { + Buckets = 0; + return; + } + assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 && "# initial buckets must be a power of two!"); Buckets = static_cast(operator new(sizeof(BucketT)*InitBuckets)); @@ -367,6 +381,9 @@ private: unsigned OldNumBuckets = NumBuckets; BucketT *OldBuckets = Buckets; + if (NumBuckets < 64) + NumBuckets = 64; + // Double the number of buckets. while (NumBuckets < AtLeast) NumBuckets <<= 1;