{"id":12751,"date":"2015-11-06T11:13:48","date_gmt":"2015-11-06T16:13:48","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12751"},"modified":"2015-11-06T11:15:12","modified_gmt":"2015-11-06T16:15:12","slug":"how-swift-implements-generics","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/11\/06\/how-swift-implements-generics\/","title":{"rendered":"How Swift Implements Generics"},"content":{"rendered":"<p><a href=\"https:\/\/www.reddit.com\/r\/swift\/comments\/3r4gpt\/how_is_swift_generics_implemented\/cwlo64w\">Chris Lattner<\/a> (via <a href=\"http:\/\/ericasadun.com\/2015\/10\/28\/todays-urls\/\">Erica Sadun<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.reddit.com\/r\/swift\/comments\/3r4gpt\/how_is_swift_generics_implemented\/cwlo64w\"><p>The semantic model of swift generics is that they use runtime dispatch through &ldquo;witness tables&rdquo; provided by the protocol conformances of the generic types. This model allows for fast -O0 compiles and separate compilation of generics.<\/p><p>The problem with this model is that actually relying on this for everything would produce code that runs very slowly. To solve this problem, the optimizer uses heuristic-driven generic specialization that does code duplication where it thinks that it is profitable and sensible.<\/p><p>The way to contrast C++ and Swift is: C++ eagerly duplicates code in the frontend (and hopefully the optimizer can eliminate some of the copies later, with LTO&#8230;). Swift does not generate any copies in the front-end, but does generate them in the optimizer.\n<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.reddit.com\/r\/swift\/comments\/3q2zqa\/when_is_swift_going_open_source\/cwbu0be\">Also<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.reddit.com\/r\/swift\/comments\/3q2zqa\/when_is_swift_going_open_source\/cwbu0be\"><p>We are still on track to open source Swift (including Linux support) &ldquo;by the end of 2015&rdquo; as promised, more details will come out when they can.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Chris Lattner (via Erica Sadun): The semantic model of swift generics is that they use runtime dispatch through &ldquo;witness tables&rdquo; provided by the protocol conformances of the generic types. This model allows for fast -O0 compiles and separate compilation of generics.The problem with this model is that actually relying on this for everything would produce [&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":[326,255,46,74,138,71,901],"class_list":["post-12751","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c-plus-plus","tag-compiler","tag-languagedesign","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\/12751","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=12751"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12751\/revisions"}],"predecessor-version":[{"id":12755,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12751\/revisions\/12755"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}