The proto message size is currently computed twice:
- Once before marshalling.
- Again during the marshalling call itself.
In high-throughput workloads, this duplicated computation is expensive and unnecessary.
By enabling UseCachedSize on MarshalOptions, we can reuse the size calculated immediately before marshalling, avoiding the redundant second call to proto.Size.
Impact
In our application, the extra proto.Size call accounted for ~12% of total CPU time. Removing it results in a significant performance improvement while preserving correctness.
Proposed Change
The proto message size is currently computed twice:
In high-throughput workloads, this duplicated computation is expensive and unnecessary.
By enabling
UseCachedSizeonMarshalOptions, we can reuse the size calculated immediately before marshalling, avoiding the redundant second call toproto.Size.Impact
In our application, the extra
proto.Sizecall accounted for ~12% of total CPU time. Removing it results in a significant performance improvement while preserving correctness.Proposed Change
UseCachedSizeinMarshalOptionsto eliminate duplicate size calculations. See encoding/proto: enable use cached size option #8569