[llvm] d82617d - [ADT] Refactor SmallPtrSetImplBase::swap (NFC) (#154261)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 18 22:39:41 PDT 2025
Author: Kazu Hirata
Date: 2025-08-18T22:39:38-07:00
New Revision: d82617d2e8b3d97ef0898cb6d2c893f8e7bd01b3
URL: https://github.com/llvm/llvm-project/commit/d82617d2e8b3d97ef0898cb6d2c893f8e7bd01b3
DIFF: https://github.com/llvm/llvm-project/commit/d82617d2e8b3d97ef0898cb6d2c893f8e7bd01b3.diff
LOG: [ADT] Refactor SmallPtrSetImplBase::swap (NFC) (#154261)
SmallPtrSetImplBase::swap needs to deal with four cases depending on
whether LHS is small and whether RHS is small. Now, the code to swap
small LHS and large RHS is symmetric with the code to swap large LHS
and small RHS.
This patch rearranges code so that we first take care of the case
where both LHS and RHS are small. Then we compute references
SmallSide and LargeSide and actually swap the two instances.
This refactoing saves about 11 lines of code. Note that
SmallDenseMap::swap also uses a similar trick.
Added:
Modified:
llvm/lib/Support/SmallPtrSet.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp
index 39fe1715d19bf..a602165a0753c 100644
--- a/llvm/lib/Support/SmallPtrSet.cpp
+++ b/llvm/lib/Support/SmallPtrSet.cpp
@@ -250,46 +250,35 @@ void SmallPtrSetImplBase::swap(const void **SmallStorage,
// FIXME: From here on we assume that both sets have the same small size.
- // If only RHS is small, copy the small elements into LHS and move the pointer
- // from LHS to RHS.
- if (!this->isSmall() && RHS.isSmall()) {
- llvm::copy(RHS.small_buckets(), SmallStorage);
- std::swap(RHS.CurArraySize, this->CurArraySize);
+ // Both a small, just swap the small elements.
+ if (this->isSmall() && RHS.isSmall()) {
+ unsigned MinEntries = std::min(this->NumEntries, RHS.NumEntries);
+ std::swap_ranges(this->CurArray, this->CurArray + MinEntries, RHS.CurArray);
+ if (this->NumEntries > MinEntries) {
+ std::copy(this->CurArray + MinEntries, this->CurArray + this->NumEntries,
+ RHS.CurArray + MinEntries);
+ } else {
+ std::copy(RHS.CurArray + MinEntries, RHS.CurArray + RHS.NumEntries,
+ this->CurArray + MinEntries);
+ }
+ assert(this->CurArraySize == RHS.CurArraySize);
std::swap(this->NumEntries, RHS.NumEntries);
std::swap(this->NumTombstones, RHS.NumTombstones);
- RHS.CurArray = this->CurArray;
- RHS.IsSmall = false;
- this->CurArray = SmallStorage;
- this->IsSmall = true;
return;
}
- // If only LHS is small, copy the small elements into RHS and move the pointer
- // from RHS to LHS.
- if (this->isSmall() && !RHS.isSmall()) {
- llvm::copy(this->small_buckets(), RHSSmallStorage);
- std::swap(RHS.CurArraySize, this->CurArraySize);
- std::swap(RHS.NumEntries, this->NumEntries);
- std::swap(RHS.NumTombstones, this->NumTombstones);
- this->CurArray = RHS.CurArray;
- this->IsSmall = false;
- RHS.CurArray = RHSSmallStorage;
- RHS.IsSmall = true;
- return;
- }
-
- // Both a small, just swap the small elements.
- assert(this->isSmall() && RHS.isSmall());
- unsigned MinEntries = std::min(this->NumEntries, RHS.NumEntries);
- std::swap_ranges(this->CurArray, this->CurArray + MinEntries, RHS.CurArray);
- if (this->NumEntries > MinEntries) {
- std::copy(this->CurArray + MinEntries, this->CurArray + this->NumEntries,
- RHS.CurArray + MinEntries);
- } else {
- std::copy(RHS.CurArray + MinEntries, RHS.CurArray + RHS.NumEntries,
- this->CurArray + MinEntries);
- }
- assert(this->CurArraySize == RHS.CurArraySize);
- std::swap(this->NumEntries, RHS.NumEntries);
- std::swap(this->NumTombstones, RHS.NumTombstones);
+ // If only one side is small, copy the small elements into the large side and
+ // move the pointer from the large side to the small side.
+ SmallPtrSetImplBase &SmallSide = this->isSmall() ? *this : RHS;
+ SmallPtrSetImplBase &LargeSide = this->isSmall() ? RHS : *this;
+ const void **LargeSideInlineStorage =
+ this->isSmall() ? RHSSmallStorage : SmallStorage;
+ llvm::copy(SmallSide.small_buckets(), LargeSideInlineStorage);
+ std::swap(LargeSide.CurArraySize, SmallSide.CurArraySize);
+ std::swap(LargeSide.NumEntries, SmallSide.NumEntries);
+ std::swap(LargeSide.NumTombstones, SmallSide.NumTombstones);
+ SmallSide.CurArray = LargeSide.CurArray;
+ SmallSide.IsSmall = false;
+ LargeSide.CurArray = LargeSideInlineStorage;
+ LargeSide.IsSmall = true;
}
More information about the llvm-commits
mailing list