Skip to content

Commit f8f5136

Browse files
mhansencopybara-github
authored andcommitted
Speed up CodedOutputStream by extracting rarely-executed string formatting code
If this code is in every method, it's making every method larger, putting pressure on the instruction cache. The string formatting is an exceptional case that shouldn't slow down the regular case. This should modestly speed up the code in here. And besides, it's just a little nicer to have this formatting centralised. PiperOrigin-RevId: 679336304
1 parent ba11052 commit f8f5136

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,22 @@ public static class OutOfSpaceException extends IOException {
948948
OutOfSpaceException(String explanationMessage, Throwable cause) {
949949
super(MESSAGE + ": " + explanationMessage, cause);
950950
}
951+
952+
OutOfSpaceException(int position, int limit, int length) {
953+
this(position, limit, length, null);
954+
}
955+
956+
OutOfSpaceException(int position, int limit, int length, Throwable cause) {
957+
this((long) position, (long) limit, length, cause);
958+
}
959+
960+
OutOfSpaceException(long position, long limit, int length) {
961+
this(position, limit, length, null);
962+
}
963+
964+
OutOfSpaceException(long position, long limit, int length, Throwable cause) {
965+
this(String.format("Pos: %d, limit: %d, len: %d", position, limit, length), cause);
966+
}
951967
}
952968

953969
/**
@@ -1310,8 +1326,7 @@ public final void write(byte value) throws IOException {
13101326
try {
13111327
buffer[position++] = value;
13121328
} catch (IndexOutOfBoundsException e) {
1313-
throw new OutOfSpaceException(
1314-
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
1329+
throw new OutOfSpaceException(position, limit, 1, e);
13151330
}
13161331
this.position = position; // Only update position if we stayed within the array bounds.
13171332
}
@@ -1339,8 +1354,7 @@ public final void writeUInt32NoTag(int value) throws IOException {
13391354
}
13401355
}
13411356
} catch (IndexOutOfBoundsException e) {
1342-
throw new OutOfSpaceException(
1343-
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
1357+
throw new OutOfSpaceException(position, limit, 1, e);
13441358
}
13451359
}
13461360

@@ -1353,8 +1367,7 @@ public final void writeFixed32NoTag(int value) throws IOException {
13531367
buffer[position + 2] = (byte) ((value >> 16) & 0xFF);
13541368
buffer[position + 3] = (byte) ((value >> 24) & 0xFF);
13551369
} catch (IndexOutOfBoundsException e) {
1356-
throw new OutOfSpaceException(
1357-
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e);
1370+
throw new OutOfSpaceException(position, limit, FIXED32_SIZE, e);
13581371
}
13591372
// Only update position if we stayed within the array bounds.
13601373
this.position = position + FIXED32_SIZE;
@@ -1384,8 +1397,7 @@ public final void writeUInt64NoTag(long value) throws IOException {
13841397
}
13851398
}
13861399
} catch (IndexOutOfBoundsException e) {
1387-
throw new OutOfSpaceException(
1388-
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
1400+
throw new OutOfSpaceException(position, limit, 1, e);
13891401
}
13901402
}
13911403
}
@@ -1403,8 +1415,7 @@ public final void writeFixed64NoTag(long value) throws IOException {
14031415
buffer[position + 6] = (byte) ((int) (value >> 48) & 0xFF);
14041416
buffer[position + 7] = (byte) ((int) (value >> 56) & 0xFF);
14051417
} catch (IndexOutOfBoundsException e) {
1406-
throw new OutOfSpaceException(
1407-
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e);
1418+
throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e);
14081419
}
14091420
// Only update position if we stayed within the array bounds.
14101421
this.position = position + FIXED64_SIZE;
@@ -1416,8 +1427,7 @@ public final void write(byte[] value, int offset, int length) throws IOException
14161427
System.arraycopy(value, offset, buffer, position, length);
14171428
position += length;
14181429
} catch (IndexOutOfBoundsException e) {
1419-
throw new OutOfSpaceException(
1420-
String.format("Pos: %d, limit: %d, len: %d", position, limit, length), e);
1430+
throw new OutOfSpaceException(position, limit, length, e);
14211431
}
14221432
}
14231433

@@ -1433,8 +1443,7 @@ public final void write(ByteBuffer value) throws IOException {
14331443
value.get(buffer, position, length);
14341444
position += length;
14351445
} catch (IndexOutOfBoundsException e) {
1436-
throw new OutOfSpaceException(
1437-
String.format("Pos: %d, limit: %d, len: %d", position, limit, length), e);
1446+
throw new OutOfSpaceException(position, limit, length, e);
14381447
}
14391448
}
14401449

@@ -1981,8 +1990,7 @@ void writeMessageNoTag(MessageLite value, Schema schema) throws IOException {
19811990
@Override
19821991
public void write(byte value) throws IOException {
19831992
if (position >= limit) {
1984-
throw new OutOfSpaceException(
1985-
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
1993+
throw new OutOfSpaceException(position, limit, 1);
19861994
}
19871995
UnsafeUtil.putByte(position++, value);
19881996
}
@@ -2043,8 +2051,7 @@ public void writeUInt32NoTag(int value) throws IOException {
20432051
value >>>= 7;
20442052
}
20452053
}
2046-
throw new OutOfSpaceException(
2047-
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
2054+
throw new OutOfSpaceException(position, limit, 1);
20482055
}
20492056
}
20502057

@@ -2053,8 +2060,7 @@ public void writeFixed32NoTag(int value) throws IOException {
20532060
try {
20542061
buffer.putInt(bufferPos(position), value);
20552062
} catch (IndexOutOfBoundsException e) {
2056-
throw new OutOfSpaceException(
2057-
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e);
2063+
throw new OutOfSpaceException(position, limit, FIXED32_SIZE, e);
20582064
}
20592065
position += FIXED32_SIZE;
20602066
}
@@ -2082,8 +2088,7 @@ public void writeUInt64NoTag(long value) throws IOException {
20822088
value >>>= 7;
20832089
}
20842090
}
2085-
throw new OutOfSpaceException(
2086-
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
2091+
throw new OutOfSpaceException(position, limit, 1);
20872092
}
20882093
}
20892094

@@ -2092,8 +2097,7 @@ public void writeFixed64NoTag(long value) throws IOException {
20922097
try {
20932098
buffer.putLong(bufferPos(position), value);
20942099
} catch (IndexOutOfBoundsException e) {
2095-
throw new OutOfSpaceException(
2096-
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e);
2100+
throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e);
20972101
}
20982102
position += FIXED64_SIZE;
20992103
}
@@ -2108,8 +2112,7 @@ public void write(byte[] value, int offset, int length) throws IOException {
21082112
if (value == null) {
21092113
throw new NullPointerException("value");
21102114
}
2111-
throw new OutOfSpaceException(
2112-
String.format("Pos: %d, limit: %d, len: %d", position, limit, length));
2115+
throw new OutOfSpaceException(position, limit, length);
21132116
}
21142117

21152118
UnsafeUtil.copyMemory(value, offset, position, length);

0 commit comments

Comments
 (0)