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

Improve RichTextCodec decoding performance #2512

Merged
merged 4 commits into from
Nov 24, 2023

Conversation

987Nabil
Copy link
Contributor

@987Nabil 987Nabil commented Nov 9, 2023

Error messages are the same per instance of CharIn. So we compute them
only once.

Error messages are the same per instance of `CharIn`. So we compute them
only once.
@codecov-commenter
Copy link

codecov-commenter commented Nov 9, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (bc6c29b) 64.94% compared to head (36626ec) 64.90%.

Files Patch % Lines
.../src/main/scala/zio/http/codec/RichTextCodec.scala 77.77% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2512      +/-   ##
==========================================
- Coverage   64.94%   64.90%   -0.04%     
==========================================
  Files         137      137              
  Lines        7243     7247       +4     
  Branches     1248     1264      +16     
==========================================
  Hits         4704     4704              
- Misses       2539     2543       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kyri-petrou
Copy link
Collaborator

@987Nabil just leaving this comment here for visibility.

Thanks for the fix but just want to raise a concern with this approach. It "fixes" the latency the issue in situations that an instance of the RichTextCodec is reused, but my worry is that at some point in a few months / years will use RichTextCodec.filter(_ => true) in a method and the same issue will arise again

I'm sure you already thought of different approaches, but does the error message needs to contain every single character in the BitSet? Perhaps it can be limited to the first 5 or 10 or even 50? If I'm understanding the code correctly, it currently contains all 65535 chars, and I'm not too sure whether that's really useful.

@@ -154,15 +154,17 @@ sealed trait RichTextCodec[A] { self =>
}
object RichTextCodec {
private[codec] case object Empty extends RichTextCodec[Unit]
private[codec] final case class CharIn(set: BitSet) extends RichTextCodec[Char]
private[codec] final case class CharIn(set: BitSet) extends RichTextCodec[Char] {
lazy val errorMessage = s"Not found: ${set.toArray.map(_.toChar).mkString}"
Copy link
Member

Choose a reason for hiding this comment

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

You can do one better by removing lazy, and adding:

private[codec] val leftErrorMessage = Left(errorMessage)

Then using leftErrorMessage below.

@987Nabil
Copy link
Contributor Author

987Nabil commented Nov 9, 2023

@kyri-petrou I see your point. I must say, the need for going with RichTextCodec.filter(_ => true) is, that one can not provide error messages. I think this could maybe already be better handled right now. I could also choose some max for adding the char to the error message.

@jdegoes
Copy link
Member

jdegoes commented Nov 10, 2023

We discussed using describe to provide compact, nice descriptions of the codec on failure.

@@ -250,6 +250,10 @@ object RichTextCodecSpec extends ZIOHttpSpec {
assertTrue(success(123) == codec.decode("123--")) &&
assertTrue(codec.decode("4123").isLeft)
},
test("With error message") {
val codec = RichTextCodec.literal("123").withError("Not 123")
assertTrue(codec.decode("678") == Left("(Expected, but did not find: Paragraph(Code(“1”,Inline)), Not 123)"))
Copy link
Member

Choose a reason for hiding this comment

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

This is rendering poorly, perhaps because describe returns Doc, that should first be properly converted into a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is actually the | operator, that chains error messages this way explicitly.

@987Nabil 987Nabil force-pushed the rich-text-codec-char-in-opt branch 2 times, most recently from bcfb4f6 to 8a3d75f Compare November 14, 2023 14:30
@987Nabil 987Nabil enabled auto-merge (squash) November 24, 2023 02:00
@987Nabil 987Nabil merged commit 41e33b5 into zio:main Nov 24, 2023
14 checks passed
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.

None yet

4 participants