开发者

C# - fixed/inline arrays

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 swit开发者_C百科ch (which can't be fast), or writing it as a C library and using P/Invoke, which would also destroy the performance.

Any suggestions?


Use C++/CLI. That gives you total control over layout, just like C, but the cost of managed/unmanaged transitions is much reduced from p/invoke (probably no extra cost at all).

Unfortunately managed code is not very good for "cache-conscious" work: inside the managed heap you are powerless to avoid false-sharing. C++/CLI lets you use the unmanaged allocator, so you can not only keep the data contiguous, but aligned to cache lines.


Also note: Using the class keyword creates a "reference type" which already adds that level of indirection you wanted to avoid. With some reorganization, you might be able to use a struct and an array and not have any more indirection that the code you proposed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜