Skip to content

Commit

Permalink
Optimize RichTextCodec (#2597)
Browse files Browse the repository at this point in the history
* Optimize performance of RichTextCodec

* Fix compiling for Scala 2.12 & 3

* fmt

* Fix failing tests

* fmt

* Fix Scala 3 compiling
  • Loading branch information
kyri-petrou authored Jan 7, 2024
1 parent f4b2b3f commit 80d384d
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.concurrent.TimeUnit

import scala.util.Random

import zio.http.Header.ContentType
import zio.http.MediaType

import org.openjdk.jmh.annotations._
Expand All @@ -14,11 +15,24 @@ import org.openjdk.jmh.annotations._
class ProbeContentTypeBenchmark {

private val extensions = List("mp4", "def", "mp3", "js", "html", "css", "gif", "jpeg")
private val header = ContentType(MediaType.application.`json`)

@Benchmark
def benchmarkApp(): Unit = {
val rand = Random.nextInt(8)
MediaType.forFileExtension(extensions(rand))
()
}

@Benchmark
def benchmarkParseContentType(): Unit = {
ContentType.parse("application/json; charset=utf-8")
()
}

@Benchmark
def benchmarkRenderContentType(): Unit = {
ContentType.render(header)
()
}
}
57 changes: 33 additions & 24 deletions zio-http/src/main/scala/zio/http/Header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import scala.util.{Either, Failure, Success, Try}
import zio._

import zio.http.codec.RichTextCodec
import zio.http.endpoint.openapi.OpenAPI.SecurityScheme.Http
import zio.http.internal.DateEncoding

sealed trait Header {
Expand Down Expand Up @@ -2466,6 +2465,8 @@ object Header {
override def self: Self = this

override def headerType: HeaderType.Typed[ContentType] = ContentType

override lazy val renderedValue: String = ContentType.render(this)
}

object ContentType extends HeaderType {
Expand All @@ -2481,47 +2482,55 @@ object Header {
private val codec: RichTextCodec[ContentType] = {

// char `.` according to BNF not allowed as `token`, but here tolerated
val token = RichTextCodec.charsNot(' ', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=')
val token = RichTextCodec.charsNot(' ', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=')
val tokenString = token.repeat.string

val tokenQuoted = RichTextCodec.charsNot(' ', '"')
val tokenStringQuoted = RichTextCodec.charsNot(' ', '"').repeat.string

val type1 = RichTextCodec.string.collectOrFail("unsupported main type") {
val type1 = RichTextCodec.string.collectOrFail("unsupported main type") {
case value if MediaType.mainTypeMap.contains(value) => value
}
val type1x = (RichTextCodec.literalCI("x-") ~ token.repeat.string).transform[String](in => s"${in._1}${in._2}")(in => ("x-", s"${in.substring(2)}"))
val codecType1 = (type1 | type1x).transform[String](_.merge) {
val type1x = (RichTextCodec.literalCI("x-") ~ tokenString).transform[String](in => s"${in._1}${in._2}")(in => ("x-", s"${in.substring(2)}"))
val codecType1 = (type1 | type1x).transform[String](_.merge) {
case x if x.startsWith("x-") => Right(x)
case x => Left(x)
}
val codecType2 = token.repeat.string
val codecType = (codecType1 <~ RichTextCodec.char('/').const('/')) ~ codecType2
val attribute = token.repeat.string
val valueUnquoted = token.repeat.string
val valueQuoted = RichTextCodec.char('"') ~ tokenQuoted.repeat.string ~ RichTextCodec.char('"')
val value = valueQuoted | valueUnquoted

val param = ((
val forwardSlash = RichTextCodec.char('/').const('/')
val codecType = (codecType1 <~ forwardSlash) ~ tokenString
val quote = RichTextCodec.char('"')
val valueQuoted = quote ~ tokenStringQuoted ~ quote
val value = valueQuoted | tokenString

val unitChunk = Chunk.single(())
val whitespaces = RichTextCodec.whitespaceChar.repeat.transform[Char](_ => ' ')(_ => unitChunk).const(' ')
val param = ((
RichTextCodec.char(';').const(';') ~>
(RichTextCodec.whitespaceChar.repeat | RichTextCodec.empty).transform[Char](_ => ' ')(_ => Left(Chunk(()))).const(' ') ~>
attribute <~
whitespaces ~>
tokenString <~
RichTextCodec.char('=').const('=')
) ~ value)
.transformOrFailLeft[ContentType.Parameter](in => ContentType.Parameter.fromCodec(in))(in => in.toCodec)
val params = param.repeat
val params = param.repeat

(codecType ~ params).transform[ContentType] { case (mainType, subType, params) =>
ContentType(
MediaType.forContentType(s"$mainType/$subType").get,
params.collect { case p if p.key == ContentType.Parameter.Boundary.name => zio.http.Boundary(p.value) }.headOption,
params.collect { case p if p.key == ContentType.Parameter.Charset.name => java.nio.charset.Charset.forName(p.value) }.headOption,
params.collectFirst { case p if p.key == ContentType.Parameter.Boundary.name => zio.http.Boundary(p.value) },
params.collectFirst { case p if p.key == ContentType.Parameter.Charset.name => java.nio.charset.Charset.forName(p.value) },
)
}(in =>
(
in.mediaType.mainType,
in.mediaType.subType,
Chunk(
in.charset.map(in => Parameter.Charset(Parameter.Payload(Parameter.Charset.name, in, false))),
in.boundary.map(in => Parameter.Boundary(Parameter.Payload(Parameter.Boundary.name, in, false))),
).flatten,
in.mediaType.subType, {
val charset = in.charset.map(in => Parameter.Charset(Parameter.Payload(Parameter.Charset.name, in, false)))
val boundary = in.boundary.map(in => Parameter.Boundary(Parameter.Payload(Parameter.Boundary.name, in, false)))
(charset, boundary) match {
case (Some(c), Some(b)) => Chunk(c, b)
case (Some(c), _) => Chunk.single(c)
case (_, Some(b)) => Chunk.single(b)
case _ => Chunk.empty
}
},
),
)
}
Expand Down
Loading

0 comments on commit 80d384d

Please sign in to comment.