This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-grpc.git
The following commit(s) were added to refs/heads/main by this push:
new b9523b3b try to improve the memory usage in ByteStringUtil (#826)
b9523b3b is described below
commit b9523b3b171c8c677d226dc0bc2bb6fdef7c0a94
Author: PJ Fanning <[email protected]>
AuthorDate: Tue Aug 4 09:41:13 2026 +0100
try to improve the memory usage in ByteStringUtil (#826)
---
.../org/apache/pekko/grpc/internal/ByteStringUtils.scala | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git
a/runtime/src/main/scala/org/apache/pekko/grpc/internal/ByteStringUtils.scala
b/runtime/src/main/scala/org/apache/pekko/grpc/internal/ByteStringUtils.scala
index 40e835b0..f970c862 100644
---
a/runtime/src/main/scala/org/apache/pekko/grpc/internal/ByteStringUtils.scala
+++
b/runtime/src/main/scala/org/apache/pekko/grpc/internal/ByteStringUtils.scala
@@ -35,12 +35,13 @@ private[grpc] object ByteStringUtils {
val nextByte = if (initialBytes < 0) -1 else stream.read() // Test for EOF
if (nextByte == -1) {
- if (initialBytes < 1) pekko.util.ByteString.empty // EOF immediately
- else {
- // WARNING: buffer is retained in full below,
- // which could be problematic if ProtobufSerializer.deserialize keeps
a reference to the ByteString
- pekko.util.ByteString.fromArrayUnsafe(buffer, 0, initialBytes)
- }
+ 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)
@@ -52,7 +53,7 @@ private[grpc] object ByteStringUtils {
bytesRead = stream.read(buffer)
}
- pekko.util.ByteString.fromArrayUnsafe(baos.toByteArray)
+ ByteString.fromArrayUnsafe(baos.toByteArray)
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]