title: proto3 - oneof vs fields with identifier
body: <p>I am writing a proto3 class for an object which currently have around 2 variations, and will grow up to 6 or 7. Only one of them would be used in a message. These variations do not share common fields. They will be encoded as a submessage in a parent message. These messages would be written once and read tens of thousands of time.</p>
  
  <p>I was wondering what would be the most performant way, memory and time of parsing wise, to achieve this so that as more variations are added, the performance is not lost.</p>
  
  <p>Consider the following variations.</p>
  
  <pre><code>message B1 {
      repeated string value = 1;
      bool hasMeta = 2;
  }
  
  message B2 {
      repeated int32 value = 1;
      map&lt;string, string&gt; foo = 2;
  }
  </code></pre>
  
  <p>First option: define an <code>oneof</code> field that refers the specific subtype.</p>
  
  <pre><code>message P1 {
      oneof parents {
          B1 boo = 1;
          B2 baz = 2;
          // add more variations here in future..
      }
      // other non-related fields...
  }
  </code></pre>
  
  <p>Second option: define an integer that acts as an identifier for the available variation. At runtime this integer can be used to determine which variation has been set (another way is to null check the variations and use the first non-null).</p>
  
  <pre><code>message P1 {
      int32 type = 1;
      B1 boo = 2;
      B2 baz = 3;
      // other non-related fields...
  }
  </code></pre>
  
  <p>I am particularly interested in the wire size and performance.</p>
  
  <p>In second option, considering only one of the variations would be set (enforced in app layer), will the wire size be more than that in first? Is memory reserved for null fields as well?</p>