Skip to content

Commit

Permalink
Do not use spread-operator when it causes excessive array copying
Browse files Browse the repository at this point in the history
(cherry picked from commit ddf6599)
  • Loading branch information
ilya-g committed May 19, 2017
1 parent 10c85a6 commit 6a15981
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions libraries/stdlib/src/kotlin/collections/Maps.kt
Expand Up @@ -43,7 +43,7 @@ public fun <K, V> emptyMap(): Map<K, V> = @Suppress("UNCHECKED_CAST") (EmptyMap
*
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> = if (pairs.size > 0) linkedMapOf(*pairs) else emptyMap()
public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> = if (pairs.size > 0) pairs.toMap(LinkedHashMap(mapCapacity(pairs.size))) else emptyMap()

/**
* Returns an empty read-only map.
Expand Down Expand Up @@ -123,7 +123,7 @@ public inline fun <K, V> linkedMapOf(): LinkedHashMap<K, V> = LinkedHashMap<K, V
* @sample samples.collections.Maps.Instantiation.linkedMapFromPairs
*/
public fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V>
= LinkedHashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
= pairs.toMap(LinkedHashMap(mapCapacity(pairs.size)))

/**
* Calculate the initial capacity of a map, based on Guava's com.google.common.collect.Maps approach. This is equivalent
Expand Down
7 changes: 6 additions & 1 deletion libraries/stdlib/src/kotlin/comparisons/Comparisons.kt
Expand Up @@ -26,6 +26,10 @@ package kotlin.comparisons
*/
public fun <T> compareValuesBy(a: T, b: T, vararg selectors: (T) -> Comparable<*>?): Int {
require(selectors.size > 0)
return compareValuesByImpl(a, b, selectors)
}

private fun <T> compareValuesByImpl(a: T, b: T, selectors: Array<out (T)->Comparable<*>?>): Int {
for (fn in selectors) {
val v1 = fn(a)
val v2 = fn(b)
Expand Down Expand Up @@ -84,8 +88,9 @@ public fun <T : Comparable<*>> compareValues(a: T?, b: T?): Int {
* compare as equal, the result of that comparison is returned from the [Comparator].
*/
public fun <T> compareBy(vararg selectors: (T) -> Comparable<*>?): Comparator<T> {
require(selectors.size > 0)
return object : Comparator<T> {
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, *selectors)
public override fun compare(a: T, b: T): Int = compareValuesByImpl(a, b, selectors)
}
}

Expand Down

0 comments on commit 6a15981

Please sign in to comment.