Skip to content

Commit

Permalink
Speed up CodedOutputStream by extracting rarely-executed string forma…
Browse files Browse the repository at this point in the history
…tting 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
  • Loading branch information
mhansen authored and copybara-github committed Sep 26, 2024
1 parent ba11052 commit f8f5136
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,22 @@ public static class OutOfSpaceException extends IOException {
OutOfSpaceException(String explanationMessage, Throwable cause) {
super(MESSAGE + ": " + explanationMessage, cause);
}

OutOfSpaceException(int position, int limit, int length) {
this(position, limit, length, null);
}

OutOfSpaceException(int position, int limit, int length, Throwable cause) {
this((long) position, (long) limit, length, cause);
}

OutOfSpaceException(long position, long limit, int length) {
this(position, limit, length, null);
}

OutOfSpaceException(long position, long limit, int length, Throwable cause) {
this(String.format("Pos: %d, limit: %d, len: %d", position, limit, length), cause);
}
}

/**
Expand Down Expand Up @@ -1310,8 +1326,7 @@ public final void write(byte value) throws IOException {
try {
buffer[position++] = value;
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
throw new OutOfSpaceException(position, limit, 1, e);
}
this.position = position; // Only update position if we stayed within the array bounds.
}
Expand Down Expand Up @@ -1339,8 +1354,7 @@ public final void writeUInt32NoTag(int value) throws IOException {
}
}
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
throw new OutOfSpaceException(position, limit, 1, e);
}
}

Expand All @@ -1353,8 +1367,7 @@ public final void writeFixed32NoTag(int value) throws IOException {
buffer[position + 2] = (byte) ((value >> 16) & 0xFF);
buffer[position + 3] = (byte) ((value >> 24) & 0xFF);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e);
throw new OutOfSpaceException(position, limit, FIXED32_SIZE, e);
}
// Only update position if we stayed within the array bounds.
this.position = position + FIXED32_SIZE;
Expand Down Expand Up @@ -1384,8 +1397,7 @@ public final void writeUInt64NoTag(long value) throws IOException {
}
}
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1), e);
throw new OutOfSpaceException(position, limit, 1, e);
}
}
}
Expand All @@ -1403,8 +1415,7 @@ public final void writeFixed64NoTag(long value) throws IOException {
buffer[position + 6] = (byte) ((int) (value >> 48) & 0xFF);
buffer[position + 7] = (byte) ((int) (value >> 56) & 0xFF);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e);
throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e);
}
// Only update position if we stayed within the array bounds.
this.position = position + FIXED64_SIZE;
Expand All @@ -1416,8 +1427,7 @@ public final void write(byte[] value, int offset, int length) throws IOException
System.arraycopy(value, offset, buffer, position, length);
position += length;
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, length), e);
throw new OutOfSpaceException(position, limit, length, e);
}
}

Expand All @@ -1433,8 +1443,7 @@ public final void write(ByteBuffer value) throws IOException {
value.get(buffer, position, length);
position += length;
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, length), e);
throw new OutOfSpaceException(position, limit, length, e);
}
}

Expand Down Expand Up @@ -1981,8 +1990,7 @@ void writeMessageNoTag(MessageLite value, Schema schema) throws IOException {
@Override
public void write(byte value) throws IOException {
if (position >= limit) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
throw new OutOfSpaceException(position, limit, 1);
}
UnsafeUtil.putByte(position++, value);
}
Expand Down Expand Up @@ -2043,8 +2051,7 @@ public void writeUInt32NoTag(int value) throws IOException {
value >>>= 7;
}
}
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
throw new OutOfSpaceException(position, limit, 1);
}
}

Expand All @@ -2053,8 +2060,7 @@ public void writeFixed32NoTag(int value) throws IOException {
try {
buffer.putInt(bufferPos(position), value);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED32_SIZE), e);
throw new OutOfSpaceException(position, limit, FIXED32_SIZE, e);
}
position += FIXED32_SIZE;
}
Expand Down Expand Up @@ -2082,8 +2088,7 @@ public void writeUInt64NoTag(long value) throws IOException {
value >>>= 7;
}
}
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
throw new OutOfSpaceException(position, limit, 1);
}
}

Expand All @@ -2092,8 +2097,7 @@ public void writeFixed64NoTag(long value) throws IOException {
try {
buffer.putLong(bufferPos(position), value);
} catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, FIXED64_SIZE), e);
throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e);
}
position += FIXED64_SIZE;
}
Expand All @@ -2108,8 +2112,7 @@ public void write(byte[] value, int offset, int length) throws IOException {
if (value == null) {
throw new NullPointerException("value");
}
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, length));
throw new OutOfSpaceException(position, limit, length);
}

UnsafeUtil.copyMemory(value, offset, position, length);
Expand Down

0 comments on commit f8f5136

Please sign in to comment.