[llvm] b9c2dd1 - [ADT] Specialize std::swap() for SetVector

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 14:14:14 PDT 2020


Author: Nikita Popov
Date: 2020-07-13T23:14:04+02:00
New Revision: b9c2dd11a5139b754afca050effac80f3b638bc8

URL: https://github.com/llvm/llvm-project/commit/b9c2dd11a5139b754afca050effac80f3b638bc8
DIFF: https://github.com/llvm/llvm-project/commit/b9c2dd11a5139b754afca050effac80f3b638bc8.diff

LOG: [ADT] Specialize std::swap() for SetVector

This is intended to address a compile-time regression from
1eddce4177cfddc86d4696b758904443b0b4f193. 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 (partly) addresses the issue by specializing std::swap()
for SetVector.

Differential Revision: https://reviews.llvm.org/D82230

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h
index 901d5b1bcd90..91ad72143ed3 100644
--- a/llvm/include/llvm/ADT/SetVector.h
+++ b/llvm/include/llvm/ADT/SetVector.h
@@ -263,6 +263,11 @@ class SetVector {
       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 @@ class SmallSetVector
 
 } // 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


        


More information about the llvm-commits mailing list