Lazily allocate DenseMaps.

This makes lookup slightly more expensive but it's worth it, unused
DenseMaps are common in LLVM code apparently.

1% speedup on clang -O3 bzip2.c
4% speedup on clang -O3 oggenc.c (Release build of clang on i386/linux)

llvm-svn: 127088
This commit is contained in:
Benjamin Kramer 2011-03-05 16:43:41 +00:00
parent a9ff8576fb
commit 4d050b6f85
1 changed files with 20 additions and 3 deletions

View File

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