Micro-optimise CodedOutputStream.AbstractBufferedEncoder.buffer(byte)

This saves one ARM instruction (`mov x1, x4`) when the array is out of bounds:
https://godbolt.org/z/7Gb7so4Ez

Because the side effects of position++ have to happen even if the array overflows.

It's fairly minor. Probably won't make a big difference.

This is a partial roll forward of cl/673588324.

PiperOrigin-RevId: 681696805
pull/18600/head
Mark Hansen 2 months ago committed by Copybara-Service
parent dd78f0ac12
commit 25e50caa3e
  1. 5
      java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

@ -2266,7 +2266,10 @@ public abstract class CodedOutputStream extends ByteOutput {
* responsibility of the caller.
*/
final void buffer(byte value) {
buffer[position++] = value;
int position = this.position;
buffer[position] = value;
// Android optimisation: 1 fewer instruction codegen vs buffer[position++].
this.position = position + 1;
totalBytesWritten++;
}

Loading…
Cancel
Save