{"id":11903,"date":"2015-08-01T11:36:58","date_gmt":"2015-08-01T15:36:58","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11903"},"modified":"2015-08-03T09:50:32","modified_gmt":"2015-08-03T13:50:32","slug":"swift-array-performance","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/08\/01\/swift-array-performance\/","title":{"rendered":"Swift Array Performance"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/627469657264553984\">Airspeed Velocity<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/AirspeedSwift\/status\/627469657264553984\">\n<p>Your reminder that building arrays with reduce, while fun, is accidentally quadratic.<\/p>\n<\/blockquote>\n<p>I would have thought that extending an array would be optimized to not make a copy.<\/p>\n<p>Update (2015-08-01): <a href=\"https:\/\/twitter.com\/jckarter\/status\/627525103903883264\">Joe Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/627525103903883264\"><p>I don&rsquo;t think <code>+<\/code> is implemented to check for unique referencing at all.<\/p><\/blockquote>\n<p>Update (2015-08-03): <a href=\"http:\/\/airspeedvelocity.net\/2015\/08\/03\/arrays-linked-lists-and-performance\/\">Airspeed Velocity<\/a> (<a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/628038556179501056\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/airspeedvelocity.net\/2015\/08\/03\/arrays-linked-lists-and-performance\/\"><p>I was surprised at how surprising some found this. Quite a few people suggested the <code>reduce<\/code> version could be changed to not do the array copy (I don&rsquo;t <em>think<\/em> it can). Some suggested maybe <code>+<\/code> could be optimized so it doesn&rsquo;t perform a copy (I don&rsquo;t think that it could easily, as we&rsquo;ll see shortly).<\/p>\n<p>[&#8230;]<\/p>\n<p>Assuming <code>combine<\/code> here is <code>{ $0 + [transform($1)] }<\/code>, you can see that <code>+<\/code> similarly has no knowledge of the fact that we&rsquo;re actually going to assign the outcome directly to the <code>result<\/code> variable. We know, on inspecting the code, that it&rsquo;ll just be fine to add the right-hand side onto the left-hand side, if that were even possible (in theory it is, since even though the array is passed immutably by value, the buffer is a class and so could be mutated since it has reference semantics). But <code>+<\/code> can&rsquo;t know that from where it sits. It definitely knows it&rsquo;s copy of the left-hand side isn&rsquo;t the only owner of the buffer. There is another owner too: <code>reduce<\/code> holds a copy of <code>result<\/code> &#8211; and is about to throw it away and replace it with a new result, but that&rsquo;s coming <em>after<\/em> <code>+<\/code> has run.<\/p><\/blockquote>\n<p><a href=\"https:\/\/twitter.com\/jckarter\/status\/628064843245850624\">Joe<\/a> <a href=\"https:\/\/twitter.com\/jckarter\/status\/628065844220657664\">Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/628064843245850624\"><p>Swift&rsquo;s parameter convention is callee-release, so isUniquelyRefd can work inside +. It&rsquo;d only succeed if + is the last use.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/628065844220657664\"><p> That optimization could lead to optimizer-dependent algorithmic complexity though, sorta like TCO.<\/p><\/blockquote>\n<p>&ldquo;TCO&rdquo; refers to <a href=\"http:\/\/stackoverflow.com\/questions\/310974\/what-is-tail-call-optimization\">tail call optimization<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Airspeed Velocity: Your reminder that building arrays with reduce, while fun, is accidentally quadratic. I would have thought that extending an array would be optimized to not make a copy. Update (2015-08-01): Joe Groff: I don&rsquo;t think + is implemented to check for unique referencing at all. Update (2015-08-03): Airspeed Velocity (tweet): I was surprised [&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-11903","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\/11903","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=11903"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11903\/revisions"}],"predecessor-version":[{"id":11919,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11903\/revisions\/11919"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}