Skip to content

Commit

Permalink
Reinstate @johnynek's optimization from #3279 (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
travisbrown authored Feb 27, 2020
1 parent d383422 commit e975a2d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions kernel/src/main/scala/cats/kernel/CommutativeSemigroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ trait CommutativeSemigroup[@sp(Int, Long, Float, Double) A] extends Any with Sem
new CommutativeSemigroup[A] {
def combine(a: A, b: A): A =
self.combine(a, self.combine(middle, b))

override def combineN(a: A, n: Int): A =
if (n <= 1) self.combineN(a, n)
else {
// a + m + a ... = combineN(a, n) + combineN(m, n - 1)
self.combine(self.combineN(a, n), self.combineN(middle, n - 1))
}
}
}

Expand Down

0 comments on commit e975a2d

Please sign in to comment.