{"id":8880,"date":"2014-05-25T23:40:36","date_gmt":"2014-05-26T03:40:36","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8880"},"modified":"2014-05-26T11:05:53","modified_gmt":"2014-05-26T15:05:53","slug":"tail-recursion-objective-c-and-arc","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/05\/25\/tail-recursion-objective-c-and-arc\/","title":{"rendered":"Tail Recursion, Objective-C, and ARC"},"content":{"rendered":"<p><a href=\"http:\/\/devetc.org\/code\/2014\/05\/24\/tail-recursion-objc-and-arc.html\">Jonathon Mah<\/a>:<\/p>\n<blockquote cite=\"http:\/\/devetc.org\/code\/2014\/05\/24\/tail-recursion-objc-and-arc.html\"><p>The problem is revealed: there <strong>is<\/strong> work to be done after the recursive call: automatic reference counting inserted a release call for the value returned from <code>node.next<\/code>.\nIf we were writing this with manual retain\/release, one wouldn&rsquo;t insert any memory management calls into this at all, because <code>-next<\/code> returns an autoreleased object.\nHowever when this is compiled under ARC, calls to <code>objc_retainAutoreleasedReturnValue<\/code> and <code>objc_release<\/code> are inserted to allow for another optimization &mdash; having the return value skip the autorelease pool entirely.\nUnfortunately in this case, it conflicts with tail call optimization.<\/p>\n<p>[&#8230;]<\/p>\n<p>In an ARC environment, tail call optimization (and thus tail recursion) is too fragile. Don&rsquo;t rely on it.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jonathon Mah: The problem is revealed: there is work to be done after the recursive call: automatic reference counting inserted a release call for the value returned from node.next. If we were writing this with manual retain\/release, one wouldn&rsquo;t insert any memory management calls into this at all, because -next returns an autoreleased object. However [&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,54,138,71],"class_list":["post-8880","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-objective-c","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8880","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=8880"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8880\/revisions"}],"predecessor-version":[{"id":8883,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8880\/revisions\/8883"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}