Skip to content

Commit 19f05bf

Browse files
authored
Make SeqLike.distinct access HashSet once per entry
The old distinct first does a contains (`!seen(x)`), then does the insert, with each of these operations doing the full elemToEntry, index, and search dance. Since HashSet.add returns a true if the element was added (false otherwise) we can get away with doing that dance only once.
1 parent e12ac74 commit 19f05bf

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

src/library/scala/collection/SeqLike.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,8 @@ trait SeqLike[+A, +Repr] extends Any with IterableLike[A, Repr] with GenSeqLike[
507507
val b = newBuilder
508508
val seen = mutable.HashSet[A]()
509509
for (x <- this) {
510-
if (!seen(x)) {
510+
if (seen.add(x)) {
511511
b += x
512-
seen += x
513512
}
514513
}
515514
b.result()

0 commit comments

Comments
 (0)