forked from OSchip/llvm-project
Make the assignment operator for SmallPtrSet much faster for normal cases.
llvm-svn: 38474
This commit is contained in:
parent
2af3063337
commit
e33356b18e
|
@ -177,42 +177,27 @@ SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) {
|
||||||
/// CopyFrom - implement operator= from a smallptrset that has the same pointer
|
/// CopyFrom - implement operator= from a smallptrset that has the same pointer
|
||||||
/// type, but may have a different small size.
|
/// type, but may have a different small size.
|
||||||
void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
|
||||||
// Allocate space if needed or clear the current elements out of the array.
|
if (isSmall() && RHS.isSmall())
|
||||||
if (CurArraySize < RHS.size()*2) {
|
assert(CurArraySize == RHS.CurArraySize &&
|
||||||
if (!isSmall())
|
"Cannot assign sets with different small sizes");
|
||||||
|
NumElements = RHS.NumElements;
|
||||||
|
NumTombstones = RHS.NumTombstones;
|
||||||
|
|
||||||
|
// If we're not currently small, and we don't have the same heap size,
|
||||||
|
// free our heap allocated storage
|
||||||
|
if (!isSmall() && CurArraySize != RHS.CurArraySize)
|
||||||
delete [] CurArray;
|
delete [] CurArray;
|
||||||
|
|
||||||
NumElements = NumTombstones = 0;
|
// If we're becoming small, prepare to insert into our stack space
|
||||||
|
if (RHS.isSmall())
|
||||||
|
CurArray = &SmallArray[0];
|
||||||
|
// Otherwise, allocate new heap space (unless we were the same size)
|
||||||
|
else if (CurArraySize != RHS.CurArraySize)
|
||||||
|
CurArray = new void*[RHS.CurArraySize+1];
|
||||||
|
|
||||||
// Get a power of two larger than twice the RHS size.
|
// Copy over the new array size
|
||||||
CurArraySize = 1 << Log2_32(RHS.size()*4);
|
CurArraySize = RHS.CurArraySize;
|
||||||
|
|
||||||
// Install the new array. Clear all the buckets to empty.
|
// Copy over the contents from the other set
|
||||||
CurArray = new void*[CurArraySize+1];
|
memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
|
||||||
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
|
||||||
|
|
||||||
// The end pointer, always valid, is set to a valid element to help the
|
|
||||||
// iterator.
|
|
||||||
CurArray[CurArraySize] = 0;
|
|
||||||
|
|
||||||
} else if (!empty()) {
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now that we know we have enough space, and that the current array is empty,
|
|
||||||
// copy over all the elements from the RHS.
|
|
||||||
for (void **BucketPtr = RHS.CurArray, **E = RHS.CurArray+RHS.CurArraySize;
|
|
||||||
BucketPtr != E; ++BucketPtr) {
|
|
||||||
// Copy over the element if it is valid.
|
|
||||||
void *Elt = *BucketPtr;
|
|
||||||
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker()) {
|
|
||||||
if (isSmall())
|
|
||||||
SmallArray[NumElements++] = Elt;
|
|
||||||
else
|
|
||||||
*const_cast<void**>(FindBucketFor(Elt)) = Elt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isSmall())
|
|
||||||
NumElements = RHS.NumElements;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue