[PATCH] D117976: [SmallVector] Optimize move assignment operator for N==0 case
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 29 11:03:54 PST 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG333f5019300c: [SmallVector] Optimize move assignment operator for N==0 case (authored by MaskRay).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D117976/new/
https://reviews.llvm.org/D117976
Files:
llvm/include/llvm/ADT/SmallVector.h
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -568,6 +568,16 @@
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 @@
// 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 @@
}
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;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117976.404292.patch
Type: text/x-patch
Size: 1614 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220129/0865d0a8/attachment.bin>
More information about the llvm-commits
mailing list