3 ms·
Curious about an example of this in Swift? Is it a variable holding a collection like an Array?
by burntcaramel 2y ago
Curious about an example of this in Swift? Is it a variable holding a collection like an Array?
- plorkyeran 2y agoA really simple example: class Foo { var foo = [Int]() var bar: [Int] { get { foo } set { foo = newValue } } } let obj = Foo() Calling `obj.foo.append(i)` in a loop takes linear time, while `obj.bar.append(1)` is quadratic time. `obj.foo.append()` does a borrow operation resulting in there never being more than one reference at a time, while `obj.bar.append()` does a get followed by a set, meaning that there's always two references to the array and every append does a copy on write. `let bar = obj.bar; for i = 0..<max { bar.append(i) }; obj.bar = bar` would do just a single CoW. Usually of course your computed properties actually do something so this difference feels less surprising. Swift does offer the undocumented `_modify` property accessor to let computed operations do borrows, but making it an official feature is waiting for noncopyable types to be finalized.