toHashSet is suboptimal for inputs with a lot of duplicates #KT-23142

This commit is contained in:
Abduqodiri Qurbonzoda
2020-04-15 02:20:50 +03:00
parent 2c4fcebfec
commit 0d5ec27ac4
6 changed files with 63 additions and 64 deletions
@@ -5,10 +5,6 @@
package templates
import templates.DocExtensions.collection
import templates.DocExtensions.element
import templates.DocExtensions.mapResult
import templates.DocExtensions.pluralize
import templates.Family.*
import templates.SequenceClass.*
@@ -35,11 +31,8 @@ object SetOps : TemplateGroupBase() {
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val set = LinkedHashSet<T>(mapCapacity(size))
for (item in this) set.add(item)
return set
"""
val capacity = "size" + if (primitive == PrimitiveType.Char) ".coerceAtMost(128)" else ""
"return toCollection(LinkedHashSet<T>(mapCapacity($capacity)))"
}
body(Sequences) {
"""
@@ -55,7 +55,6 @@ object Snapshots : TemplateGroupBase() {
1 -> setOf(if (this is List) this[0] else iterator().next())
else -> toCollection(LinkedHashSet<T>(mapCapacity(size)))
}
}
return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()
"""
@@ -63,12 +62,13 @@ object Snapshots : TemplateGroupBase() {
body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
val size = if (f == CharSequences) "length" else "size"
val size = f.code.size
val capacity = if (f == CharSequences || primitive == PrimitiveType.Char) "$size.coerceAtMost(128)" else size
"""
return when ($size) {
0 -> emptySet()
1 -> setOf(this[0])
else -> toCollection(LinkedHashSet<T>(mapCapacity($size)))
else -> toCollection(LinkedHashSet<T>(mapCapacity($capacity)))
}
"""
}
@@ -82,8 +82,11 @@ object Snapshots : TemplateGroupBase() {
returns("HashSet<T>")
body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(HashSet<T>())" }
body(CharSequences) { "return toCollection(HashSet<T>(mapCapacity(length)))" }
body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet<T>(mapCapacity(size)))" }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
val size = f.code.size
val capacity = if (f == CharSequences || primitive == PrimitiveType.Char) "$size.coerceAtMost(128)" else size
"return toCollection(HashSet<T>(mapCapacity($capacity)))"
}
}
val f_toSortedSet = fn("toSortedSet()") {
@@ -467,13 +470,18 @@ object Snapshots : TemplateGroupBase() {
else -> "samples.collections.Collections.Transformations.associateWith"
})
body {
val resultMap = when (family) {
Iterables -> "LinkedHashMap<K, V>(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))"
CharSequences -> "LinkedHashMap<K, V>(mapCapacity(length).coerceAtLeast(16))"
else -> "LinkedHashMap<K, V>()"
val capacity = when (family) {
Iterables -> "mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)"
CharSequences -> "mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)"
ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned -> if (primitive == PrimitiveType.Char) {
"mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)"
} else {
"mapCapacity(size).coerceAtLeast(16)"
}
else -> ""
}
"""
val result = $resultMap
val result = LinkedHashMap<K, V>($capacity)
return associateWithTo(result, valueSelector)
"""
}