From 00cb1c5330c74eb6677e3fef78fd8cf613ae7d22 Mon Sep 17 00:00:00 2001 From: LucianoAlmeida Date: Sat, 11 Sep 2021 19:12:42 -0300 Subject: [PATCH] [stdlib] Improve set isDisjoint by iterating on smaller set --- stdlib/public/core/Set.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/stdlib/public/core/Set.swift b/stdlib/public/core/Set.swift index f4fb463fe24db..2567b656e0f74 100644 --- a/stdlib/public/core/Set.swift +++ b/stdlib/public/core/Set.swift @@ -1123,7 +1123,15 @@ extension Set { /// otherwise, `false`. @inlinable public func isDisjoint(with other: Set) -> Bool { - return _isDisjoint(with: other) + guard !isEmpty && !other.isEmpty else { return true } + let (smaller, larger) = + count < other.count ? (self, other) : (other, self) + for member in smaller { + if larger.contains(member) { + return false + } + } + return true } @inlinable