@@ -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