Hoist AbstractBufferedEncoder.position to a field when reading/writing it repeatedly.

This produces much better code for Android: https://godbolt.org/z/xE8T9xqrr

Down from 196 bytes to 140 bytes. Bounds checks get combined together.

This is a partial roll-forward of cl/673588324.

PiperOrigin-RevId: 681703327
pull/18599/head
Mark Hansen 2 months ago committed by Copybara-Service
parent 03aec42fcb
commit 33d4ac4064
  1. 4
      java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

@ -2365,10 +2365,12 @@ public abstract class CodedOutputStream extends ByteOutput {
* responsibility of the caller.
*/
final void bufferFixed32NoTag(int value) {
int position = this.position; // Perf: hoist field to register to avoid load/stores.
buffer[position++] = (byte) (value & 0xFF);
buffer[position++] = (byte) ((value >> 8) & 0xFF);
buffer[position++] = (byte) ((value >> 16) & 0xFF);
buffer[position++] = (byte) ((value >> 24) & 0xFF);
this.position = position;
totalBytesWritten += FIXED32_SIZE;
}
@ -2377,6 +2379,7 @@ public abstract class CodedOutputStream extends ByteOutput {
* responsibility of the caller.
*/
final void bufferFixed64NoTag(long value) {
int position = this.position; // Perf: hoist field to register to avoid load/stores.
buffer[position++] = (byte) (value & 0xFF);
buffer[position++] = (byte) ((value >> 8) & 0xFF);
buffer[position++] = (byte) ((value >> 16) & 0xFF);
@ -2385,6 +2388,7 @@ public abstract class CodedOutputStream extends ByteOutput {
buffer[position++] = (byte) ((int) (value >> 40) & 0xFF);
buffer[position++] = (byte) ((int) (value >> 48) & 0xFF);
buffer[position++] = (byte) ((int) (value >> 56) & 0xFF);
this.position = position;
totalBytesWritten += FIXED64_SIZE;
}
}

Loading…
Cancel
Save