Skip to content

Commit c4011e6

Browse files
committed
ListMap.last should not generate additional garbage
Use Iterator.empty when iterating IndexedSeq and LinearSeq which are cheaply provably empty
1 parent 88de965 commit c4011e6

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

library/src/scala/collection/IndexedSeqLike.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ trait IndexedSeqLike[+A, +Repr] extends Any with SeqLike[A, Repr] {
8787
}
8888

8989
override /*IterableLike*/
90-
def iterator: Iterator[A] = new Elements(0, length)
90+
def iterator: Iterator[A] = {
91+
val len = length
92+
if (len == 0) Iterator.empty
93+
else new Elements(0, length)
94+
}
9195

9296
/* Overridden for efficiency */
9397
override def toBuffer[A1 >: A]: mutable.Buffer[A1] = {

library/src/scala/collection/LinearSeqLike.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ trait LinearSeqLike[+A, +Repr <: LinearSeqLike[A, Repr]] extends SeqLike[A, Repr
4141
override def hashCode()= scala.util.hashing.MurmurHash3.seqHash(seq) // TODO - can we get faster via "linearSeqHash" ?
4242

4343
override /*IterableLike*/
44-
def iterator: Iterator[A] = new AbstractIterator[A] {
44+
def iterator: Iterator[A] = if (self.isEmpty) Iterator.empty else new AbstractIterator[A] {
4545
var these = self
4646
def hasNext: Boolean = !these.isEmpty
4747
def next(): A =

0 commit comments

Comments
 (0)