# ARC overhead when wrapping a class in a struct which is only borrowed

**URL:** <https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037>\
**Category:** Using Swift\
**Tags:** performance, arc\
**Created:** [December 14, 2022, 9:49am UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037 "2022-12-14T09:49:22Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [December 19, 2022, 10:08pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/21 "2022-12-19T22:08:20Z")

</div>

> [@ksluder](#):
>
> Ah, ok, so it’s a meaningful question but the only way to answer it is to be overly conservative.

I'm not sure it is meaningful, because unless you suspend the entire process and don't call out to any code out of your immediate control, the answer you get is unusable as soon you get it, since anyone else borrowing the same strong reference can increment it. Doing something on behalf of the sole owner of the object seems to me like it'd require taking a lock of some sort to prevent anyone else from asserting ownership of the object while you need it to be uniquely-referenced. `inout` effectively statically implies that someone is asserting this unique ownership on the callee's behalf.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [December 19, 2022, 10:16pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/22 "2022-12-19T22:16:16Z")

</div>

I think the term “uniquely referenced” has become overloaded in this context. I believe that ideally `isKnownUniquelyReferenced()` would return `true` if and only if the argument is _either_ the sole strong reference to the object, _or_ is the sole (transitive) borrow of the sole strong reference to the object. At the point that `isKnownUniquelyReferenced()` returns, I have created an invariant that my program must uphold. It’s still a meaningful question to ask, but one that Swift (even with move semantics) lacks the tools to answer.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [December 19, 2022, 10:26pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/23 "2022-12-19T22:26:24Z")

</div>

> [@ksluder](#):
>
> I think the term “uniquely referenced” has become overloaded in this context. I believe that ideally `isKnownUniquelyReferenced()` would return `true` if and only if the argument is _either_ the sole strong reference to the object, _or_ is the sole (transitive) borrow of the sole strong reference to the object.

The problem is, there isn't any such thing as a sole shared borrow, and there isn't any way to tell because shared borrows are only represented as bitwise copies of the object pointer. We can answer the "sole strong reference" question for the owner of a strong reference, like you'd get from a `consuming` parameter, because the owned value keeps the reference count incremented, and we can answer it for an `inout` parameter, since `inout` statically has to be the sole borrower and user of the value, so we know the reference count can't change as long as the parameter holds that exclusive access.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [December 19, 2022, 10:37pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/24 "2022-12-19T22:37:36Z")

</div>

I think we’re ultimately saying the same thing in different ways. :)

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [December 20, 2022, 6:10am UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/25 "2022-12-20T06:10:17Z")

</div>

Sorry to make you repeat yourself, but this distinction hasn't sunk in to Swift community's diction yet. I often see noncopyable references referred to as unique references (see current proposals). We probably shouldn't be calling them that.

A borrow _can_ tell you whether a reference has unique ownership the same way that move-only references give you unique ownership. But mutation of the referenced object can happen via other borrows, so it isn't a _safe_ form of uniqueness.

To be unambiguous:

```swift
let object = Object()
borrowing b1 = object
borrowing b2 = object
isUniquelyReferenced(b1) // true
isUniquelyReferenced(b2) // true

```

Only useful if you know you're only mutating through `object`, not `b1`/`b2`.

The nice thing about noncopyable references is that an `inout` binding is sufficient to know that _the binding is unique_ without calling isUniquelyReferenced at all.

---

<div class="post-metadata">

**Author:** ![gonsolo](https://avatars.discourse-cdn.com/v4/letter/g/3e96dc/32.png) [@gonsolo](https://forums.swift.org/u/gonsolo)\
**Post date:** [December 20, 2022, 10:16am UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/26 "2022-12-20T10:16:26Z")

</div>

Adding -enable-ossa-modules results in the following error for me (Linux, using 5.7.2 from swiftlang.xyz):

```swift
swiftc -Xfrontend -enable-ossa-modules -O bla.swift
bla.swift:6:39: error: cannot find 'ProcessInfo' in scope
                let arguments = Array(ProcessInfo.processInfo.arguments.dropFirst())

```

```swift
import Foundation

class Main {

        static func main() {
                let arguments = Array(ProcessInfo.processInfo.arguments.dropFirst())
                print(arguments)
        }
}

```

It seems Foundation is not found anymore. Is this expected?

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [December 20, 2022, 10:25am UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/27 "2022-12-20T10:25:58Z")

</div>

> [@John\_McCall](#):
>
> Essentially the only thing you can't write is something that tests for a unique reference _and then doesn't even try to use that uniqueness_, e.g. `assert(isKnownUniquelyReferenced(myLet))` and not a whole lot else.

Which is incredibly useful. There are two other situations where I felt I needed it:

1. `DebugStringConvertible` instances where it's just useful to know when you're debugging something where you don't expect CoWs (broadly the same as your `assert`).
2. There are situations where it'd be good to know if a CoW copy is likely going to happen or not. I had this in a slab allocator recently where I'd like (if possible) to choose a slab (for adding more data) that has free space and is uniquely referenced. That was for a cache where I slice out parts of the caches slabs and sent them over the network. In almost all cases that will immediately send the data and the cache's slab is uniquely referenced so I can modify it without risking a CoW copy. But in certain cases that's not the case so I'd rather have my "which is the best slab to add the new data to" algorithm take the unique referencedness into account. Right now I'm doing something very conservative where I only modify the latest slab and I just never slice out of the latest one (and copy instead).

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [December 20, 2022, 4:00pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/28 "2022-12-20T16:00:22Z")

</div>

The other thing about allowing `isKnownUniquelyReferenced` on borrows is that, to give the results people want, we would have to prevent copies from being reduced to borrows if we couldn’t prove that the refcount wasn’t observed during that time.

The intent of `iKUR` is to conservatively answer the question “will this object be accessed other than through this specific reference or something later derived from it?”. This question must be conservatively answered `false` in at least some cases where the object will not be accessed, because of Rice’s Theorem, but a positive answer is required to be definitively accurate. This intent is why it’s acceptable for optimizations such as lifetime shortening to change the answer to this function: as long as we don’t cause a reference’s lifetime to end before it is last accessed, we’re actually just improving the accuracy of `iKUR`. But weakening a copy to a borrow would allow `iKUR` on the borrow to falsely return `true`.

The requirement of exclusivity forbids these simultaneous borrows and makes `iKUR` work today.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [December 20, 2022, 5:07pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/29 "2022-12-20T17:07:31Z")

</div>

> [@Andrew\_Trick](#):
>
> To be unambiguous:
> 
> ```swift
> let object = Object()
> borrowing b1 = object
> borrowing b2 = object
> isUniquelyReferenced(b1) // true
> isUniquelyReferenced(b2) // true
> 
> ```

For the sake of further eliminating ambiguity, isn’t `isKnownUniquelyReferenced(b1)` a compiler error right now? A `borrow` can’t satisfy an `inout` parameter, and John’s point is that this is intentional and necessary for `isKnownUniquelyReferenced()` to work.

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [December 20, 2022, 6:26pm UTC](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037/30 "2022-12-20T18:26:49Z")

</div>

> [@ksluder](#):
>
> For the sake of further eliminating ambiguity, isn’t `isKnownUniquelyReferenced(b1)` a compiler error right now? A `borrow` can’t satisfy an `inout` parameter, and John’s point is that this is intentional and necessary for `isKnownUniquelyReferenced()` to work.

Yes, both the `borrowing` binding and iKUR that takes a borrow are hypothetical. When we do have `borrowing` then iKUR won't accept such a variable. John has pointed out three times now that it would be misused. Basically, if you were ever to mutate the referenced object without `inout` exclusivity, then aliasing borrows or even copies of the reference would end up observing the mutation.

[Previous page](https://forums.swift.org/t/arc-overhead-when-wrapping-a-class-in-a-struct-which-is-only-borrowed/62037.md?page=1)
