From 3a302f088dbec2009a8a8de219d5c62183aca1ed Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 30 Nov 2020 16:22:24 +0100 Subject: [PATCH 1/2] Improve performance of `Unlifted.unapply` * Avoid creation of `Option`s and fail fast * Align signatures --- library/src/scala/quoted/Unlifted.scala | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/library/src/scala/quoted/Unlifted.scala b/library/src/scala/quoted/Unlifted.scala index d19bbcc8bf3e..cccc9ff706e2 100644 --- a/library/src/scala/quoted/Unlifted.scala +++ b/library/src/scala/quoted/Unlifted.scala @@ -27,12 +27,13 @@ object Unlifted { * } * ``` */ - def unapply[T](exprs: Seq[Expr[T]])(using unlift: Unliftable[T], qctx: Quotes): Option[Seq[T]] = - exprs.foldRight(Option(List.empty[T])) { (elem, acc) => - (elem, acc) match { - case (Unlifted(value), Some(lst)) => Some(value :: lst) - case (_, _) => None - } - } + def unapply[T](exprs: Seq[Expr[T]])(using Unliftable[T])(using Quotes): Option[Seq[T]] = + val builder = Seq.newBuilder[T] + val iter = exprs.iterator + while iter.hasNext do + iter.next() match + case Unlifted(value) => builder += value + case _ => return None + Some(builder.result) } From 5b11dc3ff2b7ca45c89e996b0adbb456ae513c91 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 30 Nov 2020 21:45:08 +0100 Subject: [PATCH 2/2] Add missing empty arguments --- library/src/scala/quoted/Unlifted.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/scala/quoted/Unlifted.scala b/library/src/scala/quoted/Unlifted.scala index cccc9ff706e2..17af2a74fede 100644 --- a/library/src/scala/quoted/Unlifted.scala +++ b/library/src/scala/quoted/Unlifted.scala @@ -34,6 +34,6 @@ object Unlifted { iter.next() match case Unlifted(value) => builder += value case _ => return None - Some(builder.result) + Some(builder.result()) }