{"id":20373,"date":"2018-01-31T14:38:44","date_gmt":"2018-01-31T19:38:44","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20373"},"modified":"2018-01-31T14:38:44","modified_gmt":"2018-01-31T19:38:44","slug":"swift-imports-fixed-size-c-arrays-as-tuples","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/01\/31\/swift-imports-fixed-size-c-arrays-as-tuples\/","title":{"rendered":"Swift Imports Fixed-size C Arrays As Tuples"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2017\/12\/swift-imports-fixed-size-c-arrays-as-tuples\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2017\/12\/swift-imports-fixed-size-c-arrays-as-tuples\/\">\n<p>The Swift equivalent of the C type <code>float[4]<\/code> would be <code>(Float, Float, Float, Float)<\/code>. This has the benefit of incurring no bridging overhead because the Swift compiler can lay out tuples in a C-compatible way.<\/p>\n<p>[&#8230;]<\/p>\n<p>Swift&rsquo;s <a href=\"https:\/\/swift.org\/compiler-stdlib\/#compiler-architecture\">Clang importer<\/a> sees the <code>char[256]<\/code> declarations and turns them into 256-tuples(!).<\/p>\n<p>[&#8230;]<\/p>\n<p>I did a quick test: my version of <code>_FixedArray256<\/code> (approximately 1,600 lines of generated Swift code) takes about 8 seconds to compile and adds nearly 750 KB to the compiled binary (with optimizations enabled). The convenience doesn&rsquo;t come for free.<\/p>\n<p>[&#8230;]<\/p>\n<p>Instead, let&rsquo;s just take the idea of obtaining a pointer to the tuple&rsquo;s storage and using that to initialize a string.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: The Swift equivalent of the C type float[4] would be (Float, Float, Float, Float). This has the benefit of incurring no bridging overhead because the Swift compiler can lay out tuples in a C-compatible way. [&#8230;] Swift&rsquo;s Clang importer sees the char[256] declarations and turns them into 256-tuples(!). [&#8230;] I did a quick [&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":[45,46,71,901],"class_list":["post-20373","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20373","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=20373"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20373\/revisions"}],"predecessor-version":[{"id":20374,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20373\/revisions\/20374"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}