{"id":23050,"date":"2018-10-10T15:02:41","date_gmt":"2018-10-10T19:02:41","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23050"},"modified":"2018-10-10T15:02:41","modified_gmt":"2018-10-10T19:02:41","slug":"swift-nil-coalescing-performance-trap","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/10\/10\/swift-nil-coalescing-performance-trap\/","title":{"rendered":"Swift Nil-coalescing Performance Trap"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/se-0231-optional-iteration\/16737\/132\">Ben Cohen<\/a> (via <a href=\"https:\/\/twitter.com\/olebegemann\/status\/1050007572202307585\">Ole Begemann<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/se-0231-optional-iteration\/16737\/132\"><p><code>?? []<\/code> is a significant performance and correctness trap.<\/p><p>Not because <code>[]<\/code> creates an array unnecessarily (it doesn&rsquo;t, the empty array is a static singleton in the standard library via a performance hack that gives me heartburn).<\/p><p>It&rsquo;s because when the array <em>isn&rsquo;t<\/em> <code>nil<\/code>, the presence of <code>?? []<\/code> affects the type checker in ways you don&rsquo;t expect[&#8230;]<\/p><p>[&#8230;]<\/p><p>So what does <code>maybeHugeRange?.reversed() ?? []<\/code> do? The <code>ReversedCollection<\/code> answer won&rsquo;t type check, because the rhs of <code>??<\/code> can&rsquo;t be one. So instead it falls back to the version on forward-only collections. That returns an array. So now, just because of that <code>?? []<\/code>, we are attempting to allocate and fill an array of size <code>Int.max<\/code>. Which blows up.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0231-optional-iteration.md\">SE-0231<\/a> (<a href=\"https:\/\/forums.swift.org\/t\/se-0231-optional-iteration\/16737\">Swift Evolution<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0231-optional-iteration.md\"><p>This proposal introduces optional iteration (<code>for?<\/code>) and hence the possibility to use optional sequences as the corresponding attribute in <code>for-in<\/code> loops.<\/p><p>[&#8230;]<\/p><p>The <code>?<\/code> notation here is a semantic emphasis rather than a functional unit: there is no <code>for!<\/code>. Syntactically marking an optional iteration is redundant, however, in constrast to <code>switch<\/code>, nil values are <em>skipped silently<\/em>. Swift strives to follow a style where silent handling of <code>nil<\/code> is acknowledged via the <code>?<\/code> sigil, distinctly reflected in optional chaining. This decision was primarily based on inconsistency and potential confusion that an otherwise left without syntactic changes <code>for-in<\/code> loop could potentially lead to (&ldquo;clarity over brevity&rdquo;).<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ben Cohen (via Ole Begemann): ?? [] is a significant performance and correctness trap.Not because [] creates an array unnecessarily (it doesn&rsquo;t, the empty array is a static singleton in the standard library via a performance hack that gives me heartburn).It&rsquo;s because when the array isn&rsquo;t nil, the presence of ?? [] affects the type [&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":"2018-10-10T19:02:44Z","apple_news_api_id":"2dbaaed4-ced8-4eef-9da6-a54b516e084e","apple_news_api_modified_at":"2018-10-10T19:02:45Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ALbqu1M7YTu-dpqVLUW4ITg","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,138,71,901],"class_list":["post-23050","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23050","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=23050"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23050\/revisions"}],"predecessor-version":[{"id":23051,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23050\/revisions\/23051"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}