{"id":9091,"date":"2014-07-06T14:35:37","date_gmt":"2014-07-06T18:35:37","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9091"},"modified":"2016-05-21T18:51:45","modified_gmt":"2016-05-21T22:51:45","slug":"secrets-of-swifts-speed","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/07\/06\/secrets-of-swifts-speed\/","title":{"rendered":"Secrets of Swift&rsquo;s Speed"},"content":{"rendered":"<p><a href=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2014-07-04-secrets-of-swifts-speed.html\">Mike Ash<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2014-07-04-secrets-of-swifts-speed.html\"><p>A simple array index is quite a bit faster than <code>objc_msgSend<\/code>, even with its fancy caching and finely tuned assembly code, so this is a nice performance win.<\/p>\r\n<p>The call to <code>testmethod2<\/code> is even nicer. Because it\u2019s declared <code>@final<\/code>, the compiler knows that nothing can override it. No matter what happens, the call will always go to the implementation in <code>Class<\/code>. Thus the compiler doesn\u2019t even need to emit a vtable lookup. It can just make a normal function call that goes straight to <code>__TFC9speedtest5Class11testmethod2fS0_FT_T_<\/code>, which is the mangled name of the testmethod2 implementation in my test case.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>The different method call semantics allow speed improvements beyond simply using faster dispatch techniques. Because the compiler can better understand where control goes, it can optimize the code better. It can potentially inline method calls or even eliminate them entirely.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Swift omits the <code>_cmd<\/code> parameter, freeing up an additional argument register for more useful purposes. For methods that take several explicit parameters (three or more for ARM, five or more for x86-64, and seven or more for ARM64), this means a small performance win on every call due to better use of argument registers.<\/p>\r\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Mike Ash: A simple array index is quite a bit faster than objc_msgSend, even with its fancy caching and finely tuned assembly code, so this is a nice performance win. The call to testmethod2 is even nicer. Because it\u2019s declared @final, the compiler knows that nothing can override it. No matter what happens, the call [&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,966,138,71,901],"class_list":["post-9091","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-message-passing","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9091","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=9091"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9091\/revisions"}],"predecessor-version":[{"id":14599,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9091\/revisions\/14599"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}