You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2017/11/03 22:14:11 UTC

[GitHub] merlimat commented on a change in pull request #882: Use thread local to allocate temp byte[] instead of allocator

merlimat commented on a change in pull request #882: Use thread local to allocate temp byte[] instead of allocator
URL: https://github.com/apache/incubator-pulsar/pull/882#discussion_r148904088
 
 

 ##########
 File path: pulsar-common/src/main/java/org/apache/pulsar/common/util/protobuf/ByteBufCodedInputStream.java
 ##########
 @@ -145,20 +144,22 @@ public void readMessage(final ByteBufMessageBuilder builder, final ExtensionRegi
         buf.writerIndex(writerIdx);
     }
 
+    private static final FastThreadLocal<byte[]> localByteArray = new FastThreadLocal<>();
+
     /** Read a {@code bytes} field value from the stream. */
     public ByteString readBytes() throws IOException {
         final int size = readRawVarint32();
         if (size == 0) {
             return ByteString.EMPTY;
         } else {
-            RecyclableHeapByteBuf heapBuf = RecyclableHeapByteBuf.get();
-            if (size > heapBuf.writableBytes()) {
-                heapBuf.capacity(size);
+            byte[] localBuf = localByteArray.get();
+            if (localBuf == null || localBuf.length < size) {
+                localBuf = new byte[Math.max(size, 1024)];
 
 Review comment:
   Ideally, we want to minimize the number of times that we have to re-assign the `localBuf`. For example, if we're deserializing strings, we might have different sizes: 16, 18, 25, 29. 
   
   Each time the size increases, we have to re-allocate the temp buffer and GC the old one. 
   
   By starting with 1KB, we should cover most cases and ideally never reallocate it. 
   
   Another strategy could be to start a bit smaller (say 128 or 256 bytes) and when we need to increase, add a 20% padding on top, such that if there are similar sized string soon after, we don't need to reallocate again. What do you think?
   

----------------------------------------------------------------
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