<p>I'm currently working on a ray-tracer in C# as a hobby project. I'm trying to achieve a decent rendering speed by implementing some tricks from a c++ implementation and have run into a spot of trouble.</p>

<p>The objects in the scenes which the ray-tracer renders are stored in a KdTree structure and the tree's nodes are, in turn, stored in an array. The optimization I'm having problems with is while trying to fit as many tree nodes as possible into a cache line. One means of doing this is for nodes to contain a pointer to the left child node only. It is then implicit that the right child follows directly after the left one in the array.</p>

<p>The nodes are structs and during tree construction they are succesfully put into the array by a static memory manager class. When I begin to traverse the tree it, at first, seems to work just fine. Then at a point early in the rendering (about the same place each time), the left child pointer of the root node is suddenly pointing at a null pointer. I have come to the conclusion that the garbage collecter has moved the structs as the array lies on the heap.</p>

<p>I've tried several things to pin the addresses in memory but none of them seems to last for the entire application lifetime as I need. The 'fixed' keyword only seems to help during single method calls and declaring 'fixed' arrays can only be done on simple types which a node isn't. Is there a good way to do this or am I just too far down the path of stuff C# wasn't meant for.</p>

<p>Btw, changing to c++, while perhaps the better choice for a high performance program, is not an option.</p>

[{'answer_id': 38304, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 0, 'selected': False, 'text': '<p>Is it really prohibitive to store the pair of array reference and index?</p>\n'}, {'answer_id': 38311, 'author': 'Luke Halliwell', 'author_id': 3974, 'author_profile': 'https://Stackoverflow.com/users/3974', 'pm_score': 3, 'selected': True, 'text': "<p>Firstly, if you're using C# normally, you can't suddenly get a null reference due to the garbage collector moving stuff, because the garbage collector also updates all references, so you don't need to worry about it moving stuff around.</p>\n\n<p>You can pin things in memory but this may cause more problems than it solves.  For one thing, it prevents the garbage collector from compacting memory properly, and may impact performance in that way.</p>\n\n<p>One thing I would say from your post is that using structs may not help performance as you hope.  C# fails to inline any method calls involving structs, and even though they've fixed this in their latest runtime beta, structs frequently don't perform that well.</p>\n\n<p>Personally, I would say C++ tricks like this don't generally tend to carry over too well into C#.  You may have to learn to let go a bit; there can be other more subtle ways to improve performance ;)</p>\n"}, {'answer_id': 38445, 'author': 'Barry Kelly', 'author_id': 3712, 'author_profile': 'https://Stackoverflow.com/users/3712', 'pm_score': 2, 'selected': False, 'text': '<p>What is your static memory manager actually doing? Unless it is doing something unsafe (P/Invoke, unsafe code), the behaviour you are seeing is a bug in your program, and not due to the behaviour of the CLR.</p>\n\n<p>Secondly, what do you mean by \'pointer\', with respect to links between structures? Do you literally mean an unsafe KdTree* pointer? Don\'t do that. Instead, use an index into the array. Since I expect that all nodes for a single tree are stored in the same array, you won\'t need a separate reference to the array. Just a single index will do.</p>\n\n<p>Finally, if you really really must use KdTree* pointers, then your static memory manager should allocate a large block using e.g. Marshal.AllocHGlobal or another unmanaged memory source; it should both treat this large block as a KdTree array (i.e. index a KdTree* C-style) <strong>and</strong> it should suballocate nodes from this array, by bumping a "free" pointer.</p>\n\n<p>If you ever have to resize this array, then you\'ll need to update all the pointers, of course.</p>\n\n<p>The basic lesson here is that unsafe pointers and managed memory do <strong>not</strong> mix outside of \'fixed\' blocks, which of course have stack frame affinity (i.e. when the function returns, the pinned behaviour goes away). There is a way to pin arbitrary objects, like your array, using GCHandle.Alloc(yourArray, GCHandleType.Pinned), but you almost certainly don\'t want to go down that route.</p>\n\n<p>You will get more sensible answers if you describe in more detail what you are doing.</p>\n'}, {'answer_id': 38584, 'author': 'Morten Christiansen', 'author_id': 4055, 'author_profile': 'https://Stackoverflow.com/users/4055', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>What is your static memory manager actually doing? Unless it is doing something unsafe (P/Invoke, unsafe code), the behaviour you are seeing is a bug in your program, and not due to the behaviour of the CLR.</p>\n</blockquote>\n\n<p>I was in fact speaking about unsafe pointers. What I wanted was something like <code>Marshal.AllocHGlobal</code>, though with a lifetime exceeding a single method call. On reflection it seems that just using an index is the right solution as I might have gotten too caught up in mimicking the c++ code.</p>\n\n<blockquote>\n  <p>One thing I would say from your post is that using structs may not help performance as you hope. C# fails to inline any method calls involving structs, and even though they've fixed this in their latest run-time beta, structs frequently don't perform that well.</p>\n</blockquote>\n\n<p>I looked into this a bit and I see it has been fixed in .NET 3.5SP1; I assume that's what you were referring to as the run-time beta. In fact, I now understand that this change accounted for a doubling of my rendering speed. Now, structs are aggressively in-lined, improving their performance greatly on X86 systems (X64 had better struct performance in advance).</p>\n"}, {'answer_id': 38606, 'author': 'Andrew', 'author_id': 1948, 'author_profile': 'https://Stackoverflow.com/users/1948', 'pm_score': 1, 'selected': False, 'text': '<p>If you <em>really</em> want to do this, you can use the GCHandle.Alloc method to specify that a pointer should be pinned without being automatically released at the end of the scope like the fixed statement.</p>\n\n<p>But, as other people have been saying, doing this is putting undue pressure on the garbage collector.  What about just creating a struct that holds onto a pair of your nodes and then managing an array of NodePairs rather than an array of nodes? </p>\n\n<p>If you really do want to have completely unmanaged access to a chunk of memory, you would probably be better off allocating the memory directly from the unmanaged heap rather than permanently pinning a part of the managed heap (this prevents the heap from being able to properly compact itself).  One quick and simple way to do this would be to use Marshal.AllocHGlobal method.</p>\n'}]
['https://Stackoverflow.com/questions/38302', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4055/']