<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Yes. NSString's hash is not the same function as String's hashValue. To make matters more confusing when you call hashValue &nbsp;on NSString type you actually get<br>NSStrings hash.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I have confirmed that we are indeed getting a lot more collisions with the bad Strings &nbsp;and I will be looking into this.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature"><br>Sent from my iPhone</div><div><br>On Mar 4, 2016, at 5:18 AM, Jens Persson &lt;<a href="mailto:jens@bitcycle.com">jens@bitcycle.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">If you make a WrappedString type where you can choose to use .hash instead of .hashValue, then the below test(wrappedStrings) will be fast if .hash is used, and slow if .hashValue is used. Demo-program:<div><div><br></div><div><div>import QuartzCore</div><div>struct WrappedString : Hashable {<br></div><div>&nbsp; &nbsp; let wrapped : String</div><div>&nbsp; &nbsp; init(_ s: String) { wrapped = s }</div><div>&nbsp; &nbsp; var hashValue: Int {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return wrapped.hash // &lt;-- NOTE: Will make test(wrappedStrings) fast.</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // return wrapped.hashValue // &lt;-- NOTE: Will make test(wrappedStrings) slow.</div><div>&nbsp; &nbsp; }</div><div>}</div><div>func ==(lhs: WrappedString, rhs: WrappedString) -&gt; Bool { return lhs.wrapped == rhs.wrapped }</div><div>func test&lt;T: Hashable&gt;(array: [T]) {</div><div>&nbsp; &nbsp; let t0 = CACurrentMediaTime()</div><div>&nbsp; &nbsp; let set = Set(array)</div><div>&nbsp; &nbsp; let t1 = CACurrentMediaTime()</div><div>&nbsp; &nbsp; print("Created set of", set.count, " elements in", t1 - t0, "seconds.")</div><div>}</div><div>// Download <a href="http://sloppyfocus.com/strings.zip">http://sloppyfocus.com/strings.zip</a>, unzip and set the correct path here:</div><div>let path = "/Users/jens/strings.txt"</div><div>do {</div><div>&nbsp; &nbsp; let strings = try String.init(contentsOfFile: path).componentsSeparatedByString("\n")</div><div>&nbsp; &nbsp; print("Loaded", strings.count, "strings.") // Prints: Loaded 88379 strings.</div><div>&nbsp; &nbsp; let wrappedStrings = strings.map { WrappedString($0) }</div><div>&nbsp; &nbsp; test(wrappedStrings) // Prints: Created set of 88379 strings in 0.0923392080003396 seconds.</div><div>&nbsp; &nbsp; test(strings) &nbsp; &nbsp; &nbsp; &nbsp;// Prints: Created set of 88379 strings in 9.25633556599496 seconds.</div><div>}</div><div>catch let e { fatalError(String(e)) }</div></div><div><br></div><div>/Jens</div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 4, 2016 at 11:13 AM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">(Correction: 0.07 seconds, not 0.7 seconds.)</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 4, 2016 at 10:33 AM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Another thing I noticed is that making a Set&lt;NSString&gt; (rather than a Set&lt;String&gt;) is fast ...<div><br></div><div>That is (for those same particular strings) this is fast (0.7 s):</div><div>let setOfNSStrings = Set&lt;NSString&gt;(strings.map { $0 as NSString })<br></div><div><br></div><div>while this is slow (9.5 s):</div><div>let setOfNSStrings = Set&lt;String&gt;(strings)<span><font color="#888888"><br></font></span></div><span><font color="#888888"><div><br></div><div>/Jens</div><div><br></div></font></span></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 4, 2016 at 9:48 AM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Are you already tracking/looking into this in someway or should I file a bug?<span><font color="#888888"><div>/Jens</div></font></span></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 3, 2016 at 5:55 AM, Arnold <span dir="ltr">&lt;<a href="mailto:aschwaighofer@apple.com" target="_blank">aschwaighofer@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>Yes this does not explain what you are seeing. (I could repeat this on my end)</div><div><br></div><div>If you look at the two runs under instruments: one run with the slow strings file and one with the fast strings file (I tested with space appended to each line); you will notice that we spend a lot more time in the Set's find function doing NSString comparisons. The only explanation I have for this is hash collisions leading to more comparisons as we are probing non empty buckets. Why one of the files would trigger this and the other not makes no sense to me.</div><div>This will need looking at.</div><div><br>Sent from my iPhone</div><div><div><div><br>On Mar 2, 2016, at 6:43 PM, Jens Persson &lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Just thought I should mention that I tried another thing:<div><br></div><div>Using my original example:</div><div><a href="http://sloppyfocus.com/slowSetFromParticularButSeeminglyNormalArrayOfStrings.html" target="_blank">http://sloppyfocus.com/slowSetFromParticularButSeeminglyNormalArrayOfStrings.html</a><br></div><div>Keeping the code as it is but adding a space to the end of every line in the actual text file (strings.txt).</div><div><br><div><div>This actually makes the slow test(strings) run almost as fast as the fast test(caseSwappedStrings), 0.09 seconds, so about a hundred times faster just because the text file has an extra space at the end of each line.</div><div><br></div><div>I just can't see how Arnold Schwaighofer's explanation explains that ... An added space character at the end of each line of that text file is just one more ASCII character, how can that make test(strings) so much faster?<br></div><div><div><div><br></div><div>And other operations on the text file makes the slow test faster too, like eg:</div><div>Inserting a space as the first char of each line, 0.9 seconds.</div><div>Removing all spaces, 0.9 seconds.</div><div>Replacing all spaces with "-" (replacing the dashes back to spaces made it slow again), 0.9 seconds.</div></div><div>Replacing all "a" with "*", 0.9 seconds.</div><div>Swapping case (now in the actual file instead of programmatically), 2.3 seconds.</div><div>(I only tried operations that kept the resulting strings unique and their characters plain ASCII.)</div><div><br></div><div>/Jens</div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 2, 2016 at 6:28 PM, Arnold Schwaighofer <span dir="ltr">&lt;<a href="mailto:aschwaighofer@apple.com" target="_blank">aschwaighofer@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">That is the difference between a “String” type instance that can use the ascii fast path and NSString backed “String” type instances.<br>
<br>
When the code translates the string (the map function that calls upper case) it becomes a natively backed “String” that isASCII.<br>
<br>
// The strings in strings.txt are not strange in any way, they all match ^[a-zA-Z ]+$.<br>
<br>
Set the isASCII bit and things are blazing fast.<br>
<br>
The other path is that componentsSeparatedByString(separator: String) -&gt; [String] returns an “[String]” where every “String” is backed by “NSString”. When it comes to do comparison we call a NSString function on the NSString and this is where the difference in performance comes from.<br>
<div><div><br>
<br>
<br>
<br>
&gt; On Mar 2, 2016, at 12:15 AM, Jens Persson via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; I guess it would be possible to isolate the problem by analyzing my example, and thereby making it possible to write a smaller benchmark which doesn't need that big textfile. I didn't manage to do that however, hence my question about the problem here.<br>
&gt; /Jens<br>
&gt;<br>
&gt; On Wed, Mar 2, 2016 at 9:11 AM, Jens Persson &lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt; wrote:<br>
&gt; Thanks, but I'll have to invite anyone with more time and experience to do that.<br>
&gt;<br>
&gt; One (of many) thing(s) I wouldn't know how include/handle is the 1.3 MB text file for the particular arrayOfStrings that makes Set(arrayOfStrings) slow. It seems a bit unnecessary/bloating to put it in the code base.<br>
&gt;<br>
&gt; /Jens<br>
&gt;<br>
&gt; On Tue, Mar 1, 2016 at 11:58 PM, Nadav Rotem &lt;<a href="mailto:nrotem@apple.com" target="_blank">nrotem@apple.com</a>&gt; wrote:<br>
&gt; Hi Jens,<br>
&gt;<br>
&gt; Thanks for reporting this issue. I don’t know what’s going on but we’ll take a look.<br>
&gt;<br>
&gt; I think it would be great if you could add this program as a benchmark under swift/benchmarks/.&nbsp; This will allow us to track the performance of this test and ensure that we don’t regress.<br>
&gt;<br>
&gt; Thanks,<br>
&gt; Nadav<br>
&gt;<br>
&gt;&gt; On Mar 1, 2016, at 5:01 AM, Jens Persson via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; For some particular arrays of strings, creating a Set&lt;String&gt;(arrayOfStrings) takes about 100 to 200 times longer than for other very similar arrays of strings (equally many unique simple [a-zA-Z ]+ strings).<br>
&gt;&gt;<br>
&gt;&gt; I've put together a tiny program to demonstrate the problem here:<br>
&gt;&gt; <a href="http://sloppyfocus.com/slowSetFromParticularButSeeminglyNormalArrayOfStrings.html" rel="noreferrer" target="_blank">http://sloppyfocus.com/slowSetFromParticularButSeeminglyNormalArrayOfStrings.html</a><br>
&gt;&gt;<br>
&gt;&gt; Is this due to a bug / performance problem in Set or can it be explained (and solved) in some way?<br>
&gt;&gt;<br>
&gt;&gt; /Jens<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; swift-dev mailing list<br>
&gt;&gt; <a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a><br>
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-dev" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-dev</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br>
&gt; <a href="http://www.bitcycle.com/" rel="noreferrer" target="_blank">http://www.bitcycle.com/</a><br>
&gt; Phone: +46-73-753 24 62<br>
&gt; E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br>
&gt; <a href="http://www.bitcycle.com/" rel="noreferrer" target="_blank">http://www.bitcycle.com/</a><br>
&gt; Phone: +46-73-753 24 62<br>
&gt; E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-dev mailing list<br>
&gt; <a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-dev" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-dev</a><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br><a href="http://www.bitcycle.com/" target="_blank">http://www.bitcycle.com/</a><br>Phone: +46-73-753 24 62<br>E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br><br></div>
</div>
</div></blockquote></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br><a href="http://www.bitcycle.com/" target="_blank">http://www.bitcycle.com/</a><br>Phone: +46-73-753 24 62<br>E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br><br></div>
</div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br><a href="http://www.bitcycle.com/" target="_blank">http://www.bitcycle.com/</a><br>Phone: +46-73-753 24 62<br>E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br><br></div>
</div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br><a href="http://www.bitcycle.com/" target="_blank">http://www.bitcycle.com/</a><br>Phone: +46-73-753 24 62<br>E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br><br></div>
</div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br><a href="http://www.bitcycle.com/" target="_blank">http://www.bitcycle.com/</a><br>Phone: +46-73-753 24 62<br>E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br><br></div>
</div>
</div></blockquote></body></html>