Skip to content

Commit

Permalink
optimise ByteStringInputStream.scala
Browse files Browse the repository at this point in the history
Update ByteStringInputStream.scala
  • Loading branch information
pjfanning committed Apr 23, 2024
1 parent 8d8a133 commit ab57232
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ private[http2] object ByteStringInputStream {
lookup.findVirtual(classOf[ByteString], "asInputStream", inputStreamMethodType)
}.toOption

def apply(bs: ByteString): InputStream =
byteStringInputStreamMethodTypeOpt.map { mh =>
mh.invoke(bs).asInstanceOf[InputStream]
}.getOrElse {
legacyConvert(bs)
}

private def legacyConvert(bs: ByteString): InputStream =
bs match {
case cs: ByteString1C =>
new ByteArrayInputStream(cs.toArrayUnsafe())
case _ =>
// NOTE: We actually measured recently, and compact + use array was pretty good usually
apply(bs.compact)
def apply(bs: ByteString): InputStream = bs match {
case cs: ByteString1C =>
new ByteArrayInputStream(cs.toArrayUnsafe())
case _ => {
byteStringInputStreamMethodTypeOpt.map { mh =>
mh.invoke(byteString).asInstanceOf[InputStream]

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / Compile and test (3.3, 8)

Not found: byteString

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / Compile and test (2.12, 8)

not found: value byteString

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / Compile and test (2.13, 8)

not found: value byteString

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / Compile and test (3.3, 11)

Not found: byteString

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / Compile and test (2.13, 11)

not found: value byteString

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / validate-links

not found: value byteString

Check failure on line 41 in http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

View workflow job for this annotation

GitHub Actions / Compile and test (2.12, 11)

not found: value byteString
}.getOrElse {
legacyConvert(bs)
}
}
}

private def legacyConvert(bs: ByteString): InputStream = bs match {
case cs: ByteString1C =>
new ByteArrayInputStream(cs.toArrayUnsafe())
case _ =>
// NOTE: We actually measured recently, and compact + use array was pretty good usually
legacyConvert(bs.compact)
}
}

0 comments on commit ab57232

Please sign in to comment.