{"id":43090,"date":"2024-05-06T16:24:41","date_gmt":"2024-05-06T20:24:41","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=43090"},"modified":"2024-05-06T16:24:41","modified_gmt":"2024-05-06T20:24:41","slug":"swifts-native-clocks-are-very-inefficient","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/05\/06\/swifts-native-clocks-are-very-inefficient\/","title":{"rendered":"Swift&rsquo;s Native Clocks Are Very Inefficient"},"content":{"rendered":"<p><a href=\"https:\/\/wadetregaskis.com\/swifts-native-clocks-are-very-inefficient\/\">Wade Tregaskis<\/a> (<a href=\"https:\/\/news.ycombinator.com\/item?id=40262897\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"https:\/\/wadetregaskis.com\/swifts-native-clocks-are-very-inefficient\/\"><p>In a nutshell, the problem is that Swift&rsquo;s <code><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/clock\">Clock<\/a><\/code> protocol has significant overheads by design.  If you look at a time profile of code like this, you&rsquo;ll see things like[&#8230;]<\/p><p>That&rsquo;s a lot of time wasted in function calls and struct initialisation and type conversion and protocol witnesses and all that guff.  The only part that&rsquo;s <em>actually<\/em> retrieving the time is the <code><a href=\"https:\/\/github.com\/apple\/swift\/blob\/625436af05b1cf8f1904096530235489daec9dac\/stdlib\/public\/Concurrency\/Clock.cpp#L30\">swift_get_time<\/a><\/code> call (which is just a wrapper over <code><a href=\"https:\/\/www.manpagez.com\/man\/3\/clock_gettime\/\">clock_gettime<\/a><\/code>, which is just a wrapper over <code><a href=\"https:\/\/www.manpagez.com\/man\/3\/clock_gettime_nsec_np\/\">clock_gettime_nsec_np<\/a>(CLOCK_UPTIME_RAW)<\/code>, which is just a wrapper over <code><a href=\"https:\/\/developer.apple.com\/documentation\/kernel\/1462446-mach_absolute_time\">mach_absolute_time<\/a><\/code>).<\/p><p>[&#8230;]<\/p><p>The downside to calling <code>mach_absolute_time<\/code><em>directly<\/em>, though, is that <a href=\"https:\/\/developer.apple.com\/documentation\/kernel\/1462446-mach_absolute_time#discussion\">it&rsquo;s on Apple&rsquo;s &ldquo;naughty&rdquo; list<\/a> &#x2013; apparently it&rsquo;s been abused for device fingerprinting, so Apple require you to beg for special permission if you want to use it (even though it&rsquo;s used by all these other APIs anyway, as the basis for their implementations, and there&rsquo;s nothing you can get from <code>mach_absolute_time<\/code> that you can&rsquo;t get from them too &#x1F928;).<\/p><\/blockquote>\n<p>This matches my experience that intuition is often wrong regarding Swift performance. Sometimes what seems like it would be a simple virtual call has more overhead than an Objective-C message send. Various dynamic stuff involving checking types\/protocols can also be much slower than with Objective-C. The good news is that <code>Date<\/code> is fast, not even calling down to <code>NSDate<\/code>, and that there&rsquo;s a <a href=\"https:\/\/github.com\/apple\/swift\/pull\/73429\">pull request<\/a> to inline some of this.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/06\/20\/privacy-manifests\/\">Privacy Manifests<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/12\/31\/the-surprising-cost-of-protocol-conformances-in-swift\/\">The Surprising Cost of Checking Protocol Conformances in Swift<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Wade Tregaskis (Hacker News): In a nutshell, the problem is that Swift&rsquo;s Clock protocol has significant overheads by design. If you look at a time profile of code like this, you&rsquo;ll see things like[&#8230;]That&rsquo;s a lot of time wasted in function calls and struct initialisation and type conversion and protocol witnesses and all that guff. [&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":"2024-05-06T20:24:45Z","apple_news_api_id":"b8014518-cd4a-4938-9ec4-6d67061fc25b","apple_news_api_modified_at":"2024-05-06T20:24:45Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AuAFFGM1KSTiexG1nBh_CWw","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":[31,2321,30,2385,138,2581,71,2200,901,364],"class_list":["post-43090","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-17","tag-mac","tag-macos-14-sonoma","tag-optimization","tag-privacy-manifests","tag-programming","tag-swift-concurrency","tag-swift-programming-language","tag-time"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/43090","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=43090"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/43090\/revisions"}],"predecessor-version":[{"id":43091,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/43090\/revisions\/43091"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=43090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=43090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=43090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}