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

Optimize the number of Transformer instances during method expansions #17

Closed
arainko opened this issue Oct 22, 2022 · 0 comments · Fixed by #19
Closed

Optimize the number of Transformer instances during method expansions #17

arainko opened this issue Oct 22, 2022 · 0 comments · Fixed by #19

Comments

@arainko
Copy link
Owner

arainko commented Oct 22, 2022

Pretty much the same as #16 but for via and intoVia, the generated code is kind of different due to beta-reduction of the lambda.
Example:

import io.github.arainko.ducktape.*

final case class WrappedInt(value: Int) extends AnyVal

final case class WrappedString(value: String) extends AnyVal

final case class TransformedWithSubtransformations[A](
  int: WrappedInt,
  string: WrappedString,
  list: List[WrappedInt],
  option: Option[A]
)

def method[A](option: Option[A], list: List[WrappedInt], string: WrappedString, int: WrappedInt) =
      TransformedWithSubtransformations[A](int, string, list, option)

val value = Input(1, "a", List(1, 2, 3), Some(4))

val actual = 
    DebugMacros.code {
       value.via(method[WrappedInt])
    }

The output is:

(({
 val option$proxy3: Option[WrappedInt] = 
  given_Transformer_Option_Option[Int, WrappedInt]((((from: Int) => (new WrappedInt(from): WrappedInt)): ToAnyVal[Int, WrappedInt])).transform(value.option)
val list$proxy3: List[WrappedInt] = 
  given_Transformer_SourceCollection_DestCollection[Int, WrappedInt, List, List]((((`from₂`: Int) => (new WrappedInt(`from₂`): WrappedInt)): ToAnyVal[Int, WrappedInt]), iterableFactory[WrappedInt]).transform(value.list)
val string$proxy3: WrappedString = 
  (((`from₃`: String) => (new WrappedString(`from₃`): WrappedString)): ToAnyVal[String, WrappedString]).transform(value.string)
val int$proxy3: WrappedInt = 
  (((`from₄`: Int) => (new WrappedInt(`from₄`): WrappedInt)): ToAnyVal[Int, WrappedInt]).transform(value.int)

method[WrappedInt](option$proxy3, list$proxy3, string$proxy3, int$proxy3)
}: Return): Return)

We can get rid of the Transformer.ToAnyVal, Transformer.FromAnyVal and Transformer.ForProduct instances by 'inlining' their insides, eg. for this line:

val string$proxy3: WrappedString = 
  (((`from₃`: String) => (new WrappedString(`from₃`): WrappedString)): ToAnyVal[String, WrappedString]).transform(value.string)

The rewritten version should just look like this:

val string$proxy3: WrappedString = new WrappedString(value.string)
@arainko arainko changed the title Optimize the number of Transformer instances during method expansions by rewriting the generated code Optimize the number of Transformer instances during method expansions Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant