Skip to content

Commit 7aae71d

Browse files
mkeskellsretronym
authored andcommitted
add some comments, and avoid the equals call in some cases
1 parent dd9400f commit 7aae71d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/src/scala/collection/GenMapLike.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ trait GenMapLike[K, +V, +Repr] extends GenIterableLike[(K, V), Repr] with Equals
124124
try {
125125
val checker = new AbstractFunction1[(K, V),Boolean] with Function0[V]{
126126
override def apply(kv: (K,V)): Boolean = {
127-
that.getOrElse(kv._1.asInstanceOf[b], this) == kv._2
127+
// Note: uncurry optimizes this to `get.getOrElse(..., this: Function0)`; there is no extra lambda allocated.
128+
val v2 = that.getOrElse(kv._1.asInstanceOf[b], this.apply())
129+
// A mis-behaving user-defined equals method might not expect the sentinel value, and we should try to limit
130+
// the chance of it escaping. Its also probably quicker to avoid the virtual call to equals.
131+
(v2.asInstanceOf[AnyRef] ne this) && v2 == kv._2
128132
}
129133
override def apply(): V = this.asInstanceOf[V]
130134
}

0 commit comments

Comments
 (0)