[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 22 22:06:21 PST 2022


MaskRay created this revision.
MaskRay added reviewers: dexonsmith, lichray, Quuxplusone.
Herald added a subscriber: pengfei.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.23KiB smaller. In lld .text is
6.42KiB smaller.


Repository:
  rG LLVM Github Monorepo

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 == 0) {
+      // 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();
+      }
+    } else {
+      SmallVectorImpl<T>::operator=(::std::move(RHS));
+    }
     return *this;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117976.402288.patch
Type: text/x-patch
Size: 917 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220123/7366e0ed/attachment.bin>


More information about the llvm-commits mailing list