mjsax commented on code in PR #21762:
URL: https://github.com/apache/kafka/pull/21762#discussion_r2937223826
##########
streams/src/main/java/org/apache/kafka/streams/state/internals/HeadersSerializer.java:
##########
@@ -49,49 +46,65 @@
class HeadersSerializer {
/**
- * Serializes headers into a byte array using varint encoding per KIP-1271.
+ * Serializes headers into a ByteBuffer using varint encoding per KIP-1271.
* <p>
* The output format is [count][header1][header2]... without a size prefix.
* The size prefix is added by the outer serializer that uses this.
* <p>
* For null or empty headers, returns an empty byte array (0 bytes)
* instead of encoding headerCount=0 (1 byte).
+ * <p>
+ * The returned ByteBuffer may have larger capacity than actual payload
size.
+ * It's position will be set to zero, and its limit marks the payload end.
*
* @param headers the headers to serialize (can be null)
* @return the serialized byte array (empty array if headers are null or
empty)
*/
- public static byte[] serialize(final Headers headers) {
+ public static ByteBuffer serialize(final Headers headers) {
final Header[] headersArray = (headers == null) ? new Header[0] :
headers.toArray();
if (headersArray.length == 0) {
- return new byte[0];
+ return ByteBuffer.allocate(0);
}
- try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final DataOutputStream out = new DataOutputStream(baos)) {
+ // we first estimate an upper bound for the buffer we need,
+ // so we can allocate the whole buffer at once
- ByteUtils.writeVarint(headersArray.length, out);
+ // start with 5 bytes for varint encoding of header count
+ int estimatedBufferSize = 5;
+ for (final Header header : headersArray) {
+ // adding 5 bytes for varint encoding of header-key length
+ estimatedBufferSize += 5 + header.key().length();
Review Comment:
Should it not be the same?
--
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]