From f4eecc9fbab2bac81d60ed9aa04c441f4e7f5777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crazy=E5=87=A1?= <15794964+CrazyFanFan@users.noreply.github.com> Date: Thu, 13 Nov 2025 00:25:48 +0800 Subject: [PATCH] optimize contains method in CxxSet --- .../ClangDerivedConformances.cpp | 11 +++++---- stdlib/public/Cxx/CxxSet.swift | 24 +++++++------------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/ClangImporter/ClangDerivedConformances.cpp b/lib/ClangImporter/ClangDerivedConformances.cpp index 593ea94c26667..d6799817c4bbd 100644 --- a/lib/ClangImporter/ClangDerivedConformances.cpp +++ b/lib/ClangImporter/ClangDerivedConformances.cpp @@ -996,11 +996,6 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl, insert->getResultInterfaceType()); impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxSet}); - // If this isn't a std::multiset, try to also synthesize the conformance to - // CxxUniqueSet. - if (!isStdDecl(clangDecl, {"set", "unordered_set"})) - return; - ProtocolDecl *cxxInputIteratorProto = ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator); if (!cxxInputIteratorProto) @@ -1025,6 +1020,12 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl, rawIteratorTy); impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawMutableIterator"), rawMutableIteratorTy); + + // If this isn't a std::multiset, try to also synthesize the conformance to + // CxxUniqueSet. + if (!isStdDecl(clangDecl, {"set", "unordered_set"})) + return; + impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxUniqueSet}); } diff --git a/stdlib/public/Cxx/CxxSet.swift b/stdlib/public/Cxx/CxxSet.swift index eb8c169536abc..80306ca1237a4 100644 --- a/stdlib/public/Cxx/CxxSet.swift +++ b/stdlib/public/Cxx/CxxSet.swift @@ -19,16 +19,22 @@ public protocol CxxSet: ExpressibleByArrayLiteral { associatedtype Element associatedtype Size: BinaryInteger + associatedtype RawIterator: UnsafeCxxInputIterator + where RawIterator.Pointee == Element + associatedtype RawMutableIterator: UnsafeCxxInputIterator + where RawMutableIterator.Pointee == Element // std::pair for std::set and std::unordered_set // iterator for std::multiset - associatedtype InsertionResult + associatedtype InsertionResult init() + func __endUnsafe() -> RawIterator + func __findUnsafe(_ value: Element) -> RawIterator + @discardableResult mutating func __insertUnsafe(_ element: Element) -> InsertionResult - func count(_ element: Element) -> Size } @@ -56,7 +62,7 @@ extension CxxSet { /// in the set. @inlinable public func contains(_ element: Element) -> Bool { - return count(element) > 0 + return self.__findUnsafe(element) != self.__endUnsafe() } } @@ -65,23 +71,11 @@ extension CxxSet { /// C++ standard library types such as `std::set` and `std::unordered_set` /// conform to this protocol. public protocol CxxUniqueSet: CxxSet { - override associatedtype Element - override associatedtype Size: BinaryInteger - associatedtype RawIterator: UnsafeCxxInputIterator - where RawIterator.Pointee == Element - associatedtype RawMutableIterator: UnsafeCxxInputIterator - where RawMutableIterator.Pointee == Element override associatedtype InsertionResult where InsertionResult: CxxPair - @discardableResult - mutating func __findUnsafe(_ value: Element) -> RawIterator - @discardableResult mutating func __eraseUnsafe(_ iter: RawIterator) -> RawMutableIterator - - @discardableResult - mutating func __endUnsafe() -> RawIterator } extension CxxUniqueSet {