chaokunyang commented on code in PR #1451:
URL: https://github.com/apache/incubator-fury/pull/1451#discussion_r1554981574
##########
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:
Yes, it's the bytecode size of the method. Invokng directlt will have
bigger code size. Since this invocation is not in critical path, we can move
it to a separate method.
--
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]