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.
What I want is simply expressed using C#'s fixed keyword for fixed-sized buffers:
public abstract class Tree<K, T> { }
sealed class Node<K, T> : Tree<K, T>
{
Node<K, T> right;
fixed Tree<K, T> nodes[127]; // inline array of 128 nodes
}
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.
I also can't generate 128 fields and use pointer arithmetic to extract the field I want because there are no conversions between pointer types and managed objects.
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.
Any suggestions?
right. You probably didn't want to reduce the size of the array, unless you want computations modulo 127 instead of bit-shifting.