private[this] def simplifyAndPruneRoots()

in src/main/scala/com/twitter/stitch/Pending.scala [132:159]


  private[this] def simplifyAndPruneRoots(): Unit = {
    var i = 0
    var j = 0

    while (i < roots.length) {
      @tailrec def loop(s: Stitch[_]): Unit =
        s.simplify(this) match {
          case Stitch.Const(r) =>
            // satisfy the promise if the primary root is satisfied
            if (i == 0) p.updateIfEmpty(r.asInstanceOf[Try[T]])
          case s if !recursions.isEmpty =>
            // evaluate recursions and re-simplify
            do {
              recursions.poll().eval()
            } while (!recursions.isEmpty)
            // update root to avoid memory leak
            roots(i) = s
            loop(s)
          case s =>
            roots(j) = s
            j += 1
        }
      loop(roots(i))
      i += 1
    }

    roots.reduceToSize(j)
  }