Skip to content
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

Add pointee: borrowing T to std.shared_ptr<T> for C++ interop #78296

Open
mrousavy opened this issue Dec 19, 2024 · 0 comments
Open

Add pointee: borrowing T to std.shared_ptr<T> for C++ interop #78296

mrousavy opened this issue Dec 19, 2024 · 0 comments
Labels
feature A feature request or implementation triage needed This issue needs more specific labels

Comments

@mrousavy
Copy link

Motivation

Currently, std.shared_ptr<T>.pointee actually copies T, which is a big issue for pretty much every type wrapped in a std::shared_ptr that I have worked with - they're wrapped in a shared_ptr for a reason.

func doSomething(value: MySharedPtr) {
  value.pointee.doFirst()
  value.pointee.doSecond()
  value.pointee.doThird()
  // 3x copy, 3x destructor!
}

This is not only a performance concern, but maybe even a fundamental issue - some types are not expected to be copied and might delete resources unsafely in their destructors.

Proposed solution

It would be dope to add an API that allows us to get a borrowing T to avoid a copy.

Also, maybe this even allows for using non-copyable C++ types in Swift?

Alternatives considered

I can add SWIFT_IMMORTAL_REFERENCE to every type that I plan on using inside of a std::shared_ptr, but this isn't ideal either, as sometimes I use external types that are not written by me.

Additional information

No response

@mrousavy mrousavy added feature A feature request or implementation triage needed This issue needs more specific labels labels Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant