Skip to content

Commit

Permalink
Add fusionMaxStackDepth constant
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru committed Mar 14, 2018
1 parent 108a313 commit f0db124
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions core/src/main/scala/cats/data/AndThen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private[cats] sealed abstract class AndThen[-T, +R]
// Fusing calls up to a certain threshold, using the fusion
// technique implemented for `cats.effect.IO#map`
this match {
case Single(f, index) if index != 127 =>
case Single(f, index) if index != fusionMaxStackDepth =>
Single(f.andThen(g), index + 1)
case _ =>
andThenF(AndThen(g))
Expand All @@ -40,7 +40,7 @@ private[cats] sealed abstract class AndThen[-T, +R]
// Fusing calls up to a certain threshold, using the fusion
// technique implemented for `cats.effect.IO#map`
this match {
case Single(f, index) if index != 127 =>
case Single(f, index) if index != fusionMaxStackDepth =>
Single(f.compose(g), index + 1)
case _ =>
composeF(AndThen(g))
Expand Down Expand Up @@ -109,4 +109,18 @@ private[cats] object AndThen {
extends AndThen[A, B]
private final case class Concat[-A, E, +B](left: AndThen[A, E], right: AndThen[E, B])
extends AndThen[A, B]

/**
* Establishes the maximum stack depth when fusing `andThen` or
* `compose` calls.
*
* The default is `128`, from which we substract one as an optimization,
* a "!=" comparisson being slightly more efficient than a "<".
*
* This value was reached by taking into account the default stack
* size as set on 32 bits or 64 bits, Linux or Windows systems,
* being enough to notice performance gains, but not big enough
* to be in danger of triggering a stack-overflow error.
*/
private final val fusionMaxStackDepth = 127
}

0 comments on commit f0db124

Please sign in to comment.