Skip to content

[stdlib] Do not allocate when creating a Set from a generic Sequence which happens to be another Set #62158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2022

Conversation

karwa
Copy link
Contributor

@karwa karwa commented Nov 17, 2022

Previous implementation calls .init(minimumCapacity:), allocating storage, and then sets the _variant, which deallocates that storage.

Also, the comment made it sound like this only applied to native Sets (perhaps excluding bridged sets?), but that does not appear to be the case. _variant is a plain, stored property - it will always be overwritten with the downcasted Set's variant.

@karwa
Copy link
Contributor Author

karwa commented Nov 17, 2022

@lorentey could you take a look?

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

Oh good catch 💯

@lorentey
Copy link
Member

@swift-ci test

@lorentey
Copy link
Member

lorentey commented Dec 1, 2022

Also, the comment made it sound like this only applied to native Sets (perhaps excluding bridged sets?), but that does not appear to be the case. _variant is a plain, stored property - it will always be overwritten with the downcasted Set's variant.

I suspect the original intention was not to adopt the storage of bridged NSSets, so that this initializer would always return a native set instance. Then again, I don't think there is a need to implement this -- the current semantics are fine. (The canonical way to convert a bridged set to native is to call reserveCapacity(0) on it, not to call this initializer.)

@lorentey lorentey merged commit 7b45c8b into swiftlang:main Dec 1, 2022
@karwa karwa deleted the patch-16 branch December 1, 2022 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants