[PATCH] D82230: [ADT] Specialize std::swap() for SetVector

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 13:04:22 PDT 2020


nikic created this revision.
nikic added reviewers: bkramer, erichkeane.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.

This is intended to address a compile-time regression from rG1eddce4177cfddc86d4696b758904443b0b4f193 <https://reviews.llvm.org/rG1eddce4177cfddc86d4696b758904443b0b4f193>. A SmallPtrSet was replaced with a SetVector there, which had an unexpected large compile-time impact. It turns out that this structure is getting swapped a lot, and previously this used an optimized `std::swap` specialization for `SmallPtrSet`. Now it ends up using the default, triple-move based implementation, which is much more expensive.

This patch addresses the issue by specializing `std::swap()` for `SetVector`. I'll provide more specific compile-time numbers once I have them.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82230

Files:
  llvm/include/llvm/ADT/SetVector.h


Index: llvm/include/llvm/ADT/SetVector.h
===================================================================
--- llvm/include/llvm/ADT/SetVector.h
+++ llvm/include/llvm/ADT/SetVector.h
@@ -263,6 +263,11 @@
       remove(*SI);
   }
 
+  void swap(SetVector<T, Vector, Set> &RHS) {
+    set_.swap(RHS.set_);
+    vector_.swap(RHS.vector_);
+  }
+
 private:
   /// A wrapper predicate designed for use with std::remove_if.
   ///
@@ -308,4 +313,22 @@
 
 } // end namespace llvm
 
+namespace std {
+
+  /// Implement std::swap in terms of SetVector swap.
+  template<typename T, typename V, typename S>
+  inline void
+  swap(llvm::SetVector<T, V, S> &LHS, llvm::SetVector<T, V, S> &RHS) {
+    LHS.swap(RHS);
+  }
+
+  /// Implement std::swap in terms of SmallSetVector swap.
+  template<typename T, unsigned N>
+  inline void
+  swap(llvm::SmallSetVector<T, N> &LHS, llvm::SmallSetVector<T, N> &RHS) {
+    LHS.swap(RHS);
+  }
+
+} // end namespace std
+
 #endif // LLVM_ADT_SETVECTOR_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82230.272162.patch
Type: text/x-patch
Size: 985 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200619/78d9a49b/attachment-0001.bin>


More information about the llvm-commits mailing list