For the More Curious: Copy on Write

At this point, you may be wondering whether the copy behavior of Swift’s value types has any performance implications. For example, if you get a new copy of an Array every time you pass it into a function or assign it to a new constant or variable, won’t you have a wasteful number of copies floating about? In reality, it depends on your data and how you are using it. In practice, the value types in Swift’s standard library implement something called copy on write.

Copy on write, or COW, refers to the implicit sharing of value types’ underlying storage. This optimization allows instances of a value type to share their underlying storage, meaning that each instance does not have its own copy of the ...

Get Swift Programming: The Big Nerd Ranch Guide 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.