Skip to content

Commit

Permalink
Reduce allocations in compare
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWSpence committed Aug 7, 2020
1 parent 926a6db commit 2182462
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,16 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable {

final def compare[AA >: A, BB >: B](that: AA Ior BB)(implicit AA: Order[AA], BB: Order[BB]): Int =
(this, that) match {
case (Ior.Left(a1), Ior.Left(a2)) => AA.compare(a1, a2)
case (Ior.Left(_), _) => -1
case (Ior.Both(a1, b1), Ior.Both(a2, b2)) => Order[(AA, BB)].compare((a1, b1), (a2, b2))
case (Ior.Both(_, _), Ior.Left(_)) => 1
case (Ior.Both(_, _), Ior.Right(_)) => -1
case (Ior.Right(b1), Ior.Right(b2)) => BB.compare(b1, b2)
case (Ior.Right(_), _) => 1
case (Ior.Left(a1), Ior.Left(a2)) => AA.compare(a1, a2)
case (Ior.Left(_), _) => -1
case (Ior.Both(a1, b1), Ior.Both(a2, b2)) => {
val r = AA.compare(a1, a2)
if (r == 0) BB.compare(b1, b2) else r
}
case (Ior.Both(_, _), Ior.Left(_)) => 1
case (Ior.Both(_, _), Ior.Right(_)) => -1
case (Ior.Right(b1), Ior.Right(b2)) => BB.compare(b1, b2)
case (Ior.Right(_), _) => 1
}

final def show[AA >: A, BB >: B](implicit AA: Show[AA], BB: Show[BB]): String =
Expand Down

0 comments on commit 2182462

Please sign in to comment.