Does swift copy on write for all structs?
问题 I know that swift will optimize to copy on write for arrays but will it do this for all structs? For example: struct Point { var x:Float = 0 } var p1 = Point() var p2 = p1 //p1 and p2 share the same data under the hood p2.x += 1 //p2 now has its own copy of the data 回答1: Array is implemented with copy-on-write behaviour – you'll get it regardless of any compiler optimisations (although of course, optimisations can decrease the number of cases where a copy needs to happen). At a basic level,