Skip to content

Commit 1d3334f

Browse files
authored
Merge pull request #567 from plokhotnyuk/scala3
Add def `rawConstruct(fieldValues: Array[Any]): Type` to reduce allocations and CPU usage
2 parents f4945a2 + e84454c commit 1d3334f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

core/src/main/scala/magnolia1/impl.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ object CaseClassDerivation:
6262
typeAnnotations
6363
):
6464
def construct[PType: ClassTag](makeParam: Param => PType): A =
65-
product.fromProduct(Tuple.fromArray(parameters.map(makeParam).to(Array)))
65+
product.fromProduct(Tuple.fromIArray(parameters.map(makeParam)))
6666

67-
def rawConstruct(fieldValues: Seq[Any]): A =
68-
product.fromProduct(Tuple.fromArray(fieldValues.to(Array)))
67+
override def rawConstruct(fieldValues: Seq[Any]): A =
68+
rawConstruct(fieldValues.toArray)
69+
70+
override def rawConstruct(fieldValues: Array[Any]): A =
71+
product.fromProduct(Tuple.fromArray(fieldValues))
6972

7073
def constructEither[Err, PType: ClassTag](
7174
makeParam: Param => Either[Err, PType]

core/src/main/scala/magnolia1/interface.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package magnolia1
33
import magnolia1.CaseClass.getDefaultEvaluatorFromDefaultVal
44

55
import scala.annotation.tailrec
6+
import scala.collection.immutable.ArraySeq
67
import scala.reflect.*
78

89
case class TypeInfo(
@@ -156,6 +157,7 @@ abstract class CaseClass[Typeclass[_], Type](
156157
makeParam: Param => Either[Err, PType]
157158
): Either[List[Err], Type]
158159
def rawConstruct(fieldValues: Seq[Any]): Type
160+
def rawConstruct(fieldValues: Array[Any]): Type = rawConstruct(ArraySeq.unsafeWrapArray(fieldValues))
159161

160162
def param[P](
161163
name: String,

0 commit comments

Comments
 (0)