Skip to content

Commit d82617d

Browse files
[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.
1 parent 6c3a0ab commit d82617d

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

llvm/lib/Support/SmallPtrSet.cpp

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -250,46 +250,35 @@ void SmallPtrSetImplBase::swap(const void **SmallStorage,
250250

251251
// FIXME: From here on we assume that both sets have the same small size.
252252

253-
// If only RHS is small, copy the small elements into LHS and move the pointer
254-
// from LHS to RHS.
255-
if (!this->isSmall() && RHS.isSmall()) {
256-
llvm::copy(RHS.small_buckets(), SmallStorage);
257-
std::swap(RHS.CurArraySize, this->CurArraySize);
253+
// Both a small, just swap the small elements.
254+
if (this->isSmall() && RHS.isSmall()) {
255+
unsigned MinEntries = std::min(this->NumEntries, RHS.NumEntries);
256+
std::swap_ranges(this->CurArray, this->CurArray + MinEntries, RHS.CurArray);
257+
if (this->NumEntries > MinEntries) {
258+
std::copy(this->CurArray + MinEntries, this->CurArray + this->NumEntries,
259+
RHS.CurArray + MinEntries);
260+
} else {
261+
std::copy(RHS.CurArray + MinEntries, RHS.CurArray + RHS.NumEntries,
262+
this->CurArray + MinEntries);
263+
}
264+
assert(this->CurArraySize == RHS.CurArraySize);
258265
std::swap(this->NumEntries, RHS.NumEntries);
259266
std::swap(this->NumTombstones, RHS.NumTombstones);
260-
RHS.CurArray = this->CurArray;
261-
RHS.IsSmall = false;
262-
this->CurArray = SmallStorage;
263-
this->IsSmall = true;
264267
return;
265268
}
266269

267-
// If only LHS is small, copy the small elements into RHS and move the pointer
268-
// from RHS to LHS.
269-
if (this->isSmall() && !RHS.isSmall()) {
270-
llvm::copy(this->small_buckets(), RHSSmallStorage);
271-
std::swap(RHS.CurArraySize, this->CurArraySize);
272-
std::swap(RHS.NumEntries, this->NumEntries);
273-
std::swap(RHS.NumTombstones, this->NumTombstones);
274-
this->CurArray = RHS.CurArray;
275-
this->IsSmall = false;
276-
RHS.CurArray = RHSSmallStorage;
277-
RHS.IsSmall = true;
278-
return;
279-
}
280-
281-
// Both a small, just swap the small elements.
282-
assert(this->isSmall() && RHS.isSmall());
283-
unsigned MinEntries = std::min(this->NumEntries, RHS.NumEntries);
284-
std::swap_ranges(this->CurArray, this->CurArray + MinEntries, RHS.CurArray);
285-
if (this->NumEntries > MinEntries) {
286-
std::copy(this->CurArray + MinEntries, this->CurArray + this->NumEntries,
287-
RHS.CurArray + MinEntries);
288-
} else {
289-
std::copy(RHS.CurArray + MinEntries, RHS.CurArray + RHS.NumEntries,
290-
this->CurArray + MinEntries);
291-
}
292-
assert(this->CurArraySize == RHS.CurArraySize);
293-
std::swap(this->NumEntries, RHS.NumEntries);
294-
std::swap(this->NumTombstones, RHS.NumTombstones);
270+
// If only one side is small, copy the small elements into the large side and
271+
// move the pointer from the large side to the small side.
272+
SmallPtrSetImplBase &SmallSide = this->isSmall() ? *this : RHS;
273+
SmallPtrSetImplBase &LargeSide = this->isSmall() ? RHS : *this;
274+
const void **LargeSideInlineStorage =
275+
this->isSmall() ? RHSSmallStorage : SmallStorage;
276+
llvm::copy(SmallSide.small_buckets(), LargeSideInlineStorage);
277+
std::swap(LargeSide.CurArraySize, SmallSide.CurArraySize);
278+
std::swap(LargeSide.NumEntries, SmallSide.NumEntries);
279+
std::swap(LargeSide.NumTombstones, SmallSide.NumTombstones);
280+
SmallSide.CurArray = LargeSide.CurArray;
281+
SmallSide.IsSmall = false;
282+
LargeSide.CurArray = LargeSideInlineStorage;
283+
LargeSide.IsSmall = true;
295284
}

0 commit comments

Comments
 (0)