A few days ago I checked the implementation of the C # Binary Tree Test @ Benchmark Game computer game and was strangely surprised: the node tree is described as a struct ( TreeNode ) referring to a class ( Next ) that has two fields of type TreeNode (strut). This clearly looks strange, so I updated this code to use a single class (ie ~ class TreeNode { TreeNode Left, Right } ). My implementation of ItemCheck :
public int ItemCheck() { if (ReferenceEquals(Left, null))
As you can find, this is very similar to the original implementation. However, the performance of this code was 2.2 times worse!
Can someone explain why such "optimization" makes sense in .NET? I basically want to understand what the other consequences of this are - of course, if it's not just a flaw in the C # / JIT compiler.
A more readable version of the same code with a few minor performance improvements can be found here: https://github.com/alexyakunin/BenchmarkGame-CSharp/blob/master/src/BinaryTrees/Program.cs
Update: I created an additional project for comparison:
source share