You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/04/20 22:45:12 UTC

[GitHub] merlimat commented on a change in pull request #1361: Issue #791: Eliminate byte[] copies in AddEntry code path

merlimat commented on a change in pull request #1361: Issue #791: Eliminate byte[] copies in AddEntry code path
URL: https://github.com/apache/bookkeeper/pull/1361#discussion_r183184344
 
 

 ##########
 File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DigestManager.java
 ##########
 @@ -89,17 +107,27 @@ public static DigestManager instantiate(long ledgerId, byte[] passwd, DigestType
      */
     public ByteBufList computeDigestAndPackageForSending(long entryId, long lastAddConfirmed, long length,
             ByteBuf data) {
-        ByteBuf headersBuffer = PooledByteBufAllocator.DEFAULT.buffer(METADATA_LENGTH + macCodeLength);
-        headersBuffer.writeLong(ledgerId);
-        headersBuffer.writeLong(entryId);
-        headersBuffer.writeLong(lastAddConfirmed);
-        headersBuffer.writeLong(length);
+        ByteBuf sendBuffer = byteBufAllocator.buffer(METADATA_LENGTH + macCodeLength + data.readableBytes());
+        sendBuffer.writeLong(ledgerId);
+        sendBuffer.writeLong(entryId);
+        sendBuffer.writeLong(lastAddConfirmed);
+        sendBuffer.writeLong(length);
 
-        update(headersBuffer);
+        update(sendBuffer);
         update(data);
-        populateValueAndReset(headersBuffer);
+        populateValueAndReset(sendBuffer);
+
+        if (data.hasArray()) {
+            // fast path: single copy into existing array
+            sendBuffer.writeBytes(data.array(), data.arrayOffset(), data.readableBytes());
+        } else {
+            // fall-back to slow path with extra object allocation and copy
+            byte[] sendArray = new byte[sendBuffer.readableBytes()];
+            sendBuffer.getBytes(sendBuffer.readerIndex(), sendArray);
+            sendBuffer.writeBytes(sendArray);
+        }
 
-        return ByteBufList.get(headersBuffer, data);
+        return ByteBufList.get(sendBuffer);
 
 Review comment:
   @eolivelli this would make an additional copy of the payload buffer

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services