My argument was simple – there is no need to reinvent the wheel with brand new implementations of NS* classes when the CF* counterparts are available. I’ve always had this idea of Foundation being a thin wrapper on CoreFoundation. Knowing its internal storage _NSArrayM can provide much more efficient implementation. For instance, while NSArray provides the default implementation for - countByEnumeratingWithState:objects:count: method of NSFastEnumeration protocol, _NSArrayM has its own code-path as well. However, where it makes sense _NSArrayM does reimplement some of its superclasses' methods. One of the most obvious drawbacks of using linear memory is that inserting an element at index 0 requires shifting all the other elements via means of _NSArrayM : NSMutableArray while ( count >= 0 ) While arrays and pointers are not the same (see “Expert C Programming” or this article), it’s not much of an abuse to consider “ malloc-ed block of memory” as an array. It boils down to a continuous segment of memory that can be easily read from and written into. The Problem of Plain Old C ArraysĮvery self-respecting programmer knows how C arrays work. The following study is based on iOS 7.0 SDK.Īs usual, you can find the accompanying Xcode project on my GitHub. Regardless of those caveats, it’s worth peeking under the hood of NSMutableArray and figuring out how it works and what can be expected of it. They’re subject to change at any time, both in terms of underlying subclasses and their ivar layouts, as well as underpinning algorithms and data structures. Implementation details of NSMutableArray are private for a reason. Once we’re through the low level details, I present the non obvious characteristics of the class. There is an entire section discussing the ARM64 assembly, so if you find that boring then do not hesitate to skip it. Since I more or less describe the complete process I used to investigate NSMutableArray, this post is fairly technical. I find the actual memory manipulation details fascinating and this is why this article focuses on mutable arrays. It doesn’t change the fact that they’re quite dull – their contents can’t be modified. Don’t get me wrong, immutable arrays certainly provide enormous benefits: not only are they thread safe but also copying them is essentially free. I’ve always wondered how NSMutableArray works internally.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |