Optimize union function #KT-36682
diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt
index e971178..fd7afbb 100644
--- a/libraries/stdlib/common/src/generated/_Arrays.kt
+++ b/libraries/stdlib/common/src/generated/_Arrays.kt
@@ -8187,7 +8187,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives
*/
public inline fun <K, V> ByteArray.associate(transform: (Byte) -> Pair<K, V>): Map<K, V> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -8277,7 +8277,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives
*/
public inline fun <K, V> BooleanArray.associate(transform: (Boolean) -> Pair<K, V>): Map<K, V> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -8292,7 +8292,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitives
*/
public inline fun <K, V> CharArray.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -8322,7 +8322,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy
*/
public inline fun <K> ByteArray.associateBy(keySelector: (Byte) -> K): Map<K, Byte> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Byte>(capacity), keySelector)
}
@@ -8412,7 +8412,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy
*/
public inline fun <K> BooleanArray.associateBy(keySelector: (Boolean) -> K): Map<K, Boolean> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Boolean>(capacity), keySelector)
}
@@ -8427,7 +8427,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesBy
*/
public inline fun <K> CharArray.associateBy(keySelector: (Char) -> K): Map<K, Char> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Char>(capacity), keySelector)
}
@@ -8455,7 +8455,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform
*/
public inline fun <K, V> ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map<K, V> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -8539,7 +8539,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform
*/
public inline fun <K, V> BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map<K, V> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -8553,7 +8553,7 @@
* @sample samples.collections.Arrays.Transformations.associateArrayOfPrimitivesByWithValueTransform
*/
public inline fun <K, V> CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
- val capacity = mapCapacity(size).coerceAtLeast(16)
+ val capacity = mapCapacity(size.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -9011,7 +9011,7 @@
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> ByteArray.associateWith(valueSelector: (Byte) -> V): Map<Byte, V> {
- val result = LinkedHashMap<Byte, V>(mapCapacity(size).coerceAtLeast(16))
+ val result = LinkedHashMap<Byte, V>(mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
@@ -9119,7 +9119,7 @@
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline fun <V> BooleanArray.associateWith(valueSelector: (Boolean) -> V): Map<Boolean, V> {
- val result = LinkedHashMap<Boolean, V>(mapCapacity(size).coerceAtLeast(16))
+ val result = LinkedHashMap<Boolean, V>(mapCapacity(size.coerceAtMost(2)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
@@ -9403,7 +9403,7 @@
* Returns a new [HashSet] of all elements.
*/
public fun ByteArray.toHashSet(): HashSet<Byte> {
- return toCollection(HashSet<Byte>(mapCapacity(size)))
+ return toCollection(HashSet<Byte>(mapCapacity(size.coerceAtMost(256))))
}
/**
@@ -9445,7 +9445,7 @@
* Returns a new [HashSet] of all elements.
*/
public fun BooleanArray.toHashSet(): HashSet<Boolean> {
- return toCollection(HashSet<Boolean>(mapCapacity(size)))
+ return toCollection(HashSet<Boolean>(mapCapacity(size.coerceAtMost(2))))
}
/**
@@ -9655,7 +9655,7 @@
return when (size) {
0 -> emptySet()
1 -> setOf(this[0])
- else -> toCollection(LinkedHashSet<Byte>(mapCapacity(size)))
+ else -> toCollection(LinkedHashSet<Byte>(mapCapacity(size.coerceAtMost(256))))
}
}
@@ -9733,7 +9733,7 @@
return when (size) {
0 -> emptySet()
1 -> setOf(this[0])
- else -> toCollection(LinkedHashSet<Boolean>(mapCapacity(size)))
+ else -> toCollection(LinkedHashSet<Boolean>(mapCapacity(size.coerceAtMost(2))))
}
}
@@ -9939,7 +9939,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <T, K> Array<out T>.groupBy(keySelector: (T) -> K): Map<K, List<T>> {
- return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<T>>(mapCapacity(size)), keySelector)
}
/**
@@ -9951,7 +9951,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> ByteArray.groupBy(keySelector: (Byte) -> K): Map<K, List<Byte>> {
- return groupByTo(LinkedHashMap<K, MutableList<Byte>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Byte>>(mapCapacity(size.coerceAtMost(256))), keySelector)
}
/**
@@ -9963,7 +9963,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> ShortArray.groupBy(keySelector: (Short) -> K): Map<K, List<Short>> {
- return groupByTo(LinkedHashMap<K, MutableList<Short>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Short>>(mapCapacity(size)), keySelector)
}
/**
@@ -9975,7 +9975,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> IntArray.groupBy(keySelector: (Int) -> K): Map<K, List<Int>> {
- return groupByTo(LinkedHashMap<K, MutableList<Int>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Int>>(mapCapacity(size)), keySelector)
}
/**
@@ -9987,7 +9987,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> LongArray.groupBy(keySelector: (Long) -> K): Map<K, List<Long>> {
- return groupByTo(LinkedHashMap<K, MutableList<Long>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Long>>(mapCapacity(size)), keySelector)
}
/**
@@ -9999,7 +9999,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> FloatArray.groupBy(keySelector: (Float) -> K): Map<K, List<Float>> {
- return groupByTo(LinkedHashMap<K, MutableList<Float>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Float>>(mapCapacity(size)), keySelector)
}
/**
@@ -10011,7 +10011,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> DoubleArray.groupBy(keySelector: (Double) -> K): Map<K, List<Double>> {
- return groupByTo(LinkedHashMap<K, MutableList<Double>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Double>>(mapCapacity(size)), keySelector)
}
/**
@@ -10023,7 +10023,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> BooleanArray.groupBy(keySelector: (Boolean) -> K): Map<K, List<Boolean>> {
- return groupByTo(LinkedHashMap<K, MutableList<Boolean>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Boolean>>(mapCapacity(size.coerceAtMost(2))), keySelector)
}
/**
@@ -10035,7 +10035,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> CharArray.groupBy(keySelector: (Char) -> K): Map<K, List<Char>> {
- return groupByTo(LinkedHashMap<K, MutableList<Char>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Char>>(mapCapacity(size.coerceAtMost(128))), keySelector)
}
/**
@@ -10048,7 +10048,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <T, K, V> Array<out T>.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10061,7 +10061,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(256))), keySelector, valueTransform)
}
/**
@@ -10074,7 +10074,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10087,7 +10087,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10100,7 +10100,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10113,7 +10113,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10126,7 +10126,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -10139,7 +10139,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(2))), keySelector, valueTransform)
}
/**
@@ -10152,7 +10152,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(128))), keySelector, valueTransform)
}
/**
@@ -11484,7 +11484,7 @@
* The returned set preserves the element iteration order of the original array.
*/
public fun ByteArray.toMutableSet(): MutableSet<Byte> {
- return toCollection(LinkedHashSet<Byte>(mapCapacity(size)))
+ return toCollection(LinkedHashSet<Byte>(mapCapacity(size.coerceAtMost(256))))
}
/**
@@ -11538,7 +11538,7 @@
* The returned set preserves the element iteration order of the original array.
*/
public fun BooleanArray.toMutableSet(): MutableSet<Boolean> {
- return toCollection(LinkedHashSet<Boolean>(mapCapacity(size)))
+ return toCollection(LinkedHashSet<Boolean>(mapCapacity(size.coerceAtMost(2))))
}
/**
@@ -11560,7 +11560,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun <T> Array<out T>.union(other: Iterable<T>): Set<T> {
- val set = this.toMutableSet()
+ val capacity = size + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<T>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11575,7 +11576,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun ByteArray.union(other: Iterable<Byte>): Set<Byte> {
- val set = this.toMutableSet()
+ val capacity = size.coerceAtMost(256) + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Byte>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11590,7 +11592,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun ShortArray.union(other: Iterable<Short>): Set<Short> {
- val set = this.toMutableSet()
+ val capacity = size + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Short>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11605,7 +11608,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun IntArray.union(other: Iterable<Int>): Set<Int> {
- val set = this.toMutableSet()
+ val capacity = size + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Int>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11620,7 +11624,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun LongArray.union(other: Iterable<Long>): Set<Long> {
- val set = this.toMutableSet()
+ val capacity = size + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Long>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11635,7 +11640,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun FloatArray.union(other: Iterable<Float>): Set<Float> {
- val set = this.toMutableSet()
+ val capacity = size + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Float>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11650,7 +11656,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun DoubleArray.union(other: Iterable<Double>): Set<Double> {
- val set = this.toMutableSet()
+ val capacity = size + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Double>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11665,7 +11672,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun BooleanArray.union(other: Iterable<Boolean>): Set<Boolean> {
- val set = this.toMutableSet()
+ val capacity = size.coerceAtMost(2) + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Boolean>(mapCapacity(capacity)))
set.addAll(other)
return set
}
@@ -11680,7 +11688,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun CharArray.union(other: Iterable<Char>): Set<Char> {
- val set = this.toMutableSet()
+ val capacity = size.coerceAtMost(128) + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<Char>(mapCapacity(capacity)))
set.addAll(other)
return set
}
diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt
index e8fb686..9b5f7f3 100644
--- a/libraries/stdlib/common/src/generated/_Collections.kt
+++ b/libraries/stdlib/common/src/generated/_Collections.kt
@@ -1226,7 +1226,7 @@
* Returns a new [HashSet] of all elements.
*/
public fun <T> Iterable<T>.toHashSet(): HashSet<T> {
- return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))
+ return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(10))))
}
/**
@@ -1304,7 +1304,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <T, K> Iterable<T>.groupBy(keySelector: (T) -> K): Map<K, List<T>> {
- return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<T>>(mapCapacity(collectionSizeOrDefault(10))), keySelector)
}
/**
@@ -1317,7 +1317,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <T, K, V> Iterable<T>.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(collectionSizeOrDefault(10))), keySelector, valueTransform)
}
/**
@@ -1536,7 +1536,8 @@
* To get a set containing all elements that are contained in both collections use [intersect].
*/
public infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
- val set = this.toMutableSet()
+ val capacity = collectionSizeOrDefault(10) + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<T>(mapCapacity(capacity)))
set.addAll(other)
return set
}
diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt
index 8a4a125..3073a86 100644
--- a/libraries/stdlib/common/src/generated/_Strings.kt
+++ b/libraries/stdlib/common/src/generated/_Strings.kt
@@ -616,7 +616,7 @@
* @sample samples.text.Strings.associate
*/
public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
- val capacity = mapCapacity(length).coerceAtLeast(16)
+ val capacity = mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
@@ -631,7 +631,7 @@
* @sample samples.text.Strings.associateBy
*/
public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K, Char> {
- val capacity = mapCapacity(length).coerceAtLeast(16)
+ val capacity = mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Char>(capacity), keySelector)
}
@@ -645,7 +645,7 @@
* @sample samples.text.Strings.associateByWithValueTransform
*/
public inline fun <K, V> CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
- val capacity = mapCapacity(length).coerceAtLeast(16)
+ val capacity = mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
@@ -805,7 +805,7 @@
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> CharSequence.groupBy(keySelector: (Char) -> K): Map<K, List<Char>> {
- return groupByTo(LinkedHashMap<K, MutableList<Char>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<Char>>(mapCapacity(length.coerceAtMost(128))), keySelector)
}
/**
@@ -818,7 +818,7 @@
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(length.coerceAtMost(128))), keySelector, valueTransform)
}
/**
diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt
index 2336ddb..f592059 100644
--- a/libraries/stdlib/common/src/generated/_UArrays.kt
+++ b/libraries/stdlib/common/src/generated/_UArrays.kt
@@ -4263,7 +4263,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <V> UByteArray.associateWith(valueSelector: (UByte) -> V): Map<UByte, V> {
- val result = LinkedHashMap<UByte, V>(mapCapacity(size).coerceAtLeast(16))
+ val result = LinkedHashMap<UByte, V>(mapCapacity(size.coerceAtMost(256)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
@@ -4478,7 +4478,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> UIntArray.groupBy(keySelector: (UInt) -> K): Map<K, List<UInt>> {
- return groupByTo(LinkedHashMap<K, MutableList<UInt>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<UInt>>(mapCapacity(size)), keySelector)
}
/**
@@ -4493,7 +4493,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> ULongArray.groupBy(keySelector: (ULong) -> K): Map<K, List<ULong>> {
- return groupByTo(LinkedHashMap<K, MutableList<ULong>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<ULong>>(mapCapacity(size)), keySelector)
}
/**
@@ -4508,7 +4508,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> UByteArray.groupBy(keySelector: (UByte) -> K): Map<K, List<UByte>> {
- return groupByTo(LinkedHashMap<K, MutableList<UByte>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<UByte>>(mapCapacity(size.coerceAtMost(256))), keySelector)
}
/**
@@ -4523,7 +4523,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K> UShortArray.groupBy(keySelector: (UShort) -> K): Map<K, List<UShort>> {
- return groupByTo(LinkedHashMap<K, MutableList<UShort>>(), keySelector)
+ return groupByTo(LinkedHashMap<K, MutableList<UShort>>(mapCapacity(size)), keySelector)
}
/**
@@ -4539,7 +4539,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> UIntArray.groupBy(keySelector: (UInt) -> K, valueTransform: (UInt) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -4555,7 +4555,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> ULongArray.groupBy(keySelector: (ULong) -> K, valueTransform: (ULong) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
@@ -4571,7 +4571,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> UByteArray.groupBy(keySelector: (UByte) -> K, valueTransform: (UByte) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size.coerceAtMost(256))), keySelector, valueTransform)
}
/**
@@ -4587,7 +4587,7 @@
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <K, V> UShortArray.groupBy(keySelector: (UShort) -> K, valueTransform: (UShort) -> V): Map<K, List<V>> {
- return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
+ return groupByTo(LinkedHashMap<K, MutableList<V>>(mapCapacity(size)), keySelector, valueTransform)
}
/**
diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt
index b7db1d3..506a061 100644
--- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt
+++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt
@@ -351,7 +351,10 @@
sequenceClassification(terminal)
typeParam("K")
returns("Map<K, List<T>>")
- body { "return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)" }
+ body {
+ val capacity = if (f == Sequences) "" else "mapCapacity(${f.code.toSetSize(primitive)})"
+ "return groupByTo(LinkedHashMap<K, MutableList<T>>($capacity), keySelector)"
+ }
}
val f_groupByTo_key = fn("groupByTo(destination: M, keySelector: (T) -> K)") {
@@ -407,7 +410,10 @@
typeParam("K")
typeParam("V")
returns("Map<K, List<V>>")
- body { "return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)" }
+ body {
+ val capacity = if (f == Sequences) "" else "mapCapacity(${f.code.toSetSize(primitive)})"
+ "return groupByTo(LinkedHashMap<K, MutableList<V>>($capacity), keySelector, valueTransform)"
+ }
}
diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt
index 97a1cb0..b0bb213 100644
--- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt
+++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt
@@ -31,8 +31,7 @@
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
- val capacity = "size" + if (primitive == PrimitiveType.Char) ".coerceAtMost(128)" else ""
- "return toCollection(LinkedHashSet<T>(mapCapacity($capacity)))"
+ "return toCollection(LinkedHashSet<T>(mapCapacity(${f.code.toSetSize(primitive)})))"
}
body(Sequences) {
"""
@@ -122,7 +121,8 @@
returns("Set<T>")
body {
"""
- val set = this.toMutableSet()
+ val capacity = ${f.code.toSetSize(primitive)} + other.collectionSizeOrDefault(10)
+ val set = toCollection(LinkedHashSet<T>(mapCapacity(capacity)))
set.addAll(other)
return set
"""
diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt
index 841ea4c..2ec43e8 100644
--- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt
+++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt
@@ -62,13 +62,11 @@
body(Sequences) { "return toCollection(LinkedHashSet<T>()).optimizeReadOnlySet()" }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
- val size = f.code.size
- val capacity = if (f == CharSequences || primitive == PrimitiveType.Char) "$size.coerceAtMost(128)" else size
"""
- return when ($size) {
+ return when (${f.code.size}) {
0 -> emptySet()
1 -> setOf(this[0])
- else -> toCollection(LinkedHashSet<T>(mapCapacity($capacity)))
+ else -> toCollection(LinkedHashSet<T>(mapCapacity(${f.code.toSetSize(primitive)})))
}
"""
}
@@ -80,12 +78,9 @@
} builder {
doc { "Returns a new [HashSet] of all ${f.element.pluralize()}." }
returns("HashSet<T>")
- body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(HashSet<T>())" }
- 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)))"
+ body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
+ "return toCollection(HashSet<T>(mapCapacity(${f.code.toSetSize(primitive)})))"
}
}
@@ -216,9 +211,9 @@
ArraysOfObjects, ArraysOfPrimitives -> "samples.collections.Arrays.Transformations.associateArrayOfPrimitives"
else -> "samples.collections.Collections.Transformations.associate"
})
- body {
+ body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
- val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
+ val capacity = mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
"""
}
@@ -227,18 +222,6 @@
return associateTo(LinkedHashMap<K, V>(), transform)
"""
}
- body(CharSequences) {
- """
- val capacity = mapCapacity(length).coerceAtLeast(16)
- return associateTo(LinkedHashMap<K, V>(capacity), transform)
- """
- }
- body(ArraysOfObjects, ArraysOfPrimitives) {
- """
- val capacity = mapCapacity(size).coerceAtLeast(16)
- return associateTo(LinkedHashMap<K, V>(capacity), transform)
- """
- }
}
val f_associateTo = fn("associateTo(destination: M, transform: (T) -> Pair<K, V>)") {
@@ -298,9 +281,9 @@
// Collection size helper methods are private, so we fall back to the calculation from HashSet's Collection
// constructor.
- body {
+ body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
- val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
+ val capacity = mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
"""
}
@@ -309,18 +292,6 @@
return associateByTo(LinkedHashMap<K, T>(), keySelector)
"""
}
- body(CharSequences) {
- """
- val capacity = mapCapacity(length).coerceAtLeast(16)
- return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
- """
- }
- body(ArraysOfObjects, ArraysOfPrimitives) {
- """
- val capacity = mapCapacity(size).coerceAtLeast(16)
- return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
- """
- }
}
val f_associateByTo_key = fn("associateByTo(destination: M, keySelector: (T) -> K)") {
@@ -383,9 +354,9 @@
* constructor.
*/
- body {
+ body(Iterables, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"""
- val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
+ val capacity = mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
"""
}
@@ -394,18 +365,6 @@
return associateByTo(LinkedHashMap<K, V>(), keySelector, valueTransform)
"""
}
- body(CharSequences) {
- """
- val capacity = mapCapacity(length).coerceAtLeast(16)
- return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
- """
- }
- body(ArraysOfObjects, ArraysOfPrimitives) {
- """
- val capacity = mapCapacity(size).coerceAtLeast(16)
- return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
- """
- }
}
val f_associateByTo_key_value = fn("associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") {
@@ -470,16 +429,7 @@
else -> "samples.collections.Collections.Transformations.associateWith"
})
body {
- 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 capacity = if (f == Sequences) "" else "mapCapacity(${f.code.toSetSize(primitive)}).coerceAtLeast(16)"
"""
val result = LinkedHashMap<K, V>($capacity)
return associateWithTo(result, valueSelector)
diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt
index 88dd81f..074046f 100644
--- a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt
+++ b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt
@@ -20,6 +20,18 @@
else -> error("size property isn't supported for $family")
}
+fun Family.CodeExtension.toSetSize(primitive: PrimitiveType?): String = when (family) {
+ Iterables -> "collectionSizeOrDefault(10)"
+ Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects -> "size"
+ ArraysOfPrimitives, ArraysOfUnsigned ->
+ if (primitive == PrimitiveType.Boolean) "size.coerceAtMost(2)"
+ else if (primitive == PrimitiveType.Char) "size.coerceAtMost(128)"
+ else if (primitive == PrimitiveType.Byte || primitive == PrimitiveType.UByte) "size.coerceAtMost(256)"
+ else "size"
+ CharSequences, Strings -> "length.coerceAtMost(128)"
+ else -> error("size property isn't supported for $family")
+}
+
object DocExtensions {
val Family.element: String