[swift-users] Optimising Set<String> comparisons

Nial Giacomelli measuredweighed at gmail.com
Sun Nov 13 13:58:36 CST 2016


Using Swift 3 I have a function that's called extremely frequently and is
appearing regularly in Profiler runs. The function takes two Set<String>
instances and simply attempts to determine whether all items from Set A are
present in Set B (it's important to note that Set B may well contain
additional items).

I've attempted to approach the problem in two ways:

let diff = a.subtracting(b)
guard diff.count == 0 else { return false }

And also by simply iterating over the contents of Set A, like so:

for item in a {
if !b.contains(item) {
return false
}
}

Both ultimately end up spending the majority of their time in
String._compareDeterministicUnicodeCollaton(String) -> Int. Which makes
sense, given what I'm doing - but ideally I'd like to come up with a more
efficient way of performing the above check. Swift's String representation
is incredibly robust, but for my needs the strings could be adequately
represented in ASCII encoding. I've also considered storing and comparing
the hashValue of the strings, to speed up comparisons...

Hopefully this is an acceptable question for this mailing list. I'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'd really appreciate
some input from more experienced Swift developers :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161113/6dd19c1e/attachment.html>


More information about the swift-users mailing list