Skip to content

Commit ce1b0e3

Browse files
committed
Use allocateUninitializedArray for array allocations not followed by System.arraycopy.
1 parent 8e6049d commit ce1b0e3

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

java/core/src/main/java/com/google/protobuf/CodedInputStream.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,10 @@ public String readString() throws IOException {
15041504
// TODO(anuraaga): It might be possible to share the optimized loop with
15051505
// readStringRequireUtf8 by implementing Java replacement logic there.
15061506
// The same as readBytes' logic
1507-
byte[] bytes = new byte[size];
1507+
byte[] bytes =
1508+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
1509+
? UnsafeUtil.allocateUninitializedArray(size)
1510+
: new byte[size];
15081511
UnsafeUtil.copyMemory(pos, bytes, 0, size);
15091512
String result = new String(bytes, UTF_8);
15101513
pos += size;
@@ -1621,7 +1624,10 @@ public ByteString readBytes() throws IOException {
16211624
return ByteString.wrap(result);
16221625
} else {
16231626
// Use UnsafeUtil to copy the memory to bytes instead of using ByteBuffer ways.
1624-
byte[] bytes = new byte[size];
1627+
byte[] bytes =
1628+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
1629+
? UnsafeUtil.allocateUninitializedArray(size)
1630+
: new byte[size];
16251631
UnsafeUtil.copyMemory(pos, bytes, 0, size);
16261632
pos += size;
16271633
return ByteString.wrap(bytes);
@@ -1655,7 +1661,10 @@ public ByteBuffer readByteBuffer() throws IOException {
16551661
return result;
16561662
} else {
16571663
// The same as readBytes' logic
1658-
byte[] bytes = new byte[size];
1664+
byte[] bytes =
1665+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
1666+
? UnsafeUtil.allocateUninitializedArray(size)
1667+
: new byte[size];
16591668
UnsafeUtil.copyMemory(pos, bytes, 0, size);
16601669
pos += size;
16611670
return ByteBuffer.wrap(bytes);
@@ -3348,7 +3357,10 @@ public boolean readBool() throws IOException {
33483357
public String readString() throws IOException {
33493358
final int size = readRawVarint32();
33503359
if (size > 0 && size <= currentByteBufferLimit - currentByteBufferPos) {
3351-
byte[] bytes = new byte[size];
3360+
byte[] bytes =
3361+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3362+
? UnsafeUtil.allocateUninitializedArray(size)
3363+
: new byte[size];
33523364
UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
33533365
String result = new String(bytes, UTF_8);
33543366
currentByteBufferPos += size;
@@ -3476,14 +3488,19 @@ public ByteString readBytes() throws IOException {
34763488
currentByteBufferPos += size;
34773489
return result;
34783490
} else {
3479-
byte[] bytes;
3480-
bytes = new byte[size];
3491+
byte[] bytes =
3492+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3493+
? UnsafeUtil.allocateUninitializedArray(size)
3494+
: new byte[size];
34813495
UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
34823496
currentByteBufferPos += size;
34833497
return ByteString.wrap(bytes);
34843498
}
34853499
} else if (size > 0 && size <= remaining()) {
3486-
byte[] temp = new byte[size];
3500+
byte[] temp =
3501+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3502+
? UnsafeUtil.allocateUninitializedArray(size)
3503+
: new byte[size];
34873504
readRawBytesTo(temp, 0, size);
34883505
return ByteString.wrap(temp);
34893506
}
@@ -3512,13 +3529,19 @@ public ByteBuffer readByteBuffer() throws IOException {
35123529
(int) (currentByteBufferPos - currentAddress - size),
35133530
(int) (currentByteBufferPos - currentAddress));
35143531
} else {
3515-
byte[] bytes = new byte[size];
3532+
byte[] bytes =
3533+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3534+
? UnsafeUtil.allocateUninitializedArray(size)
3535+
: new byte[size];
35163536
UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
35173537
currentByteBufferPos += size;
35183538
return ByteBuffer.wrap(bytes);
35193539
}
35203540
} else if (size > 0 && size <= remaining()) {
3521-
byte[] temp = new byte[size];
3541+
byte[] temp =
3542+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3543+
? UnsafeUtil.allocateUninitializedArray(size)
3544+
: new byte[size];
35223545
readRawBytesTo(temp, 0, size);
35233546
return ByteBuffer.wrap(temp);
35243547
}
@@ -3793,13 +3816,19 @@ public byte readRawByte() throws IOException {
37933816
@Override
37943817
public byte[] readRawBytes(final int length) throws IOException {
37953818
if (length >= 0 && length <= currentRemaining()) {
3796-
byte[] bytes = new byte[length];
3819+
byte[] bytes =
3820+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3821+
? UnsafeUtil.allocateUninitializedArray(length)
3822+
: new byte[length];
37973823
UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, length);
37983824
currentByteBufferPos += length;
37993825
return bytes;
38003826
}
38013827
if (length >= 0 && length <= remaining()) {
3802-
byte[] bytes = new byte[length];
3828+
byte[] bytes =
3829+
UnsafeUtil.hasUnsafeAllocateArrayOperation()
3830+
? UnsafeUtil.allocateUninitializedArray(length)
3831+
: new byte[length];
38033832
readRawBytesTo(bytes, 0, length);
38043833
return bytes;
38053834
}

0 commit comments

Comments
 (0)