The current pattern of counting the serialization size and the actual serialization in the codebase is error-prone and hard to maintain. Those 2 code paths have almost the same code repeated.
The pattern can be found in org.apache.cassandra.net.Message#Serializer and numerous locations that use org.apache.cassandra.transport.CBCodec.
I am proposing a new approach that lets both methods share the same code path. The code basically looks like the below (in the case of org.apache.cassandra.net.Message#Serializer).
Therefore, in the size calculation, we can supply the fake data output and call serialize to get the size.