title: How is it possible that a handMade dictionary is way faster than .Net dictionary?
body: <p>Reviewing one opensource project, I've come across one interesting Data structure:</p>
  
  <pre><code>// Represents a layer of "something" that covers the map
  public class CellLayer&lt;T&gt; : IEnumerable&lt;T&gt;
  {
      public readonly Size Size;
      public readonly TileShape Shape;
      public event Action&lt;CPos&gt; CellEntryChanged = null;
  
      readonly T[] entries;
  
      public CellLayer(Map map)
          : this(map.TileShape, new Size(map.MapSize.X, map.MapSize.Y)) { }
  
      public CellLayer(TileShape shape, Size size)
      {
          Size = size;
          Shape = shape;
          entries = new T[size.Width * size.Height];
      }
  
      public void CopyValuesFrom(CellLayer&lt;T&gt; anotherLayer)
      {
          if (Size != anotherLayer.Size || Shape != anotherLayer.Shape)
              throw new ArgumentException(
                  "layers must have a matching size and shape.", "anotherLayer");
          if (CellEntryChanged != null)
              throw new InvalidOperationException(
                  "Cannot copy values when there are listeners attached to the CellEntryChanged event.");
          Array.Copy(anotherLayer.entries, entries, entries.Length);
      }
  
      // Resolve an array index from cell coordinates
      int Index(CPos cell)
      {
          return Index(cell.ToMPos(Shape));
      }
  
      // Resolve an array index from map coordinates
      int Index(MPos uv)
      {
          return uv.V * Size.Width + uv.U;
      }
  
      /// &lt;summary&gt;Gets or sets the &lt;see cref="OpenRA.CellLayer"/&gt; using cell coordinates&lt;/summary&gt;
      public T this[CPos cell]
      {
          get
          {
              return entries[Index(cell)];
          }
  
          set
          {
              entries[Index(cell)] = value;
  
              if (CellEntryChanged != null)
                  CellEntryChanged(cell);
          }
      }
  
      /// &lt;summary&gt;Gets or sets the layer contents using raw map coordinates (not CPos!)&lt;/summary&gt;
      public T this[MPos uv]
      {
          get
          {
              return entries[Index(uv)];
          }
  
          set
          {
              entries[Index(uv)] = value;
  
              if (CellEntryChanged != null)
                  CellEntryChanged(uv.ToCPos(Shape));
          }
      }
  
      /// &lt;summary&gt;Clears the layer contents with a known value&lt;/summary&gt;
      public void Clear(T clearValue)
      {
          for (var i = 0; i &lt; entries.Length; i++)
              entries[i] = clearValue;
      }
  
      public IEnumerator&lt;T&gt; GetEnumerator()
      {
          return (IEnumerator&lt;T&gt;)entries.GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  </code></pre>
  
  <p>This structure represents a matrix of type T in that, given a CPos (an X,Y) structure, it returns the T element at that position. Here's one sample usage:</p>
  
  <pre><code>var dic = new CellLayer&lt;CellInfo&gt;(TileShape.Rectangle, new Size(1280,1280));
  cellLayer[new CPos(0, 1)] = new CellInfo(0, new CPos(0, 1), false);
  </code></pre>
  
  <p>Internally, the CellLayer class transforms the given CPos into a int which operates as the index for the internal array.</p>
  
  <p>By the looks of how the class operates from a client-side perspective, it felt to me like a Dictionary, so I replaced the implementation. After several runtimes tests and microbenchmarking, it turned out that using the dictionary was dozens of times slower than using the handmade CellLayer class. That surprised me. Here are the tests I did:</p>
  
  <pre><code>    [Test]
      public void DictionaryTest()
      {
          var dic = new Dictionary&lt;CPos, CellInfo&gt;(1280 * 1280);
  
          var watch = Stopwatch.StartNew();
  
          for (int i = 0; i &lt; 1280; i++)
              for (int u = 0; u &lt; 1280; u++)
                  dic[new CPos(i, u)] = new CellInfo(0, new CPos(i, u), false);
  
          Console.WriteLine(watch.ElapsedTicks);
      }
  
      [Test]
      public void CellLayerTest()
      {
          var dic = new CellLayer&lt;CellInfo&gt;(TileShape.Rectangle, new Size(1280,1280));
  
          var watch = Stopwatch.StartNew();
  
          for (int i = 0; i &lt; 1280; i++)
              for (int u = 0; u &lt; 1280; u++)
                  dic[new CPos(i, u)] = new CellInfo(0, new CPos(i, u), false);
  
          Console.WriteLine(watch.ElapsedTicks);
      }
  </code></pre>
  
  <p>I thought that .NET Collections were as optimized as possible. Can anyone explain to me how is it that using Dictionary is slower that using a "custom Dictionary"?</p>
  
  <p>Thanks</p>