Foundation could cache immutable copies of mutable containers.
| Originator: | nikolai.ruhe | ||
| Number: | rdar://16024097 | Date Originated: | 10-Feb-2014 |
| Status: | Open | Resolved: | |
| Product: | OS X SDK | Product Version: | Mac OS 10.9, iOS 7.0 |
| Classification: | Performance | Reproducible: | Always |
A typical pattern in Objective-C class design when implementing to-many relationships is to use a private ivar with mutable container type and to expose the immutable type in the getter. One example for this pattern is NSView's subviews. An NSView's client should never get the private mutable array, so in the getter we create and return an immutable copy. To make this copy cheap it's often a good idea to cache the immutable copy, making it possible to return the same instance over and over. We invalidate the cached copy only when mutating the original instance. It seems that this pattern could easily and transparently be implemented in NSMutableArray, making it available in all places where copies are requested repeatedly. Cached instances could be released weakly or when receiving memory warnings (on iOS). Not only arrays but most Foundation classes that implement NSMutableCopying could benefit from this optimization.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!