Skip to content

[NFC] Use unique_ptr in SparseSet #116617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

optimisan
Copy link
Contributor

This allows implementing the move constructor.

Copy link
Contributor Author

optimisan commented Nov 18, 2024

@llvmbot
Copy link
Member

llvmbot commented Nov 18, 2024

@llvm/pr-subscribers-llvm-adt

Author: Akshat Oke (optimisan)

Changes

This allows implementing the move constructor.


Full diff: https://github.com/llvm/llvm-project/pull/116617.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/SparseSet.h (+11-7)
diff --git a/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h
index c7793117ff5408..1adae0d4595ac4 100644
--- a/llvm/include/llvm/ADT/SparseSet.h
+++ b/llvm/include/llvm/ADT/SparseSet.h
@@ -129,7 +129,12 @@ class SparseSet {
   using DenseT = SmallVector<ValueT, 8>;
   using size_type = unsigned;
   DenseT Dense;
-  SparseT *Sparse = nullptr;
+
+  struct Deleter {
+    void operator()(SparseT *S) { free(S); }
+  };
+  std::unique_ptr<SparseT, Deleter> Sparse;
+
   unsigned Universe = 0;
   KeyFunctorT KeyIndexOf;
   SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf;
@@ -144,7 +149,7 @@ class SparseSet {
   SparseSet() = default;
   SparseSet(const SparseSet &) = delete;
   SparseSet &operator=(const SparseSet &) = delete;
-  ~SparseSet() { free(Sparse); }
+  SparseSet(SparseSet &&) = default;
 
   /// setUniverse - Set the universe size which determines the largest key the
   /// set can hold.  The universe must be sized before any elements can be
@@ -159,11 +164,10 @@ class SparseSet {
     // Hysteresis prevents needless reallocations.
     if (U >= Universe/4 && U <= Universe)
       return;
-    free(Sparse);
     // The Sparse array doesn't actually need to be initialized, so malloc
     // would be enough here, but that will cause tools like valgrind to
     // complain about branching on uninitialized data.
-    Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
+    Sparse.reset(static_cast<SparseT *>(safe_calloc(U, sizeof(SparseT))));
     Universe = U;
   }
 
@@ -205,7 +209,7 @@ class SparseSet {
     assert(Idx < Universe && "Key out of range");
     assert(Sparse != nullptr && "Invalid sparse type");
     const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
-    for (unsigned i = Sparse[Idx], e = size(); i < e; i += Stride) {
+    for (unsigned i = Sparse.get()[Idx], e = size(); i < e; i += Stride) {
       const unsigned FoundIdx = ValIndexOf(Dense[i]);
       assert(FoundIdx < Universe && "Invalid key in set. Did object mutate?");
       if (Idx == FoundIdx)
@@ -255,7 +259,7 @@ class SparseSet {
     iterator I = findIndex(Idx);
     if (I != end())
       return std::make_pair(I, false);
-    Sparse[Idx] = size();
+    Sparse.get()[Idx] = size();
     Dense.push_back(Val);
     return std::make_pair(end() - 1, true);
   }
@@ -292,7 +296,7 @@ class SparseSet {
       *I = Dense.back();
       unsigned BackIdx = ValIndexOf(Dense.back());
       assert(BackIdx < Universe && "Invalid key in set. Did object mutate?");
-      Sparse[BackIdx] = I - begin();
+      Sparse.get()[BackIdx] = I - begin();
     }
     // This depends on SmallVector::pop_back() not invalidating iterators.
     // std::vector::pop_back() doesn't give that guarantee.

@@ -205,7 +209,7 @@ class SparseSet {
assert(Idx < Universe && "Key out of range");
assert(Sparse != nullptr && "Invalid sparse type");
const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
for (unsigned i = Sparse[Idx], e = size(); i < e; i += Stride) {
for (unsigned i = Sparse.get()[Idx], e = size(); i < e; i += Stride) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make the std::unique_ptr<SparseT, Deleter> into a std::unique_ptr<SparseT[], Deleter> then you can use [] directly without the .get() I think?

Copy link
Contributor Author

@optimisan optimisan Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

@dwblaikie
Copy link
Collaborator

Oh, and please add unit test coverage for the new move functionality.

@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch from a001cd1 to 74150e7 Compare November 19, 2024 11:56
@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_nfc_use_unique_ptr_in_sparseset branch from 4d29a87 to 1de6feb Compare November 19, 2024 11:56
Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks!

@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch from 74150e7 to f3ca5cd Compare November 22, 2024 11:14
@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_nfc_use_unique_ptr_in_sparseset branch from 1de6feb to a1eb74a Compare November 22, 2024 11:15
Copy link
Contributor Author

optimisan commented Nov 22, 2024

Merge activity

  • Nov 22, 6:17 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Nov 22, 6:22 AM EST: Graphite rebased this pull request as part of a merge.
  • Nov 22, 6:24 AM EST: A user merged this pull request with Graphite.

@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch from f3ca5cd to c89ffe2 Compare November 22, 2024 11:19
Base automatically changed from users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm to main November 22, 2024 11:21
This allows implementing the move constructor.
@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_nfc_use_unique_ptr_in_sparseset branch from a1eb74a to 175470e Compare November 22, 2024 11:22
@optimisan optimisan merged commit dde9477 into main Nov 22, 2024
5 of 7 checks passed
@optimisan optimisan deleted the users/Akshat-Oke/11-18-_nfc_use_unique_ptr_in_sparseset branch November 22, 2024 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants