Skip to content

Commit b2a09d3

Browse files
Merge pull request scala#10561 from dotty-staging/improve-Unlifted-performance
Improve performance of `Unlifted.unapply`
2 parents 7d9bc16 + 5b11dc3 commit b2a09d3

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

library/src/scala/quoted/Unlifted.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ object Unlifted {
2727
* }
2828
* ```
2929
*/
30-
def unapply[T](exprs: Seq[Expr[T]])(using unlift: Unliftable[T], qctx: Quotes): Option[Seq[T]] =
31-
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
32-
(elem, acc) match {
33-
case (Unlifted(value), Some(lst)) => Some(value :: lst)
34-
case (_, _) => None
35-
}
36-
}
30+
def unapply[T](exprs: Seq[Expr[T]])(using Unliftable[T])(using Quotes): Option[Seq[T]] =
31+
val builder = Seq.newBuilder[T]
32+
val iter = exprs.iterator
33+
while iter.hasNext do
34+
iter.next() match
35+
case Unlifted(value) => builder += value
36+
case _ => return None
37+
Some(builder.result())
3738

3839
}

0 commit comments

Comments
 (0)