Remove the brittle contains optimization code #KT-47707

This commit is contained in:
Abduqodiri Qurbonzoda
2022-09-07 02:01:09 +03:00
committed by Space
parent 51fd2af6fd
commit 7060811c8b
9 changed files with 30 additions and 161 deletions
@@ -1,66 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.collections
/** Returns true if the brittle contains optimization is enabled. See KT-45438. */
internal expect fun brittleContainsOptimizationEnabled(): Boolean
/**
* Returns true if [brittleContainsOptimizationEnabled] is true
* and it's safe to convert this collection to a set without changing contains method behavior.
*/
private fun <T> Collection<T>.safeToConvertToSet() = brittleContainsOptimizationEnabled() && size > 2 && this is ArrayList
/**
* When [brittleContainsOptimizationEnabled] is true:
* - Converts this [Iterable] to a set if it is not a [Collection].
* - Converts this [Collection] to a set, when it's worth so and it doesn't change contains method behavior.
* - Otherwise returns this.
* When [brittleContainsOptimizationEnabled] is false:
* - Converts this [Iterable] to a list if it is not a [Collection].
* - Otherwise returns this.
*/
internal fun <T> Iterable<T>.convertToSetForSetOperationWith(source: Iterable<T>): Collection<T> =
when (this) {
is Set -> this
is Collection ->
when {
source is Collection && source.size < 2 -> this
else -> if (this.safeToConvertToSet()) toHashSet() else this
}
else -> if (brittleContainsOptimizationEnabled()) toHashSet() else toList()
}
/**
* When [brittleContainsOptimizationEnabled] is true:
* - Converts this [Iterable] to a set if it is not a [Collection].
* - Converts this [Collection] to a set, when it's worth so and it doesn't change contains method behavior.
* - Otherwise returns this.
* When [brittleContainsOptimizationEnabled] is false:
* - Converts this [Iterable] to a list if it is not a [Collection].
* - Otherwise returns this.
*/
internal fun <T> Iterable<T>.convertToSetForSetOperation(): Collection<T> =
when (this) {
is Set -> this
is Collection -> if (this.safeToConvertToSet()) toHashSet() else this
else -> if (brittleContainsOptimizationEnabled()) toHashSet() else toList()
}
/**
* Converts this sequence to a set if [brittleContainsOptimizationEnabled] is true,
* otherwise converts it to a list.
*/
internal fun <T> Sequence<T>.convertToSetForSetOperation(): Collection<T> =
if (brittleContainsOptimizationEnabled()) toHashSet() else toList()
/**
* Converts this array to a set if [brittleContainsOptimizationEnabled] is true,
* otherwise converts it to a list.
*/
internal fun <T> Array<T>.convertToSetForSetOperation(): Collection<T> =
if (brittleContainsOptimizationEnabled()) toHashSet() else asList()
@@ -8,8 +8,6 @@
package kotlin.collections
import kotlin.random.Random
/**
* Removes a single instance of the specified element from this
* collection, if it is present.
@@ -141,33 +139,40 @@ public fun <T> MutableCollection<in T>.addAll(elements: Array<out T>): Boolean {
return addAll(elements.asList())
}
/**
* Converts this [Iterable] to a list if it is not a [Collection].
* Otherwise, returns this.
*/
internal fun <T> Iterable<T>.convertToListIfNotCollection(): Collection<T> =
if (this is Collection) this else toList()
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] collection.
*/
public fun <T> MutableCollection<in T>.removeAll(elements: Iterable<T>): Boolean {
return removeAll(elements.convertToSetForSetOperationWith(this))
return removeAll(elements.convertToListIfNotCollection())
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] sequence.
*/
public fun <T> MutableCollection<in T>.removeAll(elements: Sequence<T>): Boolean {
val set = elements.convertToSetForSetOperation()
return set.isNotEmpty() && removeAll(set)
val list = elements.toList()
return list.isNotEmpty() && removeAll(list)
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] array.
*/
public fun <T> MutableCollection<in T>.removeAll(elements: Array<out T>): Boolean {
return elements.isNotEmpty() && removeAll(elements.convertToSetForSetOperation())
return elements.isNotEmpty() && removeAll(elements.asList())
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [elements] collection.
*/
public fun <T> MutableCollection<in T>.retainAll(elements: Iterable<T>): Boolean {
return retainAll(elements.convertToSetForSetOperationWith(this))
return retainAll(elements.convertToListIfNotCollection())
}
/**
@@ -175,7 +180,7 @@ public fun <T> MutableCollection<in T>.retainAll(elements: Iterable<T>): Boolean
*/
public fun <T> MutableCollection<in T>.retainAll(elements: Array<out T>): Boolean {
if (elements.isNotEmpty())
return retainAll(elements.convertToSetForSetOperation())
return retainAll(elements.asList())
else
return retainNothing()
}
@@ -184,9 +189,9 @@ public fun <T> MutableCollection<in T>.retainAll(elements: Array<out T>): Boolea
* Retains only elements of this [MutableCollection] that are contained in the given [elements] sequence.
*/
public fun <T> MutableCollection<in T>.retainAll(elements: Sequence<T>): Boolean {
val set = elements.convertToSetForSetOperation()
if (set.isNotEmpty())
return retainAll(set)
val list = elements.toList()
if (list.isNotEmpty())
return retainAll(list)
else
return retainNothing()
}