title: C# - fixed/inline arrays
body: <p>I'm writing a B+-tree implementation in C#, and the tree implementation I chose for my application has a very specific structure which is cache-conscious. To achieve these properties, it has strict layout policies on tree nodes.</p>
  
  <p>What I want is simply expressed using C#'s <a href="http://msdn.microsoft.com/en-us/library/zycewsya%28v=vs.80%29.aspx" rel="nofollow">fixed</a> keyword for fixed-sized buffers:</p>
  
  <pre><code>public abstract class Tree&lt;K, T&gt; { }
  sealed class Node&lt;K, T&gt; : Tree&lt;K, T&gt;
  {
      Node&lt;K, T&gt; right;
      fixed Tree&lt;K, T&gt; nodes[127]; // inline array of 128 nodes
  }
  </code></pre>
  
  <p>Unfortunately, fixed-sized buffers can only be used with primitive value types, like int and float. Just using plain arrays would add pointer indirections which destroy the cache-friendly properties of this tree type.</p>
  
  <p>I also can't generate 128 fields and use pointer arithmetic to extract the field I want because <a href="http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx" rel="nofollow">there are no conversions between pointer types and managed objects</a>.</p>
  
  <p>About the only thing left is generating 128 fields with an indexer that selects the right one based on a switch (which can't be fast), or writing it as a C library and using P/Invoke, which would also destroy the performance.</p>
  
  <p>Any suggestions?</p>