# Large Struct COW optimization

**URL:** <https://forums.swift.org/t/large-struct-cow-optimization/46921>\
**Category:** Using Swift\
**Tags:** swift\
**Created:** [March 25, 2021, 8:53pm UTC](https://forums.swift.org/t/large-struct-cow-optimization/46921 "2021-03-25T20:53:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_vladi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/_vladi/32/11262_2.png) [@\_vladi](https://forums.swift.org/u/_vladi)\
**Post date:** [March 25, 2021, 8:53pm UTC](https://forums.swift.org/t/large-struct-cow-optimization/46921/1 "2021-03-25T20:53:35Z")

</div>

Hello,

Recently I found myself forced to think about the performance issues which COW for structs might introduce in my code. To put the problem into context I will try to explain the situation in greater detail (despite some of those details are not really relevant to the problem, they will give some better sense to the context). Specifically, I have some business layer DTO entity modeled using a struct which has to be mapped to another corespoding struct model. In order to map it, the initialization needs some information contained in a different entity. Take the following example:

```
struct DestinationDTO {
    let id: String
}

struct MeetingDTO {
    let id: String
}

struct Meeting {
    let id: String
    let destinationId: String
    
    init(meeting: MeetingDTO, destinations: [DestinationDTO]) {
        self.id = meeting.id
        ...
    }
}

let destinations = [DestinationDTO(id: "1"), DestinationDTO(id: "2")])

let meeting = Meeting(meeting: MeetingDTO(id: "21"), destinations: destinations]

```

Worth mentioning is that `Meeting` only needs one element from `destinations`, based on `destinationId`.

Now as I understand from the Swift documentation, value types are copied when passed as function arguments which means `destinations` is presumably copied when passed to the `Meeting` initializer. I shall continue by saying that for my particular case the corresponding `DestinationDTO` struct is one very large. Moreover this case also occurs one level deeper, where another field of `Meeting` is initialized in the same fashion using a field from the corresponding (by destinationId) destination .

Nevertheless, also mentioned in the Swift documentation is:

> Collections defined by the standard library like arrays, dictionaries, and strings use an optimization to reduce the performance cost of copying. Instead of making a copy immediately, these collections share the memory where the elements are stored between the original instance and any copies. If one of the copies of the collection is modified, the elements are copied just before the modification. The behavior you see in your code is always as if a copy took place immediately.

Provided the `destinations` collection is not modified during the initialization, can I resolve this to: the same copy of `destinations` is used for each initialization ? (if multiple are required at call site)

Thank you.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [March 25, 2021, 9:00pm UTC](https://forums.swift.org/t/large-struct-cow-optimization/46921/2 "2021-03-25T21:00:04Z")

</div>

Yes, that should share the underlying Array and String storage.

---

<div class="post-metadata">

**Author:** ![\_vladi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/_vladi/32/11262_2.png) [@\_vladi](https://forums.swift.org/u/_vladi)\
**Post date:** [March 25, 2021, 9:08pm UTC](https://forums.swift.org/t/large-struct-cow-optimization/46921/4 "2021-03-25T21:08:55Z")

</div>

Hey thanks, sounds good.

This means that even though, say inside the initializer, I would do something like:

`let meetingDestination = destinations.first { $0.id == destinationId }`

`meetingDestination` still does not trigger COW as long as it is just read?

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [March 25, 2021, 9:11pm UTC](https://forums.swift.org/t/large-struct-cow-optimization/46921/5 "2021-03-25T21:11:02Z")

</div>

Correct. You'd need to a) mutate it in some way, while b) more than one variable was sharing the storage.

---

<div class="post-metadata">

**Author:** ![\_vladi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/_vladi/32/11262_2.png) [@\_vladi](https://forums.swift.org/u/_vladi)\
**Post date:** [March 25, 2021, 9:13pm UTC](https://forums.swift.org/t/large-struct-cow-optimization/46921/6 "2021-03-25T21:13:37Z")

</div>

Fair enough, thank you ! 🤜 🤛
