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
Merged
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions zio-http/src/main/scala/zio/http/codec/RichTextCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}
private[codec] final case class TransformOrFail[A, B](
codec: RichTextCodec[A],
to: A => Either[String, B],
from: B => Either[String, A],
) extends RichTextCodec[B]
private[codec] final case class Alt[A, B](left: RichTextCodec[A], right: RichTextCodec[B])
extends RichTextCodec[Either[A, B]]
private[codec] final case class Lazy[A](codec0: () => RichTextCodec[A]) extends RichTextCodec[A] {
private[codec] final case class Lazy[A](codec0: () => RichTextCodec[A]) extends RichTextCodec[A] {
lazy val codec: RichTextCodec[A] = codec0()
}
private[codec] final case class Zip[A, B, C](
Expand Down Expand Up @@ -528,9 +530,9 @@ object RichTextCodec {
case Empty =>
Right((value, ()))

case CharIn(bitset) =>
case self @ CharIn(bitset) =>
if (value.length == 0 || !bitset.contains(value.charAt(0).toInt))
Left(s"Not found: ${bitset.toArray.map(_.toChar).mkString}")
Left(self.errorMessage)
else
Right((value.subSequence(1, value.length), value.charAt(0)))

Expand Down
Loading