<div dir="ltr">Many thanks to Tim for suggesting isSubset, which I had somehow missed while browsing the documentation.<div><br></div><div>To answer Dave&#39;s questions: unfortunately the Set comparisons aren&#39;t an ideal candidate for asynchronous work. The comparisons take place as part of a CSS-like styling phase, whereby a number of Sets (the stylesheet definitions) are compared against many thousands of other Sets (the class lists for each styleable object). While I believe the Int comparisons would undoubtedly be faster, I&#39;m likely to lose even more time calculating the hashValues for each Set.</div><div><br></div><div>I believe the use of isSubset as well as some result caching (storing matches for subsequent queries including identical class lists) is likely to result in a decent speed improvement. Thank you both for taking the time to respond.</div><div><br></div><div>And if anyone has any suggestions for alternative approaches I&#39;m all ears :-)</div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Nov 13, 2016 at 11:49 PM, David Sweeris <span dir="ltr">&lt;<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.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 style="word-wrap:break-word"><div><div class="h5"><br><div><blockquote type="cite"><div>On Nov 13, 2016, at 1:58 PM, Nial Giacomelli via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_-760095414028911168Apple-interchange-newline"><div><div dir="ltr">Using Swift 3 I have a function that&#39;s called extremely frequently and is appearing regularly in Profiler runs. The function takes two Set&lt;String&gt; instances and simply attempts to determine whether all items from Set A are present in Set B (it&#39;s important to note that Set B may well contain additional items).<div><br></div><div>I&#39;ve attempted to approach the problem in two ways:</div><div><br></div><div><div>let diff = a.subtracting(b)</div><div>guard diff.count == 0 else { return false }</div></div><div><br></div><div>And also by simply iterating over the contents of Set A, like so:</div><div><br></div><div><div>for item in a {</div><div><span class="m_-760095414028911168gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>if !b.contains(item) {</div><div><span class="m_-760095414028911168gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>return false</div><div><span class="m_-760095414028911168gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div>}</div></div><div><br></div><div>Both ultimately end up spending the majority of their time in String._<wbr>compareDeterministicUnicodeCol<wbr>laton(String) -&gt; Int. Which makes sense, given what I&#39;m doing - but ideally I&#39;d like to come up with a more efficient way of performing the above check. Swift&#39;s String representation is incredibly robust, but for my needs the strings could be adequately represented in ASCII encoding. I&#39;ve also considered storing and comparing the hashValue of the strings, to speed up comparisons...</div><div><br></div><div>Hopefully this is an acceptable question for this mailing list. I&#39;m aware that this may not be a Swift-specific question and could well be solved with a more efficient data structure or approach, but I&#39;d really appreciate some input from more experienced Swift developers :-)</div></div></div></blockquote><br></div></div></div><div>How often do the sets change? And where do you get them from? I’m wondering, if there’s enough time between when you get the strings and when you need to know if setA is a subset of setB, could you somehow compute the answer asynchronously in a background thread or something? Strictly speaking it wouldn&#39;t be any less work, but if you can move it to where you’re waiting for user input or something you’ll probably get your answer sooner.</div><div><br></div><div>Regarding how to get the answer using less work in the first place, I think you’re doing a tiny bit of extra work — just some allocation overhead, really — by calculating the set difference and checking that its count == 0, rather than just checking if a.isSubset(of: b). Beyond that, I’m not really sure… You might be able to do something with the strings’ hash values. I mean, I don’t know if they really “work like that” (I’m quite foggy on how hash values are calculated and exactly what they’re suitable for… this might be a horrible idea), but if they do, comparing hash values (Ints) will definitely be faster than comparing Strings. I did some quick’n’dirty testing on my machine with some code very much like:</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> setA: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Set</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">&gt; = [</span><span style="font-variant-ligatures:no-common-ligatures">&quot;some&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;set&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;of&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;random&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;strings&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">]</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> setB: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Set</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">&lt;</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">&gt; = [</span><span style="font-variant-ligatures:no-common-ligatures">&quot;some&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;other&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;set&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;of&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;strings&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">]</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures"> hashedA = </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Set</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">setA</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81">map</span><span style="font-variant-ligatures:no-common-ligatures">{$0.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">hashValue</span><span style="font-variant-ligatures:no-common-ligatures">})</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures"> hashedB = </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Set</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">setB</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81">map</span><span style="font-variant-ligatures:no-common-ligatures">{$0.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">hashValue</span><span style="font-variant-ligatures:no-common-ligatures">})</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">setA</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">.</span><span style="font-variant-ligatures:no-common-ligatures">isSubset</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(of: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">setB</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures">hashedA</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81">isSubset</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(of: </span><span style="font-variant-ligatures:no-common-ligatures">hashedB</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div></div></blockquote><div><div><span style="font-variant-ligatures:no-common-ligatures;color:#000000"><br></span></div></div><div><span style="font-variant-ligatures:no-common-ligatures">The actual execution of </span><span style="color:rgb(79,129,135);font-family:Menlo;font-variant-ligatures:no-common-ligatures">hashedA</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures">.</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">isSubset</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures">(of: </span><span style="color:rgb(79,129,135);font-family:Menlo;font-variant-ligatures:no-common-ligatures">hashed<wbr>B</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures">)</span> seems to be very roughly 2x faster than <span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">setA</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures">.</span><span style="color:rgb(61,29,129);font-family:Menlo;font-variant-ligatures:no-common-ligatures">isSubset</span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures">(of: </span><span style="font-family:Menlo;font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">setB).</span> <wbr>However, if you include the overhead of calculating them, using hash values drops to about 5x <i>slower</i>. So unless you’d be reusing the hashed values a lot or something, I think you’re already doing about the least amount of work I can think of.</div><div><br></div><div>- Dave Sweeris</div></div></blockquote></div><br></div></div></div></div>