You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2019/07/12 23:27:43 UTC

[GitHub] [incubator-pinot] buchireddy commented on a change in pull request #4432: #4401 Reuse a ThreadLocal byte[] when reading String elements from the variable length value reader.

buchireddy commented on a change in pull request #4432: #4401 Reuse a ThreadLocal byte[] when reading String elements from the variable length value reader.
URL: https://github.com/apache/incubator-pinot/pull/4432#discussion_r303176579
 
 

 ##########
 File path: pinot-core/src/main/java/org/apache/pinot/core/io/util/VarLengthBytesValueReaderWriter.java
 ##########
 @@ -220,13 +232,25 @@ public String getPaddedString(int index, int numBytesPerValue, byte[] buffer) {
     // To get the length of the byte array, we use the next byte array offset.
     int length = _dataBuffer.getInt(_dataSectionStartOffSet + Integer.BYTES * (index + 1)) - offset;
 
+    // Since the byte[] is returned to the caller and caller could hold a reference to it,
+    // the buffer can't really be reused here. Hence, don't use ThreadLocal buffer in this case.
+    return getBytes(buffer, offset, length, false);
+  }
+
+  private byte[] getBytes(byte[] buffer, int offset, int length, boolean useThreadLocalBuffer) {
     byte[] b;
-    // If the caller didn't pass a buffer, create one with exact length.
-    if (buffer == null) {
-      b = new byte[length];
+    // If the caller has passed a buffer that can be used, use it.
+    if (buffer != null && buffer.length == length) {
+      b = buffer;
+    } else if (useThreadLocalBuffer) {
+      // Check if the current instance of ThreadLocal buffer is big enough. If not, resize it to double the size.
+      while (_reusableBytes.get().length < length) {
+        _reusableBytes.set(new byte[_reusableBytes.get().length * 2]);
 
 Review comment:
   Done.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org