Skip to content

Commit

Permalink
Optimise IndexedSeqOps.fold{Left,Right}
Browse files Browse the repository at this point in the history
Inherited foldRight implementation calls reverse.foldLeft
  • Loading branch information
japgolly committed Apr 27, 2020
1 parent f7ab274 commit c33a0d0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/library/scala/collection/IndexedSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ trait IndexedSeqOps[+A, +CC[_], +C] extends Any with SeqOps[A, CC, C] { self =>
} else Iterator.empty.next()
}

override def foldLeft[B](z: B)(f: (B, A) => B): B = {
// For Vectors with sizes of [100, 1000, 10000] this is [1.2, 1.6, 1.0]x as fast
// as using while+iterator.
var b = z
var i = 0
while (i < length) {
val a = self(i)
b = f(b, a)
i += 1
}
b
}

override def foldRight[B](z: B)(f: (A, B) => B): B = {
// This is 0.28% slower at size=100, 0.8% faster at size=1000, and 2.4% faster at size=10000,
// than using while+reverseIterator.
var b = z
var i = length
while (i > 0) {
i -= 1
val a = self(i)
b = f(a, b)
}
b
}

override def view: IndexedSeqView[A] = new IndexedSeqView.Id[A](this)

@deprecated("Use .view.slice(from, until) instead of .view(from, until)", "2.13.0")
Expand Down

0 comments on commit c33a0d0

Please sign in to comment.