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
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 683f310..d3e7cc0 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java
@@ -2365,10 +2365,12 @@ * 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 @@ * 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 @@ 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; } }