chaokunyang commented on code in PR #1451:
URL: https://github.com/apache/incubator-fury/pull/1451#discussion_r1555011188


##########
java/fury-core/src/main/java/org/apache/fury/memory/MemoryBuffer.java:
##########
@@ -2358,94 +2540,93 @@ public byte[] readBytesWithSizeEmbedded() {
     return arr;
   }
 
-  public byte[] readBytesAlignedSizeEmbedded() {
+  public byte[] readBytesWithAlignedSize() {
     final int numBytes = readPositiveAlignedVarInt();
     int readerIdx = readerIndex;
+    final byte[] arr = new byte[numBytes];
     // use subtract to avoid overflow
-    if (BoundsChecking.BOUNDS_CHECKING_ENABLED && readerIdx > size - numBytes) 
{
-      throw new IndexOutOfBoundsException(
-          String.format(
-              "readerIndex(%d) + length(%d) exceeds size(%d): %s",
-              readerIdx, numBytes, size, this));
+    if (readerIdx > size - numBytes) {
+      streamReader.readTo(arr, 0, numBytes);
+      return arr;
     }
-    final byte[] arr = new byte[numBytes];
     Platform.UNSAFE.copyMemory(
         this.heapMemory, this.address + readerIdx, arr, 
Platform.BYTE_ARRAY_OFFSET, numBytes);
     readerIndex = readerIdx + numBytes;
     return arr;
   }
 
-  /**
-   * This method should be used to read data written by {@link
-   * #writePrimitiveArrayWithSizeEmbedded}.
-   */
-  public char[] readCharsWithSizeEmbedded() {
-    final int numBytes = readPositiveVarInt();
+  /** This method should be used to read data written by {@link 
#writePrimitiveArrayWithSize}. */
+  public char[] readChars(int numBytes) {
     int readerIdx = readerIndex;
+    final char[] chars = new char[numBytes >> 1];
     // use subtract to avoid overflow
-    if (BoundsChecking.BOUNDS_CHECKING_ENABLED && readerIdx > size - numBytes) 
{
-      throw new IndexOutOfBoundsException(
-          String.format(
-              "readerIdx(%d) + length(%d) exceeds size(%d): %s", readerIdx, 
numBytes, size, this));
+    if (readerIdx > size - numBytes) {
+      streamReader.readToUnsafe(chars, 0, numBytes);
+      return chars;
     }
-    final char[] chars = new char[numBytes / 2];
     Platform.copyMemory(
         heapMemory, address + readerIdx, chars, Platform.CHAR_ARRAY_OFFSET, 
numBytes);
     readerIndex = readerIdx + numBytes;
     return chars;
   }
 
-  public char[] readCharsAlignedSizeEmbedded() {
-    final int numBytes = readPositiveAlignedVarInt();
+  public void readChars(char[] chars, int offset, int numBytes) {
     final int readerIdx = readerIndex;
     // use subtract to avoid overflow
-    if (BoundsChecking.BOUNDS_CHECKING_ENABLED && readerIdx > size - numBytes) 
{
-      throw new IndexOutOfBoundsException(
-          String.format(
-              "readerIdx(%d) + length(%d) exceeds size(%d): %s", readerIdx, 
numBytes, size, this));
+    if (readerIdx > size - numBytes) {
+      streamReader.readToUnsafe(chars, offset, numBytes);
+      return;
     }
-    final char[] chars = new char[numBytes / 2];
-    Platform.copyMemory(
-        heapMemory, address + readerIdx, chars, Platform.CHAR_ARRAY_OFFSET, 
numBytes);
+    Platform.copyMemory(heapMemory, address + readerIdx, chars, offset, 
numBytes);
     readerIndex = readerIdx + numBytes;
-    return chars;
   }
 
-  public long[] readLongsWithSizeEmbedded() {
-    final int numBytes = readPositiveVarInt();
+  public char[] readCharsWithAlignedSize() {
+    final int numBytes = readPositiveAlignedVarInt();
+    return readChars(numBytes);
+  }
+
+  public long[] readLongs(int numBytes) {
     int readerIdx = readerIndex;
+    int numElements = numBytes >> 3;
+    final long[] longs = new long[numElements];
     // use subtract to avoid overflow
     if (readerIdx > size - numBytes) {
-      throw new IndexOutOfBoundsException(
-          String.format(
-              "readerIdx(%d) + length(%d) exceeds size(%d): %s", readerIdx, 
numBytes, size, this));
+      streamReader.readToUnsafe(longs, 0, numElements);
+      return longs;
     }
-    final long[] longs = new long[numBytes / 8];
     Platform.copyMemory(
         heapMemory, address + readerIdx, longs, Platform.LONG_ARRAY_OFFSET, 
numBytes);
     readerIndex = readerIdx + numBytes;
     return longs;
   }
 
-  public void readChars(char[] chars, int offset, int numBytes) {
-    final int readerIdx = readerIndex;
-    // use subtract to avoid overflow
-    if (BoundsChecking.BOUNDS_CHECKING_ENABLED && readerIdx > size - numBytes) 
{
-      throw new IndexOutOfBoundsException(
-          String.format(
-              "readerIdx(%d) + length(%d) exceeds size(%d): %s", readerIdx, 
numBytes, size, this));
+  /**
+   * Bulk copy method. Copies {@code numBytes} bytes to target unsafe object 
and pointer. NOTE: This
+   * is a unsafe method, no check here, please be carefully.
+   */
+  public void readToUnsafe(Object target, long targetPointer, int numBytes) {
+    int remaining = size - readerIndex;
+    if (numBytes > remaining) {
+      streamReader.readToUnsafe(target, targetPointer, numBytes);
+    } else {
+      int readerIdx = readerIndex;
+      Platform.copyMemory(heapMemory, address + readerIdx, target, 
targetPointer, numBytes);
+      readerIndex = readerIdx + numBytes;
     }
-    Platform.copyMemory(heapMemory, address + readerIdx, chars, offset, 
numBytes);
-    readerIndex = readerIdx + numBytes;
+  }
+
+  private void fillBuffer(int minimumReadableBytes) {
+    // virtual method call has bigger code size than a method call,

Review Comment:
   I run a benchmark, the optimization doesn't make a big difference. I will 
revert it.
   ```
   Baseline:
   Benchmark                                  (bufferType)   (objectType)  
(references)   Mode  Cnt        Score       Error  Units
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
  false  thrpt   50  1576536.898 ± 54571.741  ops/s
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
   true  thrpt   50  3194178.195 ± 60680.268  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
  false  thrpt   50  1641797.023 ± 16222.155  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
   true  thrpt   50  3163612.265 ± 49756.995  ops/s
   
   Before This PR:
   Benchmark                                  (bufferType)   (objectType)  
(references)   Mode  Cnt        Score       Error  Units
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
  false  thrpt   50  2589559.872 ± 28062.176  ops/s
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
   true  thrpt   50  3468978.381 ± 37684.130  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
  false  thrpt   50  2640599.313 ± 36759.090  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
   true  thrpt   50  3451992.441 ± 51964.331  ops/s
   
   This PR with fillBuffer optimization
   Benchmark                                  (bufferType)   (objectType)  
(references)   Mode  Cnt        Score       Error  Units
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
  false  thrpt   50  2637649.628 ± 29144.769  ops/s
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
   true  thrpt   50  3481967.819 ± 38710.667  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
  false  thrpt   50  2593521.429 ± 38117.758  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
   true  thrpt   50  3424332.128 ± 53014.807  ops/s
   
   This PR without fillBuffer optimization
   Benchmark                                  (bufferType)   (objectType)  
(references)   Mode  Cnt        Score       Error  Units
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
  false  thrpt   50  2603998.829 ± 35062.683  ops/s
   UserTypeDeserializeSuite.fury_deserialize         array  STRUCT              
   true  thrpt   50  3438343.670 ± 48911.824  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
  false  thrpt   50  2631586.246 ± 34760.277  ops/s
   UserTypeDeserializeSuite.fury_deserialize  directBuffer  STRUCT              
   true  thrpt   50  3425883.355 ± 40436.642  ops/s
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to