<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 9, 2016, at 10:28 AM, Jacob Bandes-Storch via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Mon, May 9, 2016 at 10:25 AM, Joe Groff via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br class="">
> On May 7, 2016, at 10:39 AM, Austin Zheng via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class="">
><br class="">
> Hello Swift users,<br class="">
><br class="">
> I wanted to run something past you folks and get some opinions/feedback.<br class="">
><br 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: <a href="https://gist.github.com/austinzheng/d6c674780a58cb63832c4df3f809e683" rel="noreferrer" target="_blank" class="">https://gist.github.com/austinzheng/d6c674780a58cb63832c4df3f809e683</a><br class="">
><br class="">
> At line 26 we have the following code:<br class="">
><br class="">
> result.append(begin == eos ? "" : String(cs[begin..<end.successor()]))<br class="">
><br 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.<br class="">
><br 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:<br class="">
><br class="">
> init?(_: String.UTF16View)<br class="">
><br class="">
> However, the actual initializer being called was this String initializer in the Mirror code:<br class="">
><br class="">
> public init<Subject>(_ instance: Subject)<br class="">
><br 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.<br class="">
><br 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.<br class="">
<br class="">
</span>This definitely strikes me as a problem. The String<T>(_:) constructor is very easy to call by accident if you're trying to hit another unlabeled initializer. It also strikes me as not particularly "value-preserving", since stringifying many types loses information. Perhaps we should propose giving it a label, String(printing:) maybe?<br class=""></blockquote><div class=""><br class=""></div><div class="">+1</div></div></div></div></div></blockquote><div><br class=""></div>+1</div><div><br class=""></div><div> - Daniel</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br class="">
-Joe<br class="">
<div class="HOEnZb"><div class="h5">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
</div></div></blockquote></div><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=""></body></html>