<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Austin,<div class=""><br class=""></div><div class="">I further “swiftyfied” the code to this (swift-DEVELOPMENT-SNAPSHOT-2016-05-03-a):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Foundation</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(39, 42, 216);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> N = </span><span style="font-variant-ligatures: no-common-ligatures" class="">1_000_000</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> generateTestData() -&gt; [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">] {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">..&lt;</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">N</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">",,abc, 123&nbsp; ,x, , more more more,\u{A0}and yet more, "</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> splitAndTrim(s: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">, sep: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -&gt; [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">] {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> s.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">split</span><span style="font-variant-ligatures: no-common-ligatures" class="">(separator: sep, omittingEmptySubsequences: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">false</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .</span><span style="font-variant-ligatures: no-common-ligatures" class="">map</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { $0.</span><span style="font-variant-ligatures: no-common-ligatures" class="">trimmingCharacters</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">: .</span><span style="font-variant-ligatures: no-common-ligatures" class="">whitespacesAndNewlines</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()) }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> doSplits(data: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">]) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> data.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">reduce</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">) { $0 + </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">splitAndTrim</span><span style="font-variant-ligatures: no-common-ligatures" class="">(s: $1, sep: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">","</span><span style="font-variant-ligatures: no-common-ligatures" class="">)).</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> data = </span><span style="font-variant-ligatures: no-common-ligatures" class="">generateTestData</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> start = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSDate</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> sum = </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">doSplits</span><span style="font-variant-ligatures: no-common-ligatures" class="">(data: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">data</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"elapsed: </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSDate</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">().</span><span style="font-variant-ligatures: no-common-ligatures" class="">timeIntervalSince</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">start</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"sum: </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">sum</span><span style="font-variant-ligatures: no-common-ligatures" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><br class=""></span></div><div class="">I didn’t expect it to run as fast as the original (or even faster) but the code above takes 13 seconds to run… But that’s just an aside :)</div><div class=""><br class=""></div><div class="">To actually come back to the problem you mentioned:</div><div class=""><br class=""></div><div class="">You can disambiguate the initializer to call in the following ways (probably there are other ways):</div><div class=""><br class=""></div><div class="">Add an ! after the init call since the one which should be used is failable (the one that uses reflection does not), so the compiler chooses the “right” one.</div><div class=""><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">result.</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">append</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">(begin == eos ? </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">""</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> : </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">String</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">(cs[begin..&lt;end.</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">successor</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">()])!)</span>&nbsp;</div><div class=""><br class=""></div><div class="">Or you can reference the initializer and use it afterwards (more future-proof):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> utf16View = cs[begin..&lt;end.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">successor</span><span style="font-variant-ligatures: no-common-ligatures" class="">()]</span></div><div style="margin: 0px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> initializer = </span><span style="font-variant-ligatures: no-common-ligatures" class="">String</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures" class="">String</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">UTF16View</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> -&gt; </span><span style="font-variant-ligatures: no-common-ligatures" class="">String</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">?)</span></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">result.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span><span style="font-variant-ligatures: no-common-ligatures" class="">(begin == eos ? </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">""</span><span style="font-variant-ligatures: no-common-ligatures" class=""> : initializer(utf16View)!)</span></div><div class=""><br class=""></div><div class="">This reduces the time to 2.2 seconds on my machine (from ~2.9), but that’s still far from 0.8.</div><div class=""><br class=""></div></div></div></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">What do other people think? Is this something worth worrying about, or is it so rare that it shouldn't matter? Also, any suggestions as to how that code sample might be improved would be appreciated - my naive first attempt wasn't any better.</div></div></blockquote></div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div></div></div><div class="">It might be very surprising behavior and we could think about adding an external label to all those initializers that cause trouble. The performance is of course a little worrying for a&nbsp;zero-cost abstractions language, but I think there are currently more important goals (getting the design right, etc.). We are still at the early stages with a lot of stuff changing and I think when the syntax, ABI, etc. are settled, performance / optimization will get more attention :)</div><div class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">- Dennis</div></div>

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On May 7, 2016, at 7:39 PM, Austin Zheng via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello Swift users,<div class=""><br class=""></div><div class="">I wanted to run something past you folks and get some opinions/feedback.</div><div class=""><br class=""></div><div class="">About a month ago on Hacker News I saw someone commenting about how Swift's string-handling code was unbearably slow (3 seconds to run a code sample, vs. 0.8 in Java). I asked him to provide the code, and he obliged. Unfortunately, I didn't have time to dig into it until this morning. The code in its entirety can be found here:&nbsp;<a href="https://gist.github.com/austinzheng/d6c674780a58cb63832c4df3f809e683" class="">https://gist.github.com/austinzheng/d6c674780a58cb63832c4df3f809e683</a></div><div class=""><br class=""></div><div class="">At line 26 we have the following code:</div><div class=""><br class=""></div><div class=""><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class="">result</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">.</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class="">append(begin </span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">==</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class=""> eos </span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">?</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class=""> </span><span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);"><span class="pl-pds" style="box-sizing: border-box;">"</span><span class="pl-pds" style="box-sizing: border-box;">"</span></span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class=""> </span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">:</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class=""> </span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">String</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class="">(cs[begin</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">..&lt;</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class="">end</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);">successor</span><span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-variant-ligatures: normal; font-variant-position: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; line-height: 16.8px; white-space: pre; widows: 1; background-color: rgb(255, 255, 255);" class="">()]))</span></div><div class=""><br class=""></div><div class="">'cs' is a UTF16 view into an input string, while 'result' is a [String]. When I profiled the code in Instruments, I noticed that it was spending significant time within the reflection machinery.</div><div class=""><br class=""></div><div class="">It turns out that the initializer to make a String out of a utf16 view looks like this, and I believe this is the initializer the author intended to call:</div><div class=""><br class=""></div><div class=""><a class="toggle-link" style="box-sizing: border-box; color: rgb(153, 153, 153); font-size: 14px; line-height: 18px; font-family: Menlo, Consolas, monospace; display: inline-block; transition: background-color 0.4s ease-in; -webkit-transition: background-color 0.4s ease-in; margin-left: 0px !important; background-position: 0px 0px;"><span class="identifier" style="box-sizing: border-box; color: rgb(111, 65, 167);">init</span>?(<wbr style="box-sizing: border-box;" class="">_: String.UTF16View)</a></div><div class=""><br class=""></div><div class="">However, the actual initializer being called was this String initializer in the Mirror code:</div><div class=""><br class=""></div><div class="">public init&lt;Subject&gt;(_ instance: Subject)</div><div class=""><br class=""></div><div class="">This seems like a tricky gotcha for developers who aren't extremely familiar with both the String and reflection APIs. His code looked reasonable at a first glance and I didn't suspect anything was wrong until I profiled it. Even so, I only made the connection because I recognized the name of the standard library function from poking around inside the source files.</div><div class=""><br class=""></div><div class="">What do other people think? Is this something worth worrying about, or is it so rare that it shouldn't matter? Also, any suggestions as to how that code sample might be improved would be appreciated - my naive first attempt wasn't any better.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Austin</div><div class=""><br class=""></div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>