llvm.org GIT mirror llvm / 42e4bdf
When clearing a SmallPtrSet, if the set had a huge capacity, but the contents of the set were small, deallocate and shrink the set. This avoids having us to memset as much data, significantly speeding up some pathological cases. For example, this speeds up the verifier from 0.3899s to 0.0763 (5.1x) on the testcase from PR1432 in a release build. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40837 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
3 changed file(s) with 28 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
9090 unsigned size() const { return NumEntries; }
9191
9292 void clear() {
93 // If the capacity of the array is huge, and the # elements used is small,
94 // shrink the array.
9395 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
9496 shrink_and_clear();
9597 return;
7979 }
8080
8181 void clear() {
82 // If the capacity of the array is huge, and the # elements used is small,
83 // shrink the array.
84 if (!isSmall() && NumElements*4 < CurArraySize && CurArraySize > 32)
85 return shrink_and_clear();
86
8287 // Fill the array with empty markers.
8388 memset(CurArray, -1, CurArraySize*sizeof(void*));
8489 NumElements = 0;
102107 bool count(void * const Ptr) const {
103108 if (isSmall()) {
104109 // Linear search for the item.
105 for (const void *const *APtr = SmallArray, *const *E = SmallArray+NumElements;
106 APtr != E; ++APtr)
110 for (const void *const *APtr = SmallArray,
111 *const *E = SmallArray+NumElements; APtr != E; ++APtr)
107112 if (*APtr == Ptr)
108113 return true;
109114 return false;
120125 return ((uintptr_t)Ptr >> 4) & (CurArraySize-1);
121126 }
122127 const void * const *FindBucketFor(const void *Ptr) const;
128 void shrink_and_clear();
123129
124130 /// Grow - Allocate a larger backing store for the buckets and move it over.
125131 void Grow();
1616 #include
1717
1818 using namespace llvm;
19
20 void SmallPtrSetImpl::shrink_and_clear() {
21 assert(!isSmall() && "Can't shrink a small set!");
22 free(CurArray);
23
24 // Reduce the number of buckets.
25 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
26 NumElements = NumTombstones = 0;
27
28 // Install the new array. Clear all the buckets to empty.
29 CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
30 assert(CurArray && "Failed to allocate memory?");
31 memset(CurArray, -1, CurArraySize*sizeof(void*));
32
33 // The end pointer, always valid, is set to a valid element to help the
34 // iterator.
35 CurArray[CurArraySize] = 0;
36 }
1937
2038 bool SmallPtrSetImpl::insert(const void * Ptr) {
2139 if (isSmall()) {