# Does String copy storage between actors?

**URL:** <https://forums.swift.org/t/does-string-copy-storage-between-actors/74747>\
**Category:** Using Swift\
**Tags:** actors, structured-concurren\
**Created:** [September 19, 2024, 3:24pm UTC](https://forums.swift.org/t/does-string-copy-storage-between-actors/74747 "2024-09-19T15:24:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![STREGA](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/strega/32/37366_2.png) [@STREGA](https://forums.swift.org/u/STREGA)\
**Post date:** [September 19, 2024, 3:24pm UTC](https://forums.swift.org/t/does-string-copy-storage-between-actors/74747/1 "2024-09-19T15:24:00Z")

</div>

Hey all,

I’m migrating a library to Swift 6.

I use String as an identifier for file resource cache lookups.

I’m mildly aware that string is not just a struct, but also references a storage container; like array.

Are there performance hits for transferring Array or String between different actors?

For example does the entire storage get copied every time the array and string are sent? Or does the copy-on-write functionality still hold up across actors?

I’m trying to decide if creating a lookup table actor would be worth my time and energy to reduce copying, if copying would occur.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [September 19, 2024, 3:37pm UTC](https://forums.swift.org/t/does-string-copy-storage-between-actors/74747/2 "2024-09-19T15:37:38Z")

</div>

In CoW containers, `isKnownUniquelyReferenced` is used under the hood to check an atomic reference count on modification universally, including concurrent access, which is why it is atomic in the first place.

Since you can only directly send immutable values, you can access the same immutable string from any isolation.

```swift
var mutable = "foo"
let immutable = mutable // no copy, ref count +1

Task {
  // stringConsumer(mutable) <- error, can't send `mutable`
  stringConsumer(immutable) // no copies here

  var newMutable = immutable // no copy, ref count +1

  // creates a copy, `isKnownUniquelyReferenced` is `false` here
  newMutable.append("baz")
}

// creates a copy, `isKnownUniquelyReferenced` is `false` here
mutable.append("bar")
```

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [September 19, 2024, 4:20pm UTC](https://forums.swift.org/t/does-string-copy-storage-between-actors/74747/3 "2024-09-19T16:20:15Z")

</div>

> [@Max\_Desiatov](#):
>
> I'd expect `isKnownUniquelyReferenced` in CoW containers under the hood is used to check an atomic reference count on modification universally, including concurrent access, which is why it is atomic in the first place.

Correct! It's thread-safe by virtue of the reference counting being synchronized already. It's just doing a single check, and doesn't also modify the ref count, so there's no [TOC/TOU](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) concerns.

By the time you're able to call `isKnownUniquelyReferenced(x)`, you've got a strong reference to `x`. At that point, it can check if the reference count is `>1`, indicating that some other reference must exist.

Here's an enlightening thread on this matter: [isKnownUniquelyReferenced thread safety](https://forums.swift.org/t/isknownuniquelyreferenced-thread-safety/22933)

---

<div class="post-metadata">

**Author:** ![STREGA](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/strega/32/37366_2.png) [@STREGA](https://forums.swift.org/u/STREGA)\
**Post date:** [September 19, 2024, 11:56pm UTC](https://forums.swift.org/t/does-string-copy-storage-between-actors/74747/4 "2024-09-19T23:56:57Z")

</div>

Excellent. I needed that extra confidence before getting locked into passing lots of immutable strings around

Thanks guys!
