{"id":16373,"date":"2016-11-14T14:53:57","date_gmt":"2016-11-14T19:53:57","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16373"},"modified":"2016-11-15T13:44:00","modified_gmt":"2016-11-15T18:44:00","slug":"swift-copy-on-write-psa-mutating-dictionary-entries","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/11\/14\/swift-copy-on-write-psa-mutating-dictionary-entries\/","title":{"rendered":"Swift Copy-on-Write PSA: Mutating Dictionary Entries"},"content":{"rendered":"<p><a href=\"http:\/\/rosslebeau.com\/2016\/swift-copy-write-psa-mutating-dictionary-entries\">Ross LeBeau<\/a>:<\/p>\n<blockquote cite=\"http:\/\/rosslebeau.com\/2016\/swift-copy-write-psa-mutating-dictionary-entries\"><p>Swift&rsquo;s copy-on-write system usually helps efficiency by delaying the copying of structs until it&rsquo;s actually needed. However, as shown here, you can run into some pitfalls if you aren&rsquo;t vigilant of what&rsquo;s going on under the hood.<\/p>\n<p>[&#8230;]<\/p><p>The &ldquo;gotcha&rdquo; here is that at the time of <code>append<\/code>, the array is referenced twice &#8211; once by the dictionary, and once by the variable <code>items<\/code>. This triggers copy-on-write, meaning that the entire array is copied to a new location in memory, even though your intention is to mutate a single array.<\/p><\/blockquote>\n<p>This is a doubly weird example in Swift because you can&rsquo;t actually mutate dictionary entries where the value is a struct such as an array. You have to set the array again after appending or wrap the array in a reference type.<\/p>\n<p>Update (2016-11-15): See also: <a href=\"https:\/\/www.cocoawithlove.com\/blog\/2016\/09\/22\/deque.html\">Optimizing a copy-on-write double-ended queue in Swift<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ross LeBeau: Swift&rsquo;s copy-on-write system usually helps efficiency by delaying the copying of structs until it&rsquo;s actually needed. However, as shown here, you can run into some pitfalls if you aren&rsquo;t vigilant of what&rsquo;s going on under the hood. [&#8230;]The &ldquo;gotcha&rdquo; here is that at the time of append, the array is referenced twice &#8211; [&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":[46,138,71,901],"class_list":["post-16373","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16373","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=16373"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16373\/revisions"}],"predecessor-version":[{"id":16385,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16373\/revisions\/16385"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}