{"id":8603,"date":"2014-03-27T11:06:46","date_gmt":"2014-03-27T15:06:46","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8603"},"modified":"2014-04-15T20:38:39","modified_gmt":"2014-04-16T00:38:39","slug":"exposing-nsmutablearray","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/03\/27\/exposing-nsmutablearray\/","title":{"rendered":"Exposing NSMutableArray"},"content":{"rendered":"<p><a href=\"http:\/\/ciechanowski.me\/blog\/2014\/03\/05\/exposing-nsmutablearray\/\">Bartosz Ciechanowski<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/ciechanowski.me\/blog\/2014\/03\/05\/exposing-nsmutablearray\/\">\r\n<p>Inserting object at index 0 uses the circular buffer magic to put the newly inserted object at the end of the buffer.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Whenever the buffer gets full, it is reallocated with <strong>1.625<\/strong> times larger size. [&#8230;] Mike Curtiss has <a href=\"https:\/\/twitter.com\/mcurtiss\/status\/441432739523198977\">provided<\/a> a <a href=\"https:\/\/github.com\/facebook\/folly\/blob\/master\/folly\/docs\/FBVector.md#memory-handling\">very good explanation<\/a> of why making resizing factor equal to 2 is suboptimal.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>This is a shocker &#8211; <code>__NSArrayM<\/code> never reduces its size!<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>I&rsquo;ve always had this idea of Foundation being a thin wrapper on CoreFoundation. My argument was simple &#8211; there is no need to reinvent the wheel with brand new implementations of NS* classes when the CF* counterparts are available. I was shocked to realize neither <code>NSArray<\/code> nor <code>NSMutableArray<\/code> have anything in common with <code>CFArray<\/code>.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Basically, <code>CFArray<\/code> moves the memory around to accommodate the changes in the most efficient fashion, similarly to how <code>__NSArrayM<\/code> does its job. However, the <code>CFArray<\/code> does <em>not<\/em> use a circular buffer! Instead it has a larger buffer padded with zeros from both ends which makes enumeration and fetching the correct object much easier. Adding elements at either end simply eats up the remaining padding.<\/p>\r\n<\/blockquote>\r\n<p>Update (2014-04-14): David Smith notes that the Core Foundation creation functions will often <a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/455555795430547456\">now give you NS objects<\/a>, whereas it used to be the reverse. Removing the Core Foundation layer yielded a <a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/455558455453945857\">10&#8211;40% speed improvement<\/a>. Marcel Weiher notes that the NS collections were <a href=\"https:\/\/twitter.com\/mpweiher\/status\/455564277760663552\">originally faster<\/a> and Core Foundation was a <a href=\"https:\/\/twitter.com\/mpweiher\/status\/455611759538483200\">regression<\/a>, for <a href=\"https:\/\/twitter.com\/mpweiher\/status\/455572985085427712\">political reasons<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Bartosz Ciechanowski: Inserting object at index 0 uses the circular buffer magic to put the newly inserted object at the end of the buffer. [&#8230;] Whenever the buffer gets full, it is reallocated with 1.625 times larger size. [&#8230;] Mike Curtiss has provided a very good explanation of why making resizing factor equal to 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[770,69,861,859,54,138,71],"class_list":["post-8603","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-assembly-language","tag-cocoa","tag-core-foundation","tag-nsarray","tag-objective-c","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=8603"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8603\/revisions"}],"predecessor-version":[{"id":8714,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8603\/revisions\/8714"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}