[SmallVector] Optimize move assignment operator for N==0 case

Due to the SmallVector hierarchy, N==0 cannot be leveraged by functions defined
in base classes. This patch special cases N==0 for SmallVector to save code size
and be slightly more efficient.

In a Release build of x86 only clang, .text is -3.34KiB smaller. In lld .text is
7.17KiB smaller.

Reviewed By: lichray

Differential Revision: https://reviews.llvm.org/D117976
This commit is contained in:
Fangrui Song 2022-01-29 11:03:42 -08:00
parent 4a69c8ef47
commit 333f501930
1 changed files with 25 additions and 7 deletions

View File

@ -568,6 +568,16 @@ protected:
explicit SmallVectorImpl(unsigned N)
: SmallVectorTemplateBase<T>(N) {}
void assignRemote(SmallVectorImpl &&RHS) {
this->destroy_range(this->begin(), this->end());
if (!this->isSmall())
free(this->begin());
this->BeginX = RHS.BeginX;
this->Size = RHS.Size;
this->Capacity = RHS.Capacity;
RHS.resetToSmall();
}
public:
SmallVectorImpl(const SmallVectorImpl &) = delete;
@ -1032,12 +1042,7 @@ SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
// If the RHS isn't small, clear this vector and then steal its buffer.
if (!RHS.isSmall()) {
this->destroy_range(this->begin(), this->end());
if (!this->isSmall()) free(this->begin());
this->BeginX = RHS.BeginX;
this->Size = RHS.Size;
this->Capacity = RHS.Capacity;
RHS.resetToSmall();
this->assignRemote(std::move(RHS));
return *this;
}
@ -1228,7 +1233,20 @@ public:
}
SmallVector &operator=(SmallVector &&RHS) {
SmallVectorImpl<T>::operator=(::std::move(RHS));
if (N) {
SmallVectorImpl<T>::operator=(::std::move(RHS));
return *this;
}
// SmallVectorImpl<T>::operator= does not leverage N==0. Optimize the
// case.
if (this == &RHS)
return *this;
if (RHS.empty()) {
this->destroy_range(this->begin(), this->end());
this->Size = 0;
} else {
this->assignRemote(std::move(RHS));
}
return *this;
}