Skip to content

Commit 810272f

Browse files
Avoid boxing/unboxing varint, fixed32, and fixed64 fields in UnknownFieldSet.Field
PiperOrigin-RevId: 810678393
1 parent 07e072a commit 810272f

File tree

6 files changed

+92
-35
lines changed

6 files changed

+92
-35
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ private BooleanArrayList(boolean[] other, int size, boolean isMutable) {
5555
this.size = size;
5656
}
5757

58+
/**
59+
* Constructs a new mutable {@code BooleanArrayList} containing the same elements as {@code
60+
* other}.
61+
*/
62+
BooleanArrayList(BooleanArrayList other, boolean isMutable) {
63+
this(
64+
other.size == 0 ? EMPTY_ARRAY : Arrays.copyOf(other.array, other.size),
65+
other.size,
66+
isMutable);
67+
}
68+
5869
@Override
5970
protected void removeRange(int fromIndex, int toIndex) {
6071
ensureIsMutable();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ private DoubleArrayList(double[] other, int size, boolean isMutable) {
5454
this.size = size;
5555
}
5656

57+
/**
58+
* Constructs a new mutable {@code DoubleArrayList} containing the same elements as {@code other}.
59+
*/
60+
DoubleArrayList(DoubleArrayList other, boolean isMutable) {
61+
this(
62+
other.size == 0 ? EMPTY_ARRAY : Arrays.copyOf(other.array, other.size),
63+
other.size,
64+
isMutable);
65+
}
66+
5767
@Override
5868
protected void removeRange(int fromIndex, int toIndex) {
5969
ensureIsMutable();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ private FloatArrayList(float[] other, int size, boolean isMutable) {
5454
this.size = size;
5555
}
5656

57+
/**
58+
* Constructs a new mutable {@code FloatArrayList} containing the same elements as {@code other}.
59+
*/
60+
FloatArrayList(FloatArrayList other, boolean isMutable) {
61+
this(
62+
other.size == 0 ? EMPTY_ARRAY : Arrays.copyOf(other.array, other.size),
63+
other.size,
64+
isMutable);
65+
}
66+
5767
@Override
5868
protected void removeRange(int fromIndex, int toIndex) {
5969
ensureIsMutable();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ private IntArrayList(int[] other, int size, boolean isMutable) {
5454
this.size = size;
5555
}
5656

57+
/**
58+
* Constructs a new mutable {@code IntArrayList} containing the same elements as {@code other}.
59+
*/
60+
IntArrayList(IntArrayList other, boolean isMutable) {
61+
this(
62+
other.size == 0 ? EMPTY_ARRAY : Arrays.copyOf(other.array, other.size),
63+
other.size,
64+
isMutable);
65+
}
66+
5767
@Override
5868
protected void removeRange(int fromIndex, int toIndex) {
5969
ensureIsMutable();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ private LongArrayList(long[] other, int size, boolean isMutable) {
5454
this.size = size;
5555
}
5656

57+
/**
58+
* Constructs a new mutable {@code LongArrayList} containing the same elements as {@code other}.
59+
*/
60+
LongArrayList(LongArrayList other, boolean isMutable) {
61+
this(
62+
other.size == 0 ? EMPTY_ARRAY : Arrays.copyOf(other.array, other.size),
63+
other.size,
64+
isMutable);
65+
}
66+
5767
@Override
5868
protected void removeRange(int fromIndex, int toIndex) {
5969
ensureIsMutable();

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

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import java.io.InputStream;
1313
import java.io.OutputStream;
1414
import java.util.ArrayList;
15-
import java.util.Arrays;
1615
import java.util.Collections;
1716
import java.util.List;
1817
import java.util.Map;
18+
import java.util.Objects;
1919
import java.util.TreeMap;
2020

2121
/**
@@ -749,17 +749,23 @@ public boolean equals(
749749
if (!(other instanceof Field)) {
750750
return false;
751751
}
752-
return Arrays.equals(getIdentityArray(), ((Field) other).getIdentityArray());
752+
Field that = (Field) other;
753+
return Objects.equals(varint, that.varint)
754+
&& Objects.equals(fixed32, that.fixed32)
755+
&& Objects.equals(fixed64, that.fixed64)
756+
&& Objects.equals(lengthDelimited, that.lengthDelimited)
757+
&& Objects.equals(group, that.group);
753758
}
754759

755760
@Override
756761
public int hashCode() {
757-
return Arrays.hashCode(getIdentityArray());
758-
}
759-
760-
/** Returns the array of objects to be used to uniquely identify this {@link Field} instance. */
761-
private Object[] getIdentityArray() {
762-
return new Object[] {varint, fixed32, fixed64, lengthDelimited, group};
762+
int result = 1;
763+
result = 31 * result + Objects.hashCode(varint);
764+
result = 31 * result + Objects.hashCode(fixed32);
765+
result = 31 * result + Objects.hashCode(fixed64);
766+
result = 31 * result + Objects.hashCode(lengthDelimited);
767+
result = 31 * result + Objects.hashCode(group);
768+
return result;
763769
}
764770

765771
/**
@@ -783,15 +789,15 @@ public ByteString toByteString(int fieldNumber) {
783789
@SuppressWarnings({"ForeachList", "ForeachListWithUserVar"}) // No iterator allocation.
784790
public void writeTo(int fieldNumber, CodedOutputStream output) throws IOException {
785791
for (int i = 0; i < varint.size(); i++) {
786-
long value = varint.get(i);
792+
long value = varint.getLong(i);
787793
output.writeUInt64(fieldNumber, value);
788794
}
789795
for (int i = 0; i < fixed32.size(); i++) {
790-
int value = fixed32.get(i);
796+
int value = fixed32.getInt(i);
791797
output.writeFixed32(fieldNumber, value);
792798
}
793799
for (int i = 0; i < fixed64.size(); i++) {
794-
long value = fixed64.get(i);
800+
long value = fixed64.getLong(i);
795801
output.writeFixed64(fieldNumber, value);
796802
}
797803
for (int i = 0; i < lengthDelimited.size(); i++) {
@@ -809,15 +815,15 @@ public void writeTo(int fieldNumber, CodedOutputStream output) throws IOExceptio
809815
public int getSerializedSize(int fieldNumber) {
810816
int result = 0;
811817
for (int i = 0; i < varint.size(); i++) {
812-
long value = varint.get(i);
818+
long value = varint.getLong(i);
813819
result += CodedOutputStream.computeUInt64Size(fieldNumber, value);
814820
}
815821
for (int i = 0; i < fixed32.size(); i++) {
816-
int value = fixed32.get(i);
822+
int value = fixed32.getInt(i);
817823
result += CodedOutputStream.computeFixed32Size(fieldNumber, value);
818824
}
819825
for (int i = 0; i < fixed64.size(); i++) {
820-
long value = fixed64.get(i);
826+
long value = fixed64.getLong(i);
821827
result += CodedOutputStream.computeFixed64Size(fieldNumber, value);
822828
}
823829
for (int i = 0; i < lengthDelimited.size(); i++) {
@@ -901,9 +907,9 @@ public int getSerializedSizeAsMessageSetExtension(int fieldNumber) {
901907
return result;
902908
}
903909

904-
private List<Long> varint;
905-
private List<Integer> fixed32;
906-
private List<Long> fixed64;
910+
private LongArrayList varint;
911+
private IntArrayList fixed32;
912+
private LongArrayList fixed64;
907913
private List<ByteString> lengthDelimited;
908914
private List<UnknownFieldSet> group;
909915

@@ -931,17 +937,17 @@ public Builder clone() {
931937
if (result.varint == null) {
932938
copy.varint = null;
933939
} else {
934-
copy.varint = new ArrayList<>(result.varint);
940+
copy.varint = new LongArrayList(result.varint, /* isMutable= */ true);
935941
}
936942
if (result.fixed32 == null) {
937943
copy.fixed32 = null;
938944
} else {
939-
copy.fixed32 = new ArrayList<>(result.fixed32);
945+
copy.fixed32 = new IntArrayList(result.fixed32, /* isMutable= */ true);
940946
}
941947
if (result.fixed64 == null) {
942948
copy.fixed64 = null;
943949
} else {
944-
copy.fixed64 = new ArrayList<>(result.fixed64);
950+
copy.fixed64 = new LongArrayList(result.fixed64, /* isMutable= */ true);
945951
}
946952
if (result.lengthDelimited == null) {
947953
copy.lengthDelimited = null;
@@ -965,19 +971,19 @@ public Builder clone() {
965971
public Field build() {
966972
Field built = new Field();
967973
if (result.varint == null) {
968-
built.varint = Collections.emptyList();
974+
built.varint = LongArrayList.emptyList();
969975
} else {
970-
built.varint = Collections.unmodifiableList(new ArrayList<>(result.varint));
976+
built.varint = new LongArrayList(result.varint, /* isMutable= */ false);
971977
}
972978
if (result.fixed32 == null) {
973-
built.fixed32 = Collections.emptyList();
979+
built.fixed32 = IntArrayList.emptyList();
974980
} else {
975-
built.fixed32 = Collections.unmodifiableList(new ArrayList<>(result.fixed32));
981+
built.fixed32 = new IntArrayList(result.fixed32, /* isMutable= */ false);
976982
}
977983
if (result.fixed64 == null) {
978-
built.fixed64 = Collections.emptyList();
984+
built.fixed64 = LongArrayList.emptyList();
979985
} else {
980-
built.fixed64 = Collections.unmodifiableList(new ArrayList<>(result.fixed64));
986+
built.fixed64 = new LongArrayList(result.fixed64, /* isMutable= */ false);
981987
}
982988
if (result.lengthDelimited == null) {
983989
built.lengthDelimited = Collections.emptyList();
@@ -1007,19 +1013,19 @@ public Builder clear() {
10071013
public Builder mergeFrom(Field other) {
10081014
if (!other.varint.isEmpty()) {
10091015
if (result.varint == null) {
1010-
result.varint = new ArrayList<Long>();
1016+
result.varint = new LongArrayList();
10111017
}
10121018
result.varint.addAll(other.varint);
10131019
}
10141020
if (!other.fixed32.isEmpty()) {
10151021
if (result.fixed32 == null) {
1016-
result.fixed32 = new ArrayList<Integer>();
1022+
result.fixed32 = new IntArrayList();
10171023
}
10181024
result.fixed32.addAll(other.fixed32);
10191025
}
10201026
if (!other.fixed64.isEmpty()) {
10211027
if (result.fixed64 == null) {
1022-
result.fixed64 = new ArrayList<>();
1028+
result.fixed64 = new LongArrayList();
10231029
}
10241030
result.fixed64.addAll(other.fixed64);
10251031
}
@@ -1041,27 +1047,27 @@ public Builder mergeFrom(Field other) {
10411047
/** Add a varint value. */
10421048
public Builder addVarint(long value) {
10431049
if (result.varint == null) {
1044-
result.varint = new ArrayList<>();
1050+
result.varint = new LongArrayList();
10451051
}
1046-
result.varint.add(value);
1052+
result.varint.addLong(value);
10471053
return this;
10481054
}
10491055

10501056
/** Add a fixed32 value. */
10511057
public Builder addFixed32(int value) {
10521058
if (result.fixed32 == null) {
1053-
result.fixed32 = new ArrayList<>();
1059+
result.fixed32 = new IntArrayList();
10541060
}
1055-
result.fixed32.add(value);
1061+
result.fixed32.addInt(value);
10561062
return this;
10571063
}
10581064

10591065
/** Add a fixed64 value. */
10601066
public Builder addFixed64(long value) {
10611067
if (result.fixed64 == null) {
1062-
result.fixed64 = new ArrayList<>();
1068+
result.fixed64 = new LongArrayList();
10631069
}
1064-
result.fixed64.add(value);
1070+
result.fixed64.addLong(value);
10651071
return this;
10661072
}
10671073

0 commit comments

Comments
 (0)