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
pull/18470/head
Mark Hansen 2 months ago committed by Copybara-Service
parent ba11052918
commit f8f5136c42
  1. 55
      java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

@ -948,6 +948,22 @@ public abstract class CodedOutputStream extends ByteOutput {
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);
}
}
/**
@ -1310,8 +1326,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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.
}
@ -1339,8 +1354,7 @@ public abstract class CodedOutputStream extends ByteOutput {
}
}
} 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);
}
}
@ -1353,8 +1367,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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;
@ -1384,8 +1397,7 @@ public abstract class CodedOutputStream extends ByteOutput {
}
}
} 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);
}
}
}
@ -1403,8 +1415,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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;
@ -1416,8 +1427,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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);
}
}
@ -1433,8 +1443,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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);
}
}
@ -1981,8 +1990,7 @@ public abstract class CodedOutputStream extends ByteOutput {
@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);
}
@ -2043,8 +2051,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value >>>= 7;
}
}
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
throw new OutOfSpaceException(position, limit, 1);
}
}
@ -2053,8 +2060,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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;
}
@ -2082,8 +2088,7 @@ public abstract class CodedOutputStream extends ByteOutput {
value >>>= 7;
}
}
throw new OutOfSpaceException(
String.format("Pos: %d, limit: %d, len: %d", position, limit, 1));
throw new OutOfSpaceException(position, limit, 1);
}
}
@ -2092,8 +2097,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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;
}
@ -2108,8 +2112,7 @@ public abstract class CodedOutputStream extends ByteOutput {
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);

Loading…
Cancel
Save