aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@vespa.ai>2024-02-22 14:55:19 +0000
committerTor Brede Vekterli <vekterli@vespa.ai>2024-02-22 15:13:55 +0000
commitfabb09b9e5ed9a26a81af6742470563147011064 (patch)
treeefdad01e1ca694d7784434668fedff5e8688f1b6 /documentapi
parent70c3280d9423f0ead4592814aa6a6f93911d97a8 (diff)
Use temporary byte array when serializing Protobuf in Java
The previous attempt at avoiding unneeded memory allocation and copying was futile because--obviously in retrospect--the underlying byte buffer is fixed in size and not dynamically growable. This did not manifest itself in any unit tests (too little data) or the set of system tests that I ran manually. It seems likely that we want to reconsider the encode/decode APIs in the `DocumentProtocol` to allow for more optimal memory management.
Diffstat (limited to 'documentapi')
-rw-r--r--documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/RoutableFactories80.java11
1 files changed, 4 insertions, 7 deletions
diff --git a/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/RoutableFactories80.java b/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/RoutableFactories80.java
index a4dcd660ab8..2d29697717b 100644
--- a/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/RoutableFactories80.java
+++ b/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/RoutableFactories80.java
@@ -59,13 +59,10 @@ abstract class RoutableFactories80 {
public boolean encode(Routable obj, DocumentSerializer out) {
try {
var protoMsg = encoderFn.apply(apiClass.cast(obj));
- var protoStream = CodedOutputStream.newInstance(out.getBuf().getByteBuffer()); // Not AutoCloseable...
- try {
- protoMsg.writeTo(protoStream);
- } finally {
- protoStream.flush();
- }
- } catch (IOException | UnsupportedOperationException e) {
+ // TODO avoid this buffer indirection by directly exposing an OutputStream to write into...!
+ // ... or at the very least have a way to preallocate buffer output of protoMsg.getSerializedSize() bytes!
+ out.getBuf().put(protoMsg.toByteArray());
+ } catch (RuntimeException e) {
logCodecError("encoding", e);
return false;
}