From 25e50caa3ec1b370964fbc8330b686a5c070f40e Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Wed, 2 Oct 2024 20:25:37 -0700 Subject: [PATCH] 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 --- .../src/main/java/com/google/protobuf/CodedOutputStream.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 fccdb2178e..683f3106fc 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/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++; }