Skip to content

[cxx-interop] Add CxxMutableRandomAccessCollection protocol #76106

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
Sep 2, 2024

Conversation

egorzhdan
Copy link
Contributor

This conforms mutable C++ container types, such as std::vector, to MutableCollection via a new overlay protocol CxxMutableRandomAccessCollection.

rdar://134531554

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Aug 27, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

LGTM!

internal func _getRawIterator(at index: Int) -> RawIterator {
var rawIterator = self.__beginUnsafe()
rawIterator += RawIterator.Distance(index)
precondition(self.__endUnsafe() - rawIterator > 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the plan for these when hardening for libcxx is on? Are we relying on the optimizer to remove the redundant checks or do we have a way to disable precondition checks when hardening is turned on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@susmonteiro might know more details here, but the general idea is that there won't be a bounds-check here with hardening enabled, unless you also enable libc++ bounded iterators. This is because we're not using the C++ operator[] – this function would only be invoked if the C++ type does not have a suitable overload of operator[].

We don't currently have a good way to check if bounded iterators are enabled and provide an alternative implementation, so for now we're just hoping that the optimizer removes these checks, I think.

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-mutable-rac branch from 14dab96 to 536630f Compare August 27, 2024 18:14
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@susmonteiro susmonteiro left a comment

Choose a reason for hiding this comment

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

LGTM thanks!

This conforms mutable C++ container types, such as `std::vector`, to `MutableCollection` via a new overlay protocol `CxxMutableRandomAccessCollection`.

rdar://134531554
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-mutable-rac branch from 536630f to 0ab6815 Compare August 28, 2024 11:57
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@swift-ci please build toolchain CentOS 7

@egorzhdan
Copy link
Contributor Author

@swift-ci please build toolchain UBI9

@egorzhdan
Copy link
Contributor Author

@swift-ci please benchmark

@egorzhdan egorzhdan merged commit 39b8b3c into main Sep 2, 2024
5 of 6 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/cxx-mutable-rac branch September 2, 2024 10:35
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