timw commented on code in PR #862:
URL: https://github.com/apache/pekko-grpc/pull/862#discussion_r3851704404
##########
runtime/src/main/scala/org/apache/pekko/grpc/internal/ByteStringUtils.scala:
##########
@@ -34,26 +34,43 @@ private[grpc] object ByteStringUtils {
val initialBytes = stream.read(buffer, 0, buffer.length)
val nextByte = if (initialBytes < 0) -1 else stream.read() // Test for EOF
- if (nextByte == -1) {
- if (initialBytes < 1) ByteString.empty // EOF immediately
- else if (initialBytes > (buffer.length >> 1))
- // Most of the buffer is used — reuse it to avoid a copy
- ByteString.fromArrayUnsafe(buffer, 0, initialBytes)
- else
- // Small read from a large buffer — copy to right-size so the rest can
be GC'd
- ByteString.fromArray(buffer, 0, initialBytes)
- } else {
- val baos = new ByteArrayOutputStream(buffer.length * 2) // To avoid
immediate resize
- baos.write(buffer, 0, initialBytes)
- baos.write(nextByte)
+ if (nextByte == -1)
+ toByteStringUnsafe(buffer, initialBytes)
+ else {
+ val bsos = new ByteStringOutputStream(buffer.length * 2) // To avoid
immediate resize
+ bsos.write(buffer, 0, initialBytes)
+ bsos.write(nextByte)
var bytesRead = stream.read(buffer)
while (bytesRead >= 0) {
- baos.write(buffer, 0, bytesRead)
+ bsos.write(buffer, 0, bytesRead)
bytesRead = stream.read(buffer)
}
- ByteString.fromArrayUnsafe(baos.toByteArray)
+ bsos.toByteStringUnsafe
}
}
+
+ def toByteStringUnsafe(buf: Array[Byte], count: Int): ByteString =
+ if (count < 1)
+ ByteString.empty
+ else if (count > (buf.length >> 1))
+ // Most of the buffer is used — reuse it to avoid a copy
+ ByteString.fromArrayUnsafe(buf, 0, count)
+ else
+ // Small read from a large buffer — copy to right-size so the rest can
be GC'd
+ ByteString.fromArray(buf, 0, count)
+}
+
+/**
+ * OutputStream to ByteString adapter, avoiding copying of the buffered data
where possible.
+ */
+private[grpc] class ByteStringOutputStream(capacity: Int) extends
ByteArrayOutputStream(capacity) {
+ def this() = this(32)
Review Comment:
Probably not - just added it for consistency with the superclass default
constructor.
Will remove it as it's currently unused.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]