{"id":32139,"date":"2021-04-15T14:30:20","date_gmt":"2021-04-15T18:30:20","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=32139"},"modified":"2026-02-20T09:10:01","modified_gmt":"2026-02-20T14:10:01","slug":"collecting-concurrentperformiterations-results-in-a-swift-array","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/04\/15\/collecting-concurrentperformiterations-results-in-a-swift-array\/","title":{"rendered":"Collecting concurrentPerform(iterations:) Results in a Swift Array"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/1382411986152030209\">David Smith<\/a> (<a href=\"https:\/\/web.archive.org\/web\/20210414191512\/https:\/\/twitter.com\/Catfish_Man\/status\/1382411986152030209\">archive<\/a>):<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/1382411986152030209\">\n<p>Useful pattern for aggregating the results of parallel work in Swift:<\/p>\n<pre>let result = Array(unsafeUninitializedCapacity: count) { (buffer) in\n  DispatchQueue.concurrentPerform(iterations: count) { (idx) in\n    buffer[idx] = processItem(idx)\n  }\n}<\/pre>\n<p>Avoids making an extra buffer copy<\/p>\n<p>If you make the <code>Array<\/code> up front and try to operate directly on it instead of the <code>UnsafeMutableBufferPointer<\/code> in that initializer, each thread will get its own copy due to copy-on-write, which generally is not what you wanted.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/1382421706220867586\">David Smith<\/a> (<a href=\"https:\/\/web.archive.org\/web\/20210414195329\/https:\/\/twitter.com\/Catfish_Man\/status\/1382421706220867586\">archive<\/a>):<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/1382421706220867586\">\n<p>I would not trust it [with small array elements]. Aligned word-sized non-float things are your friends when dealing with concurrency. If you&rsquo;re not sure, try TSAN, and consider just using a lock.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/04\/12\/high-performance-numeric-programming-with-swift\/\">High Performance Numeric Programming With Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/03\/16\/underused-and-overused-gcd-patterns\/\">Underused and Overused GCD Patterns<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/05\/swift-5-exclusivity-enforcement\/\">Swift 5 Exclusivity Enforcement<\/a><\/li>\n<\/ul>\n\n<p id=\"collecting-concurrentperformiterations-results-in-a-swift-array-update-2026-02-20\">Update (<a href=\"#collecting-concurrentperformiterations-results-in-a-swift-array-update-2026-02-20\">2026-02-20<\/a>): For objects and other types that use ARC, directly assigning into the buffer as shown above will crash in <code>swift_unknownObjectRelease()<\/code> as Swift tries to release the garbage memory that was in that position in the buffer. You should instead use:<\/p>\n<pre>buffer.initializeElement(at: idx, to: processItem(idx))<\/pre>\n<p>The original tweets have been deleted, so perhaps I&rsquo;m missing something from the context, but the part about each thread getting its own copy if you make an <code>Array<\/code> up front does not sound correct to me.<\/p>","protected":false},"excerpt":{"rendered":"<p>David Smith (archive): Useful pattern for aggregating the results of parallel work in Swift: let result = Array(unsafeUninitializedCapacity: count) { (buffer) in DispatchQueue.concurrentPerform(iterations: count) { (idx) in buffer[idx] = processItem(idx) } } Avoids making an extra buffer copy If you make the Array up front and try to operate directly on it instead of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-04-15T18:30:25Z","apple_news_api_id":"9d79af2f-d48a-410a-a9e1-7894259547ae","apple_news_api_modified_at":"2026-02-20T14:10:05Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AnXmvL9SKQQqp4XiUJZVHrg","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,800,880,31,1837,30,1891,138,901],"class_list":["post-32139","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-concurrency","tag-grand-central-dispatch-gcd","tag-ios","tag-ios-14","tag-mac","tag-macos-11-0","tag-optimization","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32139","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=32139"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32139\/revisions"}],"predecessor-version":[{"id":51040,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32139\/revisions\/51040"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=32139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=32139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=32139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}