title: What is the most efficient way to represent small values in a struct?
body: <p>Often I find myself having to represent a structure that consists of very small values. For example, <code>Foo</code> has 4 values, <code>a, b, c, d</code> that, range from <code>0 to 3</code>. Usually I don't care, but sometimes, those structures are </p>
  
  <ol>
  <li><p>used in a tight loop;</p></li>
  <li><p>their values are read a billion times/s, and that is <strong>the</strong> bottleneck of the program;</p></li>
  <li><p>the whole program consists of a big array of billions of <code>Foo</code>s;</p></li>
  </ol>
  
  <p>In that case, I find myself having trouble deciding how to represent <code>Foo</code> efficiently. I have basically 4 options:</p>
  
  <pre><code>struct Foo {
      int a;
      int b;
      int c;
      int d;
  };
  
  struct Foo {
      char a;
      char b;
      char c;
      char d;
  };
  
  struct Foo {
      char abcd;
  };
  
  struct FourFoos {
      int abcd_abcd_abcd_abcd;
  };
  </code></pre>
  
  <p>They use 128, 32, 8, 8 bits respectively per <code>Foo</code>, ranging from sparse to densely packed. The first example is probably the most linguistic one, but using it would essentially increase by 16 times the size of the program, which doesn't sound quite right. Moreover, most of the memory will be filled with zeroes and not be used at all, which makes me wonder if this isn't a waste. On the other hands, packing them densely brings an additional overhead for of reading them.</p>
  
  <p>What is the computationally 'fastest' method for representing small values in a struct?</p>