Remove unnecessary intermediate casts in CodedOutputStream

https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.1.3

The JLS guarantees this is the same:

> A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

PiperOrigin-RevId: 683416604
pull/18537/head
Mark Hansen 2 months ago committed by Copybara-Service
parent fe349b7398
commit 6ea07ef84c
  1. 24
      java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

@ -1414,14 +1414,14 @@ public abstract class CodedOutputStream extends ByteOutput {
public final void writeFixed64NoTag(long value) throws IOException { public final void writeFixed64NoTag(long value) throws IOException {
int position = this.position; // Perf: hoist field to register to avoid load/stores. int position = this.position; // Perf: hoist field to register to avoid load/stores.
try { try {
buffer[position] = (byte) ((int) value); buffer[position] = (byte) value;
buffer[position + 1] = (byte) ((int) (value >> 8)); buffer[position + 1] = (byte) (value >> 8);
buffer[position + 2] = (byte) ((int) (value >> 16)); buffer[position + 2] = (byte) (value >> 16);
buffer[position + 3] = (byte) ((int) (value >> 24)); buffer[position + 3] = (byte) (value >> 24);
buffer[position + 4] = (byte) ((int) (value >> 32)); buffer[position + 4] = (byte) (value >> 32);
buffer[position + 5] = (byte) ((int) (value >> 40)); buffer[position + 5] = (byte) (value >> 40);
buffer[position + 6] = (byte) ((int) (value >> 48)); buffer[position + 6] = (byte) (value >> 48);
buffer[position + 7] = (byte) ((int) (value >> 56)); buffer[position + 7] = (byte) (value >> 56);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e); throw new OutOfSpaceException(position, limit, FIXED64_SIZE, e);
} }
@ -2384,10 +2384,10 @@ public abstract class CodedOutputStream extends ByteOutput {
buffer[position++] = (byte) (value >> 8); buffer[position++] = (byte) (value >> 8);
buffer[position++] = (byte) (value >> 16); buffer[position++] = (byte) (value >> 16);
buffer[position++] = (byte) (value >> 24); buffer[position++] = (byte) (value >> 24);
buffer[position++] = (byte) ((int) (value >> 32)); buffer[position++] = (byte) (value >> 32);
buffer[position++] = (byte) ((int) (value >> 40)); buffer[position++] = (byte) (value >> 40);
buffer[position++] = (byte) ((int) (value >> 48)); buffer[position++] = (byte) (value >> 48);
buffer[position++] = (byte) ((int) (value >> 56)); buffer[position++] = (byte) (value >> 56);
this.position = position; this.position = position;
totalBytesWritten += FIXED64_SIZE; totalBytesWritten += FIXED64_SIZE;
} }

Loading…
Cancel
Save