{"id":8253,"date":"2013-12-09T17:33:59","date_gmt":"2013-12-09T22:33:59","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8253"},"modified":"2015-02-01T19:01:52","modified_gmt":"2015-02-02T00:01:52","slug":"the-foundation-collection-classes","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2013\/12\/09\/the-foundation-collection-classes\/","title":{"rendered":"The Foundation Collection Classes"},"content":{"rendered":"<p><a href=\"http:\/\/www.objc.io\/issue-7\/collections.html\">Peter Steinberger<\/a> has a good survey of the Cocoa collection classes, with some interesting performance information:<\/p>\r\n<blockquote cite=\"http:\/\/www.objc.io\/issue-7\/collections.html\"><p>Notice that <code>NSPointerArray<\/code> requires more than <strong>250x (!)<\/strong> more time than <code>NSMutableArray<\/code>. This is really surprising and unexpected. Tracking memory is harder and it&rsquo;s likely that <code>NSPointerArray<\/code> is more efficient here, but since we use one shared instance for <code>NSNull<\/code> to mark empty objects, there shouldn&rsquo;t be much overhead except pointers.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>The eviction method of <code>NSCache<\/code> is non-deterministic and not documented. It&rsquo;s not a good idea to put in super-large objects like images that might fill up your cache faster than it can evict itself. (This was the case of many memory-related crashes in <a href=\"http:\/\/PSPDFKit.com\">PSPDFKit<\/a>, where we initially used <code>NSCache<\/code> for storing pre-rendered images of pages, before switching to custom caching code based on a LRU linked list.)<\/p><\/blockquote>\r\n<p>Not in the article, but related, is <a href=\"https:\/\/alpha.app.net\/cparnot\/post\/16901357\">Charles Parnot&rsquo;s recent observation<\/a> that the <a href=\"https:\/\/developer.apple.com\/library\/mac\/releasenotes\/Foundation\/RN-FoundationOlderNotes\/\">Mountain Lion release notes<\/a> warn against using <code>+[NSMapTable weakToStrongObjectsMapTable]<\/code>:<\/p>\r\n<blockquote cite=\"https:\/\/developer.apple.com\/library\/mac\/releasenotes\/Foundation\/RN-FoundationOlderNotes\/\"><p>However, weak-to-strong NSMapTables are not currently recommended, as the strong values for weak keys which get zero\u2019d out do not get cleared away (and released) until\/unless the map table resizes itself.<\/p><\/blockquote>\r\n<p>Update (2013-12-09): <code>NSMapTable<\/code> is an important building block, so it&rsquo;s a mystery to me why Apple hasn&rsquo;t gotten it right yet. It seems like it would be straightforward to implement clearing of the values using associated objects. Perhaps Apple is waiting until true weak-deallocation notifications are added to the runtime?<\/p>","protected":false},"excerpt":{"rendered":"<p>Peter Steinberger has a good survey of the Cocoa collection classes, with some interesting performance information: Notice that NSPointerArray requires more than 250x (!) more time than NSMutableArray. This is really surprising and unexpected. Tracking memory is harder and it&rsquo;s likely that NSPointerArray is more efficient here, but since we use one shared instance for [&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":[69,31,30,571,54,71],"class_list":["post-8253","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-mac","tag-memory-management","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8253","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=8253"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8253\/revisions"}],"predecessor-version":[{"id":10652,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8253\/revisions\/10652"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}