title: protobuf-net message serialized size property
body: <p>We are using protobuf-net for serialization and deserialization of messages in an application whose public protocol is based on Google Protocol Buffers. The library is excellent and covers our all requirements except for this one: we need to find out the serialized message length in bytes before the message is actually serialized.</p>
  
  <p><a href="https://stackoverflow.com/questions/2313724/protocol-buffers-c-protobuf-net-messagebytesize">The question</a> has already been asked a year and a half ago and according to Marc, the only way to do this was to serialize to a MemoryStream and read the <code>.Length</code> property afterwards. This is not acceptable in our case, because MemoryStream allocates a byte buffer behind the scenes and we have to avoid this.</p>
  
  <p>This line from the same response gives us hope that it might be possible after all:</p>
  
  <blockquote>
    <p>If you clarify what the use-case is, I'm sure we can make it easily
    available (if it isn't already).</p>
  </blockquote>
  
  <p>Here is our use case. We have messages whose size varies between several bytes and two megabytes. The application pre-allocates byte buffers used for socket operations and for serializing / deserializing and once the warm-up phase is over, no additional buffers can be created (hint: avoding GC and heap fragmentation). Byte buffers are essentially pooled. We also want to avoid copying bytes between buffers / streams as much as possible.</p>
  
  <p>We have come up with two possible strategies and both of them require message size upfront:</p>
  
  <ol>
  <li>Use (large) fixed-size byte buffers and serialize all messages that can fit into one buffer; send the content of the buffer using <code>Socket.Send</code>. We have to know when the next message cannot fit into the buffer and stop serializing. Without message size, the only way to achieve this is to wait for an exception to occur during <code>Serialize</code>.</li>
  <li>Use (small) variable size byte buffers and serialize each message into one buffer; send the content of the buffer using <code>Socket.Send</code>. In order to check out the byte buffer with appropriate size from the pool, we need to know how much bytes does a serialized message have.</li>
  </ol>
  
  <p>Because the protocol is already defined (we cannot change this) and requires message length prefix to be Varint32, we cannot use <code>SerializeWithLengthPrefix</code> method.</p>
  
  <p>So is it possible to add a method that estimates a message size without serialization into a stream? If it is something that does not fit into the current feature set and roadmap of the library, but is doable, we are interested into extending the library ourselves. We are also looking for alternative approaches, if there are any.</p>