Skip to content

Conversation

@CrazyFanFan
Copy link
Contributor

Replaces count(element) > 0 with __findUnsafe(element) != __endUnsafe() in CxxSet.contains implementation to avoid redundant counting operations and improve performance.

@egorzhdan
Copy link
Contributor

@swift-ci please test

@CrazyFanFan
Copy link
Contributor Author

The Windows CI tests are currently failing, but looking at the logs, it looks like it’s unrelated to my changes here.


init()

func __endUnsafe() -> RawIterator
func __findUnsafe(_ value: Element) -> RawIterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was mutating previously, is there a reason why it's not anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out!

I removed mutating because neither __endUnsafe() nor __findUnsafe(_:) modifies the underlying storage—this aligns with their semantic purpose, and they’re used in the non-mutating contains(_:).

That said, I recognize this is a breaking change to the public API. Though, since these methods are prefixed with __, I wondered if that might make the change more acceptable.

I’d like to align with the project’s preferences here—would you recommend restoring the original signatures to preserve compatibility, or keeping the semantically accurate version?

Happy to implement whichever approach makes the most sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Cxx and CxxStdlib overlays aren't part of the Swift's ABI stability guarantees, so it's totally fine to change their interfaces. For this reason these two libraries are distributed as part of the toolchain, not part of the SDK, and are version-locked to the compiler.

As long as making these functions non-mutating doesn't break any tests, it should be OK to make this change.

@@ -19,16 +19,22 @@
public protocol CxxSet<Element>: ExpressibleByArrayLiteral {
associatedtype Element
associatedtype Size: BinaryInteger
associatedtype RawIterator: UnsafeCxxInputIterator
where RawIterator.Pointee == Element
associatedtype RawMutableIterator: UnsafeCxxInputIterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be UnsafeCxxMutableInputIterator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::set iterators are indeed immutable since modifying set elements would break the internal ordering. This aligns with the existing StdSet implementation in Swift's ClangImporter

@hnrklssn
Copy link
Member

@swift-ci please smoke test windows platform

@CrazyFanFan CrazyFanFan force-pushed the optimization/cxx_set_contains branch from 197856e to f4eecc9 Compare November 14, 2025 13:29
@egorzhdan
Copy link
Contributor

@swift-ci please test

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @CrazyFanFan! As long as the CI is green, this LGTM.

Copy link
Member

@hnrklssn hnrklssn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple of C++ interop benchmarks in https://github.com/swiftlang/swift/tree/main/benchmark/cxx-source. Feel free to add one for CxxSet.contains if you want. I won't block on it though.

@egorzhdan
Copy link
Contributor

I'll merge this PR now. @CrazyFanFan if you're interested in adding a benchmark as @hnrklssn suggested, please feel free to submit a separate PR – no pressure on that though 😉

@egorzhdan egorzhdan merged commit 6b118ac into swiftlang:main Nov 20, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ interop Feature: Interoperability with C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants