{"id":9052,"date":"2014-06-27T16:15:36","date_gmt":"2014-06-27T20:15:36","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9052"},"modified":"2014-08-18T00:21:06","modified_gmt":"2014-08-18T04:21:06","slug":"the-case-for-message-passing-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/06\/27\/the-case-for-message-passing-in-swift\/","title":{"rendered":"The Case for Message Passing in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.buckleyisms.com\/home\/2014\/6\/16\/the-case-for-message-passing-in-swift.html\">Michael Donald Buckley<\/a> (via <a href=\"https:\/\/twitter.com\/olebegemann\/status\/482069997100216320\">Ole Begemann<\/a>):<\/p><blockquote cite=\"http:\/\/www.buckleyisms.com\/home\/2014\/6\/16\/the-case-for-message-passing-in-swift.html\"><p>Apple introduced Swift as &ldquo;Objective-C without the C&rdquo;, but in many ways, Swift is closer to &ldquo;C++ without the C&rdquo; (or <a href=\"http:\/\/mjtsai.com\/blog\/2014\/06\/13\/instantiating-classes-by-name-in-swift\/\">Objective-C without the Smalltalk<\/a>). Swift&rsquo;s generics, for example, are closer to C++&rsquo;s templates than to any feature of Objective-C. One of the more subtle ways that Swift resembles C++ is in how it calls methods. Unlike Objective-C, where methods are called using message passing, Swift uses a vtable for method dispatch, and when the compiler has enough information, inlines the function call directly. While this improves performance over message passing, it trades power and flexibility for performance gains that aren&rsquo;t necessary for most apps.<\/p>\r\n<p>In this article, I will present use cases which are currently impossible or difficult to implement without message passing.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Objective-C isn&rsquo;t going away any time soon, but if Swift succeeds in becoming the dominant language for iOS and Mac development, Objective-C will go away. Eventually the cost of maintaining two languages and runtimes will cease to be worth it. Like Carbon before it, Objective-C will be deprecated, leaving Swift as the only supported language for app development on Apple platforms. In general, I am loathe to argue that my way is the one true way to do things, but since Swift will eventually be the one way to do things, and since now is the time when large changes will be easiest to introduce, I feel compelled to argue that Swift should forswear all but message passing.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>On the Mac, swizzling is used to implement plug-in functionality, even for apps that don&rsquo;t have a plug-in API. This adds features that are very important to power users. Many popular Photoshop and Xcode plug-ins use method swizzling. GPGMail, a plug-in for Mail.app which adds OpenPGP encryption to Mail.app, makes <a href=\"https:\/\/github.com\/GPGTools\/GPGMail\/blob\/dev\/Source\/GMCodeInjector.m\">extensive use of method swizzling<\/a>. This functionality is incredibly important to security-minded individuals, especially in the wake of the Snowden leaks. If Mail.app were rewritten in Swift, this would not be possible.<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/news.ycombinator.com\/item?id=7955175\">Drew Crawford<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/news.ycombinator.com\/item?id=7955175\"><p>Swift already does have a way to opt-in to message passing, it&rsquo;s called the @objc flag (and performSelector:). It works fine, but it&rsquo;s extra work to opt into it and use it. But, I don&rsquo;t think that&rsquo;s bad given that it disables so many useful optimizations.<\/p><p>What may be bad is that the name &ldquo;@objc&rdquo; is a dead giveaway that this is a feature with a deprecation date. So I think we need some other, reflectionish system going forward.<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/news.ycombinator.com\/item?id=7955170\">Michael Donald Buckley<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/news.ycombinator.com\/item?id=7955170\"><p>My argument is that Swift will be primarily used to write high-level apps. For these apps, message passing is almost never a bottleneck. Swift is not going to replace the optimized C and C++ code, especially for the really low-level stuff.<\/p><p>But even if Apple does decide to rewrite all the Core* frameworks et. al. with Swift, there&rsquo;s still an argument that Swift should use message passing by default, and a way to opt in to vtable and direct dispatch for performance-critical code. For higher-level apps, message-passing is a lot less useful if it&rsquo;s opt-in.\r\n<\/p><\/blockquote>\r\n<a href=\"https:\/\/twitter.com\/mpweiher\/status\/482270627198681088\">Marcel<\/a> <a href=\"https:\/\/twitter.com\/mpweiher\/status\/482270871395266560\">Weiher<\/a><a href=\"https:\/\/twitter.com\/mpweiher\/status\/482271232273178624\">:<\/a>\r\n<blockquote><p>To me it looks like replacing ObjC and the frameworks (over time) and the style that puts everyone on an equal footing.  So instead of having a language where anyone can build something like Core Data, you will have a language with built-in support for Core Data. Apple&rsquo;s Core Data, that is, and you <em>will<\/em> like it. Just another tightening of the screws (App Store, App Store&#8211;only APIs despite promises).<\/p><\/blockquote>\r\n<p>Update (2014-07-05): <a href=\"https:\/\/devforums.apple.com\/message\/992895#992895\">Michael Donald Buckley<\/a> (via <a href=\"http:\/\/mjtsai.com\/blog\/2014\/06\/27\/the-case-for-message-passing-in-swift\/#comment-2095391\">Sash<\/a>):<\/p>\r\n<blockquote cite=\"https:\/\/devforums.apple.com\/message\/992895#992895\"><p>In the rare occsion that objc_msgSend is a bottleneck in an Objective-C app, there are techniques like IMP caching that can improve performance, and that&rsquo;s really the kind of thing I&rsquo;m advocating here. Give developers some way to opt in to vtables when there&rsquo;s a real performance problem, but allow the greater flexibility of message sending by default. It&rsquo;s possible to start with message passing and optimize when needed, but it&rsquo;s not possible to start with vtable dispatch and use message sending when needed.<\/p>\r\n<p>Message sending allows many problems to be solved with much less code. This increases programmer producivity and lowers maintenence costs. Since message sending is fast enough in most cases, the end user sees no difference between vtables and message sending, so Swift should use the solution that results in better programmer productivity.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Michael Donald Buckley (via Ole Begemann):Apple introduced Swift as &ldquo;Objective-C without the C&rdquo;, but in many ways, Swift is closer to &ldquo;C++ without the C&rdquo; (or Objective-C without the Smalltalk). Swift&rsquo;s generics, for example, are closer to C++&rsquo;s templates than to any feature of Objective-C. One of the more subtle ways that Swift resembles C++ [&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,54,71,901],"class_list":["post-9052","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-message-passing","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9052","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=9052"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9052\/revisions"}],"predecessor-version":[{"id":9331,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9052\/revisions\/9331"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}