From 6a77514a835d737e3dc153366d6d64b93f0eb3bd Mon Sep 17 00:00:00 2001 From: Alexander Korepanov Date: Mon, 19 Jun 2023 14:24:19 +0200 Subject: [PATCH] [JS IR] Get rid of HashMap EqualityComparator ^KT-59001 --- .../kotlin/collections/EqualityComparator.kt | 26 ------------------- .../js/src/kotlin/collections/HashMap.kt | 9 +++---- .../kotlin/collections/InternalHashCodeMap.kt | 20 +++++++------- .../js/src/kotlin/collections/InternalMap.kt | 1 - .../kotlin/collections/InternalStringMap.kt | 2 +- 5 files changed, 14 insertions(+), 44 deletions(-) delete mode 100644 libraries/stdlib/js/src/kotlin/collections/EqualityComparator.kt diff --git a/libraries/stdlib/js/src/kotlin/collections/EqualityComparator.kt b/libraries/stdlib/js/src/kotlin/collections/EqualityComparator.kt deleted file mode 100644 index db20670fd13..00000000000 --- a/libraries/stdlib/js/src/kotlin/collections/EqualityComparator.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2010-2018 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 - -internal interface EqualityComparator { - /** - * Subclasses must override to return a value indicating - * whether or not two keys or values are equal. - */ - abstract fun equals(value1: Any?, value2: Any?): Boolean - - /** - * Subclasses must override to return the hash code of a given key. - */ - abstract fun getHashCode(value: Any?): Int - - - object HashCode : EqualityComparator { - override fun equals(value1: Any?, value2: Any?): Boolean = value1 == value2 - - override fun getHashCode(value: Any?): Int = value?.hashCode() ?: 0 - } -} \ No newline at end of file diff --git a/libraries/stdlib/js/src/kotlin/collections/HashMap.kt b/libraries/stdlib/js/src/kotlin/collections/HashMap.kt index 63d98ee6d27..f52be877fc9 100644 --- a/libraries/stdlib/js/src/kotlin/collections/HashMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/HashMap.kt @@ -49,17 +49,14 @@ public actual open class HashMap : AbstractMutableMap, MutableMap - private val equality: EqualityComparator - internal constructor(internalMap: InternalMap) : super() { this.internalMap = internalMap - this.equality = internalMap.equality } /** * Creates a new empty [HashMap]. */ - actual constructor() : this(InternalHashCodeMap(EqualityComparator.HashCode)) + actual constructor() : this(InternalHashCodeMap()) /** * Creates a new empty [HashMap] with the specified initial capacity and load factor. @@ -110,7 +107,7 @@ public actual open class HashMap : AbstractMutableMap, MutableMap>? = null actual override val entries: MutableSet> @@ -138,5 +135,5 @@ public actual open class HashMap : AbstractMutableMap, MutableMap stringMapOf(vararg pairs: Pair): HashMap { - return HashMap(InternalStringMap(EqualityComparator.HashCode)).apply { putAll(pairs) } + return HashMap(InternalStringMap()).apply { putAll(pairs) } } diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalHashCodeMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalHashCodeMap.kt index b6fc524bc0f..9526bee609b 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalHashCodeMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalHashCodeMap.kt @@ -24,14 +24,13 @@ import kotlin.collections.AbstractMutableMap.SimpleEntry * have the same hash, each value in hashCodeMap is actually an array containing all entries whose * keys share the same hash. */ -internal class InternalHashCodeMap(override val equality: EqualityComparator) : InternalMap { - +internal class InternalHashCodeMap : InternalMap { private var backingMap: dynamic = createJsMap() override var size: Int = 0 private set override fun put(key: K, value: V): V? { - val hashCode = equality.getHashCode(key) + val hashCode = hash(key) val chainOrEntry = getChainOrEntryOrNull(hashCode) if (chainOrEntry == null) { // This is a new chain, put it to the map. @@ -40,7 +39,7 @@ internal class InternalHashCodeMap(override val equality: EqualityComparat if (chainOrEntry !is Array<*>) { // It is an entry val entry: SimpleEntry = chainOrEntry - if (equality.equals(entry.key, key)) { + if (entry.key == key) { return entry.setValue(value) } else { backingMap[hashCode] = arrayOf(entry, SimpleEntry(key, value)) @@ -63,11 +62,11 @@ internal class InternalHashCodeMap(override val equality: EqualityComparat } override fun remove(key: K): V? { - val hashCode = equality.getHashCode(key) + val hashCode = hash(key) val chainOrEntry = getChainOrEntryOrNull(hashCode) ?: return null if (chainOrEntry !is Array<*>) { val entry: MutableEntry = chainOrEntry - if (equality.equals(entry.key, key)) { + if (entry.key == key) { jsDeleteProperty(backingMap, hashCode) size-- return entry.value @@ -78,7 +77,7 @@ internal class InternalHashCodeMap(override val equality: EqualityComparat val chain: Array> = chainOrEntry for (index in chain.indices) { val entry = chain[index] - if (equality.equals(key, entry.key)) { + if (key == entry.key) { if (chain.size == 1) { chain.asDynamic().length = 0 // remove the whole array @@ -106,10 +105,10 @@ internal class InternalHashCodeMap(override val equality: EqualityComparat override fun get(key: K): V? = getEntry(key)?.value private fun getEntry(key: K): MutableEntry? { - val chainOrEntry = getChainOrEntryOrNull(equality.getHashCode(key)) ?: return null + val chainOrEntry = getChainOrEntryOrNull(hash(key)) ?: return null if (chainOrEntry !is Array<*>) { val entry: MutableEntry = chainOrEntry - if (equality.equals(entry.key, key)) { + if (entry.key == key) { return entry } else { return null @@ -121,7 +120,7 @@ internal class InternalHashCodeMap(override val equality: EqualityComparat } private fun Array>.findEntryInChain(key: K): MutableEntry? = - firstOrNull { entry -> equality.equals(entry.key, key) } + firstOrNull { entry -> entry.key == key } override fun iterator(): MutableIterator> { @@ -187,4 +186,5 @@ internal class InternalHashCodeMap(override val equality: EqualityComparat return if (chainOrEntry === undefined) null else chainOrEntry } + private fun hash(key: K) = key?.hashCode() ?: 0 } diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt index 6c92894b3cc..24ac3840222 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt @@ -9,7 +9,6 @@ package kotlin.collections * The common interface of [InternalStringMap] and [InternalHashCodeMap]. */ internal interface InternalMap : MutableIterable> { - val equality: EqualityComparator val size: Int operator fun contains(key: K): Boolean operator fun get(key: K): V? diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt index 2d588f098fd..32a4bd9ecca 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt @@ -17,7 +17,7 @@ import kotlin.collections.MutableMap.MutableEntry * because we want to have it erased to Any? in order not to generate type-safe override bridges for * [get], [contains], [remove] etc, if they ever are generated. */ -internal class InternalStringMap(override val equality: EqualityComparator) : InternalMap { +internal class InternalStringMap : InternalMap { private var backingMap: dynamic = createJsMap() override var size: Int = 0