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

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 25 15:13:59 PST 2022


MaskRay updated this revision to Diff 403049.
MaskRay edited the summary of this revision.
MaskRay added a comment.

Switch to early return.

@lichray It is unclear how to deduplicate `if (!this->isSmall()) free(this->begin()); this->BeginX = ...; this->Size = ...`, this portion of code moves `this->destroy_range(...)` before `if (RHS.empty())`.


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
@@ -1226,7 +1226,25 @@
   }
 
   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;
+    this->destroy_range(this->begin(), this->end());
+    if (RHS.empty()) {
+      this->Size = 0;
+    } else {
+      if (!this->isSmall())
+        free(this->begin());
+      this->BeginX = RHS.BeginX;
+      this->Size = RHS.Size;
+      this->Capacity = RHS.Capacity;
+      RHS.resetToSmall();
+    }
     return *this;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117976.403049.patch
Type: text/x-patch
Size: 889 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220125/87688695/attachment.bin>


More information about the llvm-commits mailing list