title: Optimal Serialization of Primitive Types
body: <p>We are beginning to roll out more and more WAN deployments of our product (.NET fat client with an IIS hosted Remoting backend). Because of this we are trying to reduce the size of the data on the wire.</p>
  
  <p>We have overridden the default serialization by implementing ISerializable (similar to <a href="http://www.codeproject.com/KB/cs/FastSerialization.aspx" rel="nofollow noreferrer">this</a>), and we are seeing anywhere from 12% to 50% gains. Most of our efforts focus on optimizing arrays of primitive types. <strong>Is there a fancy way of serializing primitive types, beyond the obvious?</strong></p>
  
  <p>For example, today we serialize an array of ints as follows:</p>
  
  <blockquote>
    <p>[4-bytes (array length)][4-bytes][4-bytes]</p>
  </blockquote>
  
  <p>Can anyone do significantly better?</p>
  
  <p>The most obvious example of a significant improvement, for boolean arrays, is putting 8 bools in each byte, which we already do.</p>
  
  <p>Note: <em>Saving 7 bits per bool may seem like a waste of time, but when you are dealing with large magnitudes of data (which we are), it adds up very fast.</em></p>
  
  <p>Note: We want to avoid general compression algorithms because of the latency associated with it. Remoting only supports buffered requests/responses (no chunked encoding). I realize there is a fine line between compression and optimal serialization, but our tests indicate we can afford very specific serialization optimizations at very little cost in latency. Whereas reprocessing the entire buffered response into new compressed buffer is too expensive.</p>