Implement manual Copy on write - Swift questions

Hi,


Apple in his document about Writing High-Performance Swift Code, explains a way to do the Copy on Write manually, here:


https://github.com/apple/swift/blob/master/docs/OptimizationTips.rst#advice-use-copy-on-write-semantics-for-large-values


Nice section, but this arises some questions to me:


  • When we should start to consider to use manual Copy on Write?
  • What can be a large value?
  • How that cost can be measured?
  • Any reference to benchmarks / memory usage using COW?
  • How we can solve the non thread safe nature for the copy on write?


Thanks

Accepted Answer

I recommend you ask this question over at forums.swift.org, in the Using Swift category, and you will find lots of well-informed people who'd love to discuss this with you. 🙂


A couple of not-highly-technical points to consider:


— It's easier to use a reference type (class) than to add copy-on-write behavior to a value type (class), if that's a realistic solution in a particular scenario.


— Background threads (in macOS) don't have very much stack allocated by default. (It was 8KB, the last time I looked. The main thread has a lot more.) "A large value" might be estimated as the size of your struct multiplied by the number of copies that might exist at one time. If that total reaches several KB, it's probably time to start thinking about reference types or COW semantics, since using such values in a background thread starts to look risky.

Implement manual Copy on write - Swift questions
 
 
Q