Skip to content

Conversation

TimWSpence
Copy link

@TimWSpence TimWSpence commented May 6, 2022

In most cases (in particular for the builtin snake case and kebab case) the name transformations can be expanded at compile time. Previously they were run at runtime on every invocation of the codec. This was particularly expensive for some of the builtins like the snake_case codec as it was allocating and compiling a Pattern instance per member field per invocation of the codec.

Note however that we do need a fallback to the previous implementation for some user-provided transformations eg

//This fails without the @nowarn annotation
object Person {
  val transform: String => String = _.toLowerCase()

  @nowarn("msg=Failed to transform names at compile time. Transformation will happen at runtime on codec invocation instead. This is due to the limitations of scala.reflect.macros.blackbox.Context#eval")
  implicit val decoderForPerson: Decoder[Person] = deriveDecoder[Person](transform, true, None)
}
//This is ok
object Person {
  implicit val decoderForPerson: Decoder[Person] = deriveDecoder[Person]((s: String) => s.toLowerCase(), true, None)}

TimWSpence added 3 commits May 6, 2022 11:07
These can be run at compile time. Previously they were run at runtime on
every invocation of the codec. This was particularly expensive for some
of the builtins like the snake_case codec as it was allocating and
compiling a Pattern instance per member field per invocation of the
codec
at compile time

```
//This fails without the @nowarn annotation
object Person {
  val transform: String => String = _.toLowerCase()

  @nowarn("msg=Failed to transform member names at compile time")
  implicit val decoderForPerson: Decoder[Person] = deriveDecoder[Person](transform, true, None)
}
```

```
//This is ok
object Person {
  implicit val decoderForPerson: Decoder[Person] = deriveDecoder[Person]((s: String) => s.toLowerCase(), true, None)}
```
@TimWSpence
Copy link
Author

TimWSpence commented May 6, 2022

Possibly the compiler warning could have a suggestion of how to fix provided transformations so that they're expanded at compile time but I'm not really sure what the rules are.

I will update this ASAP with a change to add some tests for when we can't expand at compile time as I doubt that the existing tests cover this

@TimWSpence
Copy link
Author

Apologies for opening this in a half-finished state. I didn't realize at first that we can't always do the transformation at compile time due to limitations in blackbox.Context#eval. I think that it's ready now! :)

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 this pull request may close these issues.

1 participant