From e975a2d3ef7edb692e543ed44fe5bd45d5811f5b Mon Sep 17 00:00:00 2001 From: Travis Brown Date: Thu, 27 Feb 2020 16:10:38 +0100 Subject: [PATCH] Reinstate @johnynek's optimization from #3279 (#3321) --- .../src/main/scala/cats/kernel/CommutativeSemigroup.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/src/main/scala/cats/kernel/CommutativeSemigroup.scala b/kernel/src/main/scala/cats/kernel/CommutativeSemigroup.scala index ed2a036c30..9cdb22c6a2 100644 --- a/kernel/src/main/scala/cats/kernel/CommutativeSemigroup.scala +++ b/kernel/src/main/scala/cats/kernel/CommutativeSemigroup.scala @@ -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)) + } } }