[PATCH] D156005: Use `getHashValue` in `SetVector::insert` and `SetVector::contains`

Evan Wilde via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 21 16:05:26 PDT 2023


etcwilde created this revision.
etcwilde added reviewers: 0xdc03, nikic.
etcwilde added projects: All, LLVM.
Herald added a subscriber: StephenFan.
etcwilde requested review of this revision.
Herald added a subscriber: llvm-commits.

https://reviews.llvm.org/D152497 changed the semantics to use `operator==` when inserting into a "small" SetVector instead of the hash value. This change puts the original hash-value equality check semantics back, but keeps the linear search. `SetVector::insert` and `SetVector::contains` used to use hash-value equality to identify values that were the same, though `remove` used value-equality to identify values that were the same.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156005

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
@@ -56,7 +56,7 @@
           typename Set = DenseSet<T>, unsigned N = 0>
 class SetVector {
   // Much like in SmallPtrSet, this value should not be too high to prevent
-  // excessively long linear scans from occuring.
+  // excessively long linear scans from occurring.
   static_assert(N <= 32, "Small size should be less than or equal to 32!");
 
 public:
@@ -162,7 +162,11 @@
   bool insert(const value_type &X) {
     if constexpr (canBeSmall())
       if (isSmall()) {
-        if (llvm::find(vector_, X) == vector_.end()) {
+        unsigned hashedKey = llvm::DenseMapInfo<value_type>::getHashValue(X);
+        auto comparator = [hashedKey](const value_type &element) -> bool {
+          return hashedKey == llvm::DenseMapInfo<value_type>::getHashValue(element);
+        };
+        if (llvm::find_if(vector_, comparator) == vector_.end()) {
           vector_.push_back(X);
           if (vector_.size() > N)
             makeBig();
@@ -253,8 +257,13 @@
   /// Check if the SetVector contains the given key.
   bool contains(const key_type &key) const {
     if constexpr (canBeSmall())
-      if (isSmall())
-        return is_contained(vector_, key);
+      if (isSmall()) {
+        unsigned hashedKey = llvm::DenseMapInfo<value_type>::getHashValue(key);
+        auto comparator = [hashedKey](const value_type &element) -> bool {
+          return hashedKey == llvm::DenseMapInfo<value_type>::getHashValue(element);
+        };
+        return llvm::find_if(vector_, comparator) != vector_.end();
+      }
 
     return set_.find(key) != set_.end();
   }
@@ -264,7 +273,7 @@
   size_type count(const key_type &key) const {
     if constexpr (canBeSmall())
       if (isSmall())
-        return is_contained(vector_, key);
+        return contains(key);
 
     return set_.count(key);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156005.543103.patch
Type: text/x-patch
Size: 1990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230721/58d497c1/attachment.bin>


More information about the llvm-commits mailing list