{"id":14748,"date":"2016-06-07T11:30:56","date_gmt":"2016-06-07T15:30:56","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14748"},"modified":"2016-06-07T11:30:56","modified_gmt":"2016-06-07T15:30:56","slug":"mutexes-and-closure-capture-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/06\/07\/mutexes-and-closure-capture-in-swift\/","title":{"rendered":"Mutexes and Closure Capture in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/06\/02\/threads-and-mutexes.html\">Matt Gallagher<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/06\/02\/threads-and-mutexes.html\">\n<p>Using a mutex in Swift isn&rsquo;t particularly difficult but I&rsquo;m going to use the topic to highlight a subtle performance nuisance in Swift: dynamic heap allocation during closure capture. We want our mutex to be fast but passing a closure to execute inside a mutex can reduce the performance by a factor of 10 due to memory allocation overhead.<\/p>\n<p>[&#8230;]<\/p>\n<p>One of the advantages with closure capture is how effortless it seems. Items inside the closure have the same <em>names<\/em> inside and outside the closure and the connection between the two is obvious. When we avoid closure capture and instead try to pass all values in as parameters, we&rsquo;re forced to either rename all our variables or shadow names &#8211; neither of which helps comprehension &#8211; and we still run the risk of accidentally capturing a variable and losing all efficiency anyway.<\/p>\n<p>[&#8230;]<\/p>\n<p>It turns out that despite being a private function in the same file &#8211; where Swift can fully inline the function &#8211; Swift is not eliminating redundant retains and releases on the the <code>PThreadMutex<\/code> parameter (which is a <code>class<\/code> type to avoid breaking the <code>pthread_mutex_t<\/code> by copying it).<\/p>\n<p>We can force the compiler to avoid these retains and releases by making the function an extension on <code>PThreadMutex<\/code>, rather than a free function[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>But despite the speed increase, using a semaphore for a mutex is probably not the best approach for a general mutex. Semaphores are prone to a form of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Priority_inversion\">priority inversion<\/a>.<\/p>\n<\/blockquote>\n<p>Long-term, Swift should be able to keep <code>@noescape<\/code> closures on the stack.<\/p>","protected":false},"excerpt":{"rendered":"<p>Matt Gallagher: Using a mutex in Swift isn&rsquo;t particularly difficult but I&rsquo;m going to use the topic to highlight a subtle performance nuisance in Swift: dynamic heap allocation during closure capture. We want our mutex to be fast but passing a closure to execute inside a mutex can reduce the performance by a factor of [&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,800,880,31,1137,30,1199,138,71,901],"class_list":["post-14748","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-concurrency","tag-grand-central-dispatch-gcd","tag-ios","tag-ios-9","tag-mac","tag-mac-os-x-10-11","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14748","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=14748"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14748\/revisions"}],"predecessor-version":[{"id":14749,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14748\/revisions\/14749"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}