Skip to content

Commit ff4d3d2

Browse files
committed
Merge pull request scala#4168 from Ichoran/issue/9000
SI-9000 equals for immutable collections should check identity
2 parents 3b1ac06 + d3bd2b7 commit ff4d3d2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/library/scala/collection/LinearSeqOptimized.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,16 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea
249249
override /*IterableLike*/
250250
def sameElements[B >: A](that: GenIterable[B]): Boolean = that match {
251251
case that1: LinearSeq[_] =>
252-
var these = this
253-
var those = that1
254-
while (!these.isEmpty && !those.isEmpty && these.head == those.head) {
255-
these = these.tail
256-
those = those.tail
252+
// Probably immutable, so check reference identity first (it's quick anyway)
253+
(this eq that1) || {
254+
var these = this
255+
var those = that1
256+
while (!these.isEmpty && !those.isEmpty && these.head == those.head) {
257+
these = these.tail
258+
those = those.tail
259+
}
260+
these.isEmpty && those.isEmpty
257261
}
258-
these.isEmpty && those.isEmpty
259262
case _ =>
260263
super.sameElements(that)
261264
}

0 commit comments

Comments
 (0)