Skip to content

Commit

Permalink
ProtobufSerializer minimize getSerializedSize() usage (apple#2694)
Browse files Browse the repository at this point in the history
Motivation:
ProtobufSerializer uses `MessageLite.getSerializedSize()` on each
serialization operation. This method may require a deep traversal
of all internal objects and require intermediate allocations
(e.g. for maps) to compute the actual size.

Modifications:
- Use `CodedOutputStream.getTotalBytesWritten()` and
`CodedInputStream.getTotalBytesRead()` to determine how many bytes
were generated/consumed during serialization.
  • Loading branch information
Scottmitch authored Sep 18, 2023
1 parent 78d4050 commit c9b1fc5
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void serialize(final T toSerialize, final BufferAllocator allocator, fina
}

// Forward write index of our buffer
buffer.writerIndex(writerIdx + toSerialize.getSerializedSize());
buffer.writerIndex(writerIdx + out.getTotalBytesWritten());
}

@Override
Expand All @@ -92,7 +92,7 @@ public T deserialize(final Buffer serializedData, final BufferAllocator allocato
}

T result = parser.parseFrom(in);
serializedData.skipBytes(result.getSerializedSize());
serializedData.skipBytes(in.getTotalBytesRead());
return result;
} catch (InvalidProtocolBufferException e) {
throw new SerializationException(e);
Expand Down

0 comments on commit c9b1fc5

Please sign in to comment.