SIGN IN SIGN UP

Protocol Buffers - Google's data interchange format

0 0 3 C++

Optimize GeneratedMessage.ExtendableBuilder to avoid temporary allocations when merging empty extendable messages.

When toBuilder() is called on an immutable ExtendableMessage whose extensions field is the singleton FieldSet.emptySet(), generated code calls ExtendableBuilder.mergeExtensionFields(other).

Because other.extensions is FieldSet.emptySet() (which is not null), mergeExtensionFields previously invoked ensureExtensionsIsMutable(), which unnecessarily allocated a brand new temporary FieldSet.Builder and SmallSortedMap. Later, when build() executed, FieldSet.Builder.buildImpl recognized that the builder's map was empty and correctly returned the singleton FieldSet.emptySet().

By checking !other.extensions.isEmpty() before mutating the builder, we completely eliminate these wasted temporary allocations.

PiperOrigin-RevId: 916174058
P
Protobuf Team Bot committed
a37dbd643372b05de834190941671a1563d7e499
Parent: d3e5c57
Committed by Copybara-Service <copybara-worker@google.com> on 5/15/2026, 9:08:21 PM