Avoid boxing when serializing primitive lists in CodedOutputStreamWriter
commiteaee04d89719ed8e0fde96b6d37cf741d866f511
authorProtobuf Team Bot <protobuf-github-bot@google.com>
Mon, 22 Apr 2024 23:02:13 +0000 (22 16:02 -0700)
committerCopybara-Service <copybara-worker@google.com>
Mon, 22 Apr 2024 23:12:47 +0000 (22 16:12 -0700)
tree4b779ac3ede880465bf2cb0444b102886d451d9c
parentd1cf09abcff7636101fd27e94ea5aa7b9f603f5e
Avoid boxing when serializing primitive lists in CodedOutputStreamWriter

Previously, these List.get methods returned Object, forcing boxing, and (if not in the primitive's box cache) allocation, with all the cost that entails.

I've taken the approach of duplicating methods to specialise for primitives, like BinaryWriter does for non-lite protos.

I initially considered checking the class of the list on every iteration of the
loop, but that feels wasteful, when we can
check it once at the start of the loop. This also means we have the same
behaviour as serverside protos. At the cost of a few more methods, but
hopefully they're trivially inlineable, so hopefully leading to a small dex increase without really increasing the method count.

Given this is a public API, I don't think we can remove the List<Long> overloads either.

PiperOrigin-RevId: 627183583
java/core/src/main/java/com/google/protobuf/CodedOutputStreamWriter.java