From f8f5136c427ac1cbd5184bc57200e36c7e4bf82e Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Thu, 26 Sep 2024 16:32:25 -0700 Subject: [PATCH] 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 --- .../google/protobuf/CodedOutputStream.java | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 57be310f0027..22a4aa4a1651 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -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); + } } /** @@ -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. } @@ -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); } } @@ -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; @@ -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); } } } @@ -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; @@ -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); } } @@ -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); } } @@ -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); } @@ -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); } } @@ -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; } @@ -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); } } @@ -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; } @@ -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);