For the More Curious: Copy on Write

You might be wondering about the performance implications of Swift’s copying behavior. For example, if you get a new copy of an Array every time you pass it in to a function or assign it to a new constant or variable, won’t you have a wasteful number of copies floating about? What about very large arrays?

The answers depend on your data and how you are using it.

Swift’s standard library types use a strategy called copy on write to ensure that working with value types is not wasteful. Copy on write, or COW, refers to the implicit sharing of value types’ underlying storage. Instances of a value type do not immediately have their own copies of the data. They share their underlying storage, with each instance ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.