https://kotlinlang.org logo
Title
e

eygraber

09/06/2018, 4:28 AM
Looking for input on if it's worth filing a request on YouTrack for this: The implementation of set difference in the stdlib is:
public operator fun <T> Set<T>.minus(elements: Iterable<T>): Set<T> {
    val other = elements.convertToSetForSetOperationWith(this)
    if (other.isEmpty())
        return this.toSet()
    if (other is Set)
        return this.filterNotTo(LinkedHashSet<T>()) { it in other }
    val result = LinkedHashSet<T>(this)
    result.removeAll(other)
    return result
}
My request is that an initial capacity get added to the
LinkedHashSet
that is returned if
other
is a
Set
. I know there's no way to know what the value would be, but it could certainly be approximated at better than the default by doing
LinkedHashSet<T>(this.size - other.size)
. That way, in the "best case" where everything in
other
is in the set, the capacity will be correct, and in the "worst case", where nothing in
other
is in the set, the capacity will have to grow to the size of the set, but we'll likely be closer to that than the default (16). Am I missing something / is this not necessary, or is this worth filing a bug over?
k

karelpeeters

09/06/2018, 5:54 AM
I'd say run some benchmarks to see if there's a significant difference, and post the results in #stdlib
g

gildor

09/06/2018, 6:15 AM
Seams reasonable. I think make sense to report this to kotl.in/issue
e

eygraber

09/06/2018, 7:35 AM
Actually the more I think about it, the more I think it's better as is. Lots of corner cases, e.g.
other.size > this.size
, if
this
and
other
are large, and most of
this
ends up getting filtered out there'll be a lot of wasted memory, etc...