Skip to content

Commit d16f467

Browse files
authored
[stdlib] Do not allocate when creating a Set from a generic Sequence which happens to be another Set
1 parent 942490d commit d16f467

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

stdlib/public/core/Set.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,13 +671,12 @@ extension Set: SetAlgebra {
671671
@inlinable
672672
public init<Source: Sequence>(_ sequence: __owned Source)
673673
where Source.Element == Element {
674-
self.init(minimumCapacity: sequence.underestimatedCount)
675674
if let s = sequence as? Set<Element> {
676-
// If this sequence is actually a native `Set`, then we can quickly
677-
// adopt its native buffer and let COW handle uniquing only
678-
// if necessary.
679-
self._variant = s._variant
675+
// If this sequence is actually a `Set`, then we can quickly
676+
// adopt its storage and let COW handle uniquing only if necessary.
677+
self = s
680678
} else {
679+
self.init(minimumCapacity: sequence.underestimatedCount)
681680
for item in sequence {
682681
insert(item)
683682
}

0 commit comments

Comments
 (0)