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

Do Not Attempt Decoding When We Know It Will Fail #230

Merged
merged 2 commits into from
Oct 21, 2022

Conversation

isomarcte
Copy link
Contributor

This commit changes the orDefault and orDefaultAccumulating methods to not attempt decoding when we,

  • Know we have a default value
  • We know decoding will fail.

The code is made a bit more complex and less optimal because we have to run the decoder when the value is null, though we don't when it is missing. This is because the Circe Decoder[Option[A]] has a special case for this input {"a": null}. All other types would consider the value null and the missing key "a" to be the same case and use a default, but Decoder[Option[A]] considers {"a": null} as Right(None).

See the test "Option[T] with default should be None if null decoded" for a complete example.

In the future, for defaulting codecs, we might consider treating a missing value and JNull the same in all cases, even for Option.

This commit also adds a benchmark package which was used to verify the issue referenced in circe/circe#1972.

Note, this optimization works around the issue in circe/circe#1972, but there is still an issue in circe proper for circe/circe#1972. This optimization is good with or without the issue described in circe/circe#1972.

This commit changes the `orDefault` and `orDefaultAccumulating` methods to not attempt decoding when we,

* Know we have a default value
* We know decoding will fail.

The code is made a bit more complex and less optimal because we have to run the decoder when the value is `null`, though we don't when it is missing. This is because the Circe `Decoder[Option[A]]` has a special case for this input `{"a": null}`. All other types would consider the value `null` and the missing key `"a"` to be the same case and use a default, but `Decoder[Option[A]]` considers `{"a": null}` as `Right(None)`.

See the test `"Option[T] with default should be None if null decoded"` for a complete example.

In the future, for defaulting codecs, we might consider treating a missing value and `JNull` the same in all cases, even for `Option`.

This commit also adds a benchmark package which was used to verify the issue referenced in circe/circe#1972.

Note, this optimization works _around_ the issue in circe/circe#1972, but there is still an issue in circe proper for circe/circe#1972. This optimization is good with or without the issue described in circe/circe#1972.
@isomarcte
Copy link
Contributor Author

@zarthross any thoughts here?

FWIW, I can make this better if I can match on the OptionDecoder I added in circe/circe#1974

@zarthross zarthross merged commit e36aec4 into circe:main Oct 21, 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 this pull request may close these issues.

2 participants