Skip to content

Commit afe9a44

Browse files
authored
use ByteString.toArrayUnsafe where it is safe to do so and improves performance (apache#424)
1 parent 8836b50 commit afe9a44

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/hpack/ByteStringInputStream.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private[http2] object ByteStringInputStream {
2828
bs match {
2929
case cs: ByteString1C =>
3030
// TODO optimise, ByteString needs to expose InputStream (esp if array backed, nice!)
31-
new ByteArrayInputStream(cs.toArray)
31+
new ByteArrayInputStream(cs.toArrayUnsafe())
3232
case _ =>
3333
// NOTE: We actually measured recently, and compact + use array was pretty good usually
3434
apply(bs.compact)

http/src/main/scala/org/apache/pekko/http/scaladsl/coding/DeflateCompressor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DeflateCompressor private[coding] (compressionLevel: Int) extends Compress
5151

5252
protected def compressWithBuffer(input: ByteString, buffer: Array[Byte]): ByteString = {
5353
require(deflater.needsInput())
54-
deflater.setInput(input.toArray)
54+
deflater.setInput(input.toArrayUnsafe())
5555
drainDeflater(deflater, buffer)
5656
}
5757
protected def flushWithBuffer(buffer: Array[Byte]): ByteString = {

http/src/main/scala/org/apache/pekko/http/scaladsl/coding/GzipCompressor.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private[coding] class GzipCompressor(compressionLevel: Int) extends DeflateCompr
4141
header() ++ super.finishWithBuffer(buffer) ++ trailer()
4242

4343
private def updateCrc(input: ByteString): Unit = {
44-
checkSum.update(input.toArray)
44+
checkSum.update(input.toArrayUnsafe())
4545
bytesRead += input.length
4646
}
4747
private def header(): ByteString =
@@ -117,7 +117,7 @@ private[coding] class GzipDecompressor(
117117
}
118118
private def crc16(data: ByteString) = {
119119
val crc = new CRC32
120-
crc.update(data.toArray)
120+
crc.update(data.toArrayUnsafe())
121121
crc.getValue.toInt & 0xFFFF
122122
}
123123
}

http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2FrameHpackSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ trait Http2FrameHpackSupport extends Http2FrameProbeDelegator with Http2FrameSen
5858
val decoder = new Decoder(Http2Protocol.InitialMaxHeaderListSize, Http2Protocol.InitialMaxHeaderTableSize)
5959

6060
def decodeHeaders(bytes: ByteString): Seq[(String, String)] = {
61-
val bis = new ByteArrayInputStream(bytes.toArray)
61+
val bis = new ByteArrayInputStream(bytes.toArrayUnsafe())
6262
val hs = new VectorBuilder[(String, String)]()
6363

6464
decoder.decode(bis,

0 commit comments

Comments
 (0)