I have a very large structure, which I want to ensure is not copied needlessly. How can I make a copy-on-write container for it?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Optimization techniques for backtracking regex imp
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
Here's a bit simpler example.
A copy-on-write is usually a
struct
wrapper over some backing object.The trick lies in asserting
isUniquelyReferenced
every time the boxed value is mutated. If the underlying storage object is singly referenced, nothing is to be done. However if another reference exists, one must create a new storage.Is this code thread-safe? It is exactly as safe as any other value type, e.g.
Int
orBool
.The previous answers aren't wrong, but there's a much simpler way. The Swift team has a list of performance tips, and they say:
It doesn't get much simpler than that!