Advanced Swift: Values and References 路 Challenge: COW | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1940309-advanced-swift-values-and-references/lessons/7

Ray does a great job but I鈥檓 pretty sure he鈥檚 comment on this is dated as Swift did optimizations to String where this is no longer an issue.

From the current Swift Docs on developer.apple.com:

Performance Optimizations

Although strings in Swift have value semantics, strings use a copy-on-write strategy to store their data in a buffer. This buffer can then be shared by different copies of a string. A string鈥檚 data is only copied lazily, upon mutation, when more than one string instance is using the same buffer. Therefore, the first in any sequence of mutating operations may cost O(n) time and space.

When a string鈥檚 contiguous storage fills up, a new buffer must be allocated and data must be moved to the new storage. String buffers use an exponential growth strategy that makes appending to a string a constant time operation when averaged over many append operations.