Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split subtypes array initialization to multiple functions. #485

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions core/src/main/scala/magnolia1/magnolia.scala
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ object Magnolia {
error(s"could not find any direct subtypes of $typeSymbol")
}

val subtypesVal = c.freshName(TermName("subtypes"))
val typeclasses = for (subType <- subtypes) yield {
val path = CoproductType(genericType.toString)
val frame = stack.Frame(path, resultType, assignedName)
Expand All @@ -640,29 +639,50 @@ object Magnolia {
.fold(error(_), identity)
}

val assignments = typeclasses.zipWithIndex.map { case ((subType, typeclass), idx) =>
val symbol = subType.typeSymbol
val (annotationTrees, inheritedAnnotationTrees) = annotationsOf(symbol)
q"""$subtypesVal($idx) = $SubtypeObj[$typeConstructor, $genericType, $subType](
${typeNameOf(subType)},
$idx,
$ArrayObj(..${annotationTrees}): Array[_root_.scala.Any],
$ArrayObj(..${inheritedAnnotationTrees}): Array[_root_.scala.Any],
$ArrayObj(..${typeAnnotationsOf(symbol, fromParents = true)}): Array[_root_.scala.Any],
$CallByNeedObj($typeclass),
(t: $genericType) => t.isInstanceOf[$subType],
(t: $genericType) => t.asInstanceOf[$subType]
)"""
}

val subType = appliedType(SubtypeTpe, typeConstructor, genericType)
val groupSize = 500
val partialSubtypesVal = c.freshName(TermName("partialSubtypes"))
val (functionNames, partialAssignmentFunctions) = typeclasses
.grouped(groupSize)
.toList
.map { group =>
val name = c.freshName(TermName("partialAssignments"))
val assignments = group.zipWithIndex.map { case ((subType, typeclass), idx) =>
val symbol = subType.typeSymbol
val (annotationTrees, inheritedAnnotationTrees) = annotationsOf(symbol)
q"""$partialSubtypesVal($idx) = $SubtypeObj[$typeConstructor, $genericType, $subType](
${typeNameOf(subType)},
$idx,
$ArrayObj(..${annotationTrees}): Array[_root_.scala.Any],
$ArrayObj(..${inheritedAnnotationTrees}): Array[_root_.scala.Any],
$ArrayObj(..${typeAnnotationsOf(symbol, fromParents = true)}): Array[_root_.scala.Any],
$CallByNeedObj($typeclass),
(t: $genericType) => t.isInstanceOf[$subType],
(t: $genericType) => t.asInstanceOf[$subType]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can extract those as helper methods in the beginning to reduce the method size

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's not so easy, because if we make helper methods they need to deal with ClassTag 🤔

Copy link
Author

@nox213 nox213 Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I understand what you mean. Yes, it seems we need to generate sort of runtime reflection codes by macro if we want extract that codes as helper method. So I would say keeping the current way is better even if the runtime reflection code would reduce the method size dramatically. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's keep it like this

)"""
}
val tree = q"""def $name = {
val $partialSubtypesVal = new $ArrayClass[$subType](${assignments.size})
..$assignments
$partialSubtypesVal
}
"""

(name, tree)
}
.unzip

val subtypesVal = c.freshName(TermName("subtypes"))
val combinations = functionNames.map(name => q"""$subtypesVal ++= $name""")

Some(q"""{
val $subtypesVal = new $ArrayClass[$subType](${assignments.size})
..$assignments
..$partialAssignmentFunctions
val $subtypesVal = Array.newBuilder[$subType]
nox213 marked this conversation as resolved.
Show resolved Hide resolved
..$combinations
$typeNameDef
${c.prefix}.split(new $SealedTraitSym(
$typeName,
$subtypesVal: $ArrayClass[$subType],
$subtypesVal.result(): $ArrayClass[$subType],
$ArrayObj(..$classAnnotationTrees),
$ArrayObj(..$inheritedClassAnnotationTrees),
$ArrayObj(..$classTypeAnnotationTrees)
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/scala/magnolia1/examples/print.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait GenericPrint {
}
}

def split[T](ctx: SealedTrait[Print, T])(): Print[T] = { value =>
def split[T](ctx: SealedTrait[Print, T]): Print[T] = { value =>
ctx.split(value) { sub =>
sub.typeclass.print(sub.cast(value))
}
Expand Down