{"id":32104,"date":"2021-04-12T17:00:58","date_gmt":"2021-04-12T21:00:58","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=32104"},"modified":"2021-04-15T14:33:31","modified_gmt":"2021-04-15T18:33:31","slug":"high-performance-numeric-programming-with-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/04\/12\/high-performance-numeric-programming-with-swift\/","title":{"rendered":"High Performance Numeric Programming With Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.fast.ai\/2019\/01\/10\/swift-numerics\/\">Jeremy Howard<\/a> (via <a href=\"https:\/\/twitter.com\/depth42\/status\/1190185136404467713\">Frank Illenberger<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.fast.ai\/2019\/01\/10\/swift-numerics\/\"><p>I&rsquo;ve managed to create a couple of libraries that can achieve the same speed as carefully optimized vectorized C code, whilst being concise and easy to use. [&#8230;] I will include examples mainly from my <a href=\"https:\/\/github.com\/jph00\/BaseMath\">BaseMath<\/a> library, which provides generic math functions for <code>Float<\/code> and <code>Double<\/code>, and optimized versions for various collections of them.<\/p><p>[&#8230;]<\/p><p>One of the really cool things about Swift is that wrappers like the above have no run-time overhead. As you see, I&rsquo;ve marked them with the <code>inlinable<\/code> attribute, which tells LLVM that it&rsquo;s OK to replace calls to this function with the actual function body. This kind of <em>zero-overhead abstraction<\/em> is one of the most important features of C++; it&rsquo;s really amazing to see it in such a concise and expressive language as Swift.<\/p><p>[&#8230;]<\/p><p>Normally, because Swift has to handle the complexities of COW, it can&rsquo;t fully optimize a loop like this. But by using a pointer instead, we skip those checks, and Swift can run the code at full speed. Note that due to copy-on-write it&rsquo;s possible for the array to move if you assign to it, and it can also move if you do things such as resize it; therefore, you should only grab the pointer at the time you need it.<\/p><p>[&#8230;]<\/p><p>I think this is quite remarkable; we&rsquo;ve been able to create a simple API which is just as fast as the pointer code, but to the class user that complexity is entirely hidden away.<\/p><p>[&#8230;]<\/p><p>I also find Swift&rsquo;s performance is harder to reason about and optimize than C.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/02\/16\/swift-for-tensorflow-canceled\/\">Swift for TensorFlow Canceled<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/11\/08\/swift-numerics\/\">Swift Numerics<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/11\/02\/efficiently-mutating-nested-swift-data-structures\/\">Efficiently Mutating Nested Swift Data Structures<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/07\/fast-safe-mutable-state-in-swift-5\/\">Fast Safe Mutable State in Swift 5<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/06\/why-swifts-copy-on-write-is-safe\/\">Why Swift&rsquo;s Copy-on-Write Is Safe<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/02\/18\/swift-ownership-manifesto\/\">Swift Ownership Manifesto<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/11\/14\/swift-copy-on-write-psa-mutating-dictionary-entries\/\">Swift Copy-on-Write PSA: Mutating Dictionary Entries<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/01\/08\/exploring-swift-arrays-implementation\/\">Exploring Swift Array&rsquo;s Implementation<\/a><\/li>\n<\/ul>\n\n<p id=\"high-performance-numeric-programming-with-swift-update-2021-04-15\">Update (2021-04-15): <a href=\"https:\/\/twitter.com\/NSExceptional\/status\/1381717006827814915\">Tanner Bennett<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/NSExceptional\/status\/1381717006827814915\">\n<p>TIL <code>@inlineable<\/code> is no longer a private attribute<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jeremy Howard (via Frank Illenberger): I&rsquo;ve managed to create a couple of libraries that can achieve the same speed as carefully optimized vectorized C code, whilst being concise and easy to use. [&#8230;] I will include examples mainly from my BaseMath library, which provides generic math functions for Float and Double, and optimized versions for [&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":"2021-04-12T21:01:02Z","apple_news_api_id":"66f8db62-156e-4c3f-9804-310a31f1045d","apple_news_api_modified_at":"2021-04-15T18:33:34Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AZvjbYhVuTD-YBDEKMfEEXQ","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":[259,74,138,71,901],"class_list":["post-32104","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-math","tag-opensource","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32104","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=32104"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32104\/revisions"}],"predecessor-version":[{"id":32148,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32104\/revisions\/32148"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=32104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=32104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=32104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}