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

Optimise HttpContentCodec#lookup #3024

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions zio-http/shared/src/main/scala-2/zio/http/syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package zio.http

/**
* Copied from: https://github.com/ghostdogpr/caliban/pull/2366
*/
private[http] object syntax {
val NullFn: () => AnyRef = () => null

implicit final class EnrichedImmutableMapOps[K, V <: AnyRef](private val self: Map[K, V]) extends AnyVal {
def getOrElseNull(key: K): V = self.getOrElse(key, NullFn()).asInstanceOf[V]
}
}
17 changes: 17 additions & 0 deletions zio-http/shared/src/main/scala-3/zio/http/syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package zio.http

import scala.annotation.static

/**
* Copied from: https://github.com/ghostdogpr/caliban/pull/2366
*/
private[http] object syntax {
@static val NullFn: () => AnyRef = () => null

extension [K, V <: AnyRef](inline map: Map[K, V]) {
transparent inline def getOrElseNull(key: K): V = map.getOrElse(key, NullFn()).asInstanceOf[V]
}
}

// Required for @static fields
private final class syntax private
Copy link
Member Author

Choose a reason for hiding this comment

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

@kyri-petrou What is this for? Do I need it? 🤔

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio.http.codec

import scala.collection.immutable.ListMap
import scala.collection.immutable.{HashMap, ListMap}

import zio._

Expand Down Expand Up @@ -139,9 +139,11 @@ final case class HttpContentCodec[A](
}

def lookup(mediaType: MediaType): Option[BinaryCodecWithSchema[A]] = {
if (lookupCache.contains(mediaType)) {
lookupCache(mediaType)
} else {
import zio.http.syntax._

val codec = lookupCache.getOrElseNull(mediaType)
if (codec ne null) codec
else {
val codec = choices.collectFirst { case (mt, codec) if mt.matches(mediaType) => codec }
lookupCache = lookupCache + (mediaType -> codec)
codec
Expand Down
Loading