Skip to content

Commit 7d72438

Browse files
authored
Merge pull request scala/scala#8575 from rorygraves/mike/2.12.x-quickHashMapEquals
faster HashMap equality check
2 parents 431b476 + 76fad6f commit 7d72438

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

library/src/scala/collection/immutable/HashMap.scala

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ package scala
1414
package collection
1515
package immutable
1616

17+
import java.{util => ju}
18+
1719
import generic._
1820
import scala.annotation.unchecked.{uncheckedVariance => uV}
1921
import parallel.immutable.ParHashMap
@@ -248,6 +250,18 @@ object HashMap extends ImmutableMapFactory[HashMap] with BitOperations.Int {
248250
protected override def merge0[B1 >: B](that: HashMap[A, B1], level: Int, merger: Merger[A, B1]): HashMap[A, B1] = {
249251
that.updated0(key, hash, level, value, kv, merger.invert)
250252
}
253+
254+
override def equals(that: Any): Boolean = {
255+
that match {
256+
case hm: HashMap1[_,_] =>
257+
(this eq hm) ||
258+
(hm.hash == hash && hm.key == key && hm.value == value)
259+
case _: HashMap[_, _] =>
260+
false
261+
case _ =>
262+
super.equals(that)
263+
}
264+
}
251265
}
252266

253267
private[collection] class HashMapCollision1[A, +B](private[collection] val hash: Int, val kvs: ListMap[A, B @uV])
@@ -319,6 +333,19 @@ object HashMap extends ImmutableMapFactory[HashMap] with BitOperations.Int {
319333
for (p <- kvs) m = m.updated0(p._1, this.hash, level, p._2, p, merger.invert)
320334
m
321335
}
336+
337+
override def equals(that: Any): Boolean = {
338+
that match {
339+
case hm: HashMapCollision1[_,_] =>
340+
(this eq hm) ||
341+
(hm.hash == hash && hm.kvs == kvs)
342+
case _: HashMap[_, _] =>
343+
false
344+
case _ =>
345+
super.equals(that)
346+
}
347+
}
348+
322349
}
323350

324351
@deprecatedInheritance("This class will be made final in a future release.", "2.12.2")
@@ -578,6 +605,22 @@ object HashMap extends ImmutableMapFactory[HashMap] with BitOperations.Int {
578605
case hm: HashMap[_, _] => this
579606
case _ => sys.error("section supposed to be unreachable.")
580607
}
608+
609+
override def equals(that: Any): Boolean = {
610+
that match {
611+
case hm: HashTrieMap[_, _] =>
612+
(this eq hm) || {
613+
this.bitmap == hm.bitmap &&
614+
this.size0 == hm.size0 &&
615+
ju.Arrays.equals(this.elems.asInstanceOf[Array[AnyRef]], hm.elems.asInstanceOf[Array[AnyRef]])
616+
}
617+
case _: HashMap[_, _] =>
618+
false
619+
case _ =>
620+
super.equals(that)
621+
}
622+
}
623+
581624
}
582625

583626
/**

0 commit comments

Comments
 (0)