Skip to content

Commit 7232231

Browse files
committed
Improve performance of Unlifted.unapply
* Avoid creation of `Option`s and fail fast * Align signatures
1 parent c372fa1 commit 7232231

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[Seq[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)