{"id":13236,"date":"2016-01-08T17:33:16","date_gmt":"2016-01-08T22:33:16","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13236"},"modified":"2016-05-03T11:54:35","modified_gmt":"2016-05-03T15:54:35","slug":"exploring-swift-arrays-implementation","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/01\/08\/exploring-swift-arrays-implementation\/","title":{"rendered":"Exploring Swift Array&rsquo;s Implementation"},"content":{"rendered":"<p><a href=\"http:\/\/ankit.im\/swift\/2016\/01\/08\/exploring-swift-array-implementation\/\">Ankit Aggarwal<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/ankit.im\/swift\/2016\/01\/08\/exploring-swift-array-implementation\/\"><p>Swift declares a C struct in its SwiftShim&rsquo;s module inside GlobalObject.h called <code>_SwiftArrayBodyStorage<\/code><\/p><p>[&#8230;]<\/p><ul><li><code>count<\/code> is the number of elements currently stored in the buffer<\/li><li><p><code>_capacityAndFlags<\/code> is used to store two things:<\/p><ul><li>first is the capacity of the buffer<\/li><li>second &ldquo;Is the Element type bitwise-compatible with some Objective-C class?&rdquo;<\/li><\/ul><\/li><\/ul><\/blockquote>\r\n\r\n<p>Via <a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/685207135232262144\">David Smith<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/685207135232262144\"><p>Apparently C-like performance, memory safety, resizing, copy-on-write, and ObjC bridging all together is hard<\/p><\/blockquote>\r\n\r\n<p>The <code>subscript<\/code> implementation in <code>_ContiguousArrayBuffer<\/code> has an interesting note:<\/p>\r\n<blockquote cite=\"http:\/\/ankit.im\/swift\/2016\/01\/08\/exploring-swift-array-implementation\/\"><p>Manually swap because it makes the ARC optimizer happy.<\/p><\/blockquote>\r\n<p>The method is also marked with a <a href=\"https:\/\/twitter.com\/praeclarum\/status\/685210747211628544\">mysterious<\/a> <code>nonmutating<\/code> <a href=\"https:\/\/twitter.com\/andy_matuschak\/status\/522529881717215232\">keyword<\/a>, which the Swift language guide mentions but does not explain. This could be an optimization to avoid unnecessary writebacks or a way to make the compiler enforce that the setter does not actually change any properties of the structure (just the contents of the memory they point to).<\/p>\r\n\r\n<p>Update (2016-05-03): <a href=\"http:\/\/jasdev.me\/nonmutating\">Jasdev Singh<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/jasdev.me\/nonmutating\">\r\n<p>Stepping through Sidney\u2019s example, we can see how <code class=\"highlighter-rouge\">nonmutating<\/code> signals that a setter doesn\u2019t modify the containing instance, but instead has global side effects.<\/p>\r\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ankit Aggarwal: Swift declares a C struct in its SwiftShim&rsquo;s module inside GlobalObject.h called _SwiftArrayBodyStorage[&#8230;]count is the number of elements currently stored in the buffer_capacityAndFlags is used to store two things:first is the capacity of the buffersecond &ldquo;Is the Element type bitwise-compatible with some Objective-C class?&rdquo; Via David Smith: Apparently C-like performance, memory safety, resizing, [&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":[55,46,74,138,71,901,943],"class_list":["post-13236","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-languagedesign","tag-opensource","tag-optimization","tag-programming","tag-swift-programming-language","tag-swift-runtime"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13236","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=13236"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13236\/revisions"}],"predecessor-version":[{"id":14318,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13236\/revisions\/14318"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}