pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] rdhabalia commented on a change in pull request #882: Use thread local to allocate temp byte[] instead of allocator
Date Thu, 01 Jan 1970 00:00:00 GMT
rdhabalia 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_r148906436
 
 

 ##########
 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:
   > 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
   
   What if very first request is 1KB and rest of them are 256 bytes in that case also we will
keep 1KB because we won't reduce it back. right? So, if that is the case then probably below
simple thing also would work as we will increase only if request has higher size than require
and we will never reduce it.
   ```
   if (localBuf == null || localBuf.length < size) {
       localBuf = new byte[Math.max(size, 1024)];
   }
   ```
   
   But as we have limited number of io threads so, I am fine with either way. 

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

Mime
View raw message